【问题标题】:allow cross origin request for laravel route on nginx允许在 nginx 上对 laravel 路由进行跨域请求
【发布时间】:2018-12-28 08:56:21
【问题描述】:

我想从另一个来源访问 laravel 5.5 api 端点 https://foo.bar.com/api/v1.0/foo/bar。因此我需要允许跨源请求。我已将标头添加到我的 nginx 配置中。然而我的浏览器仍然抱怨它不存在。 这是我的 nginx 配置:

server {
   listen       *:443 ssl;

   server_name  foo.bar.com ;
   ssl on;

   ssl_certificate           /etc/nginx/nxv_bhxwewp1idzm.crt;
   ssl_certificate_key       /etc/nginx/nxv_bhxwewp1idzm.key;
   ssl_session_cache         shared:SSL:10m;
   ssl_session_timeout       5m;
   ssl_protocols             TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers               "...";
   ssl_prefer_server_ciphers on;
   client_max_body_size 1m;
     index  index.html index.htm index.php;

   access_log            /var/log/nginx/ssl-nxv_bhxwewp1idzm.access.log;
   error_log             /var/log/nginx/ssl-nxv_bhxwewp1idzm.error.log;


   root /var/www/share/foo.bar.com;
   location ~ ^/index\.php(/|$) {


     set $path_info $fastcgi_path_info;
     root  /var/www/share/foo.bar.com/public/;
     fastcgi_index index.php;
     fastcgi_split_path_info ^(.+\.php)(/.*)$;
     try_files $uri $uri/ /index.php$is_args$args;
     include /etc/nginx/fastcgi_params;
     fastcgi_pass 127.0.0.1:9000;
     add_header 'Access-Control-Allow-Origin' '*';

     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

   }
   location / {

    root  /var/www/share/foo.bar.com/public/;
    try_files $uri $uri/ /index.php$is_args$args;
    autoindex off;
    index  index.html index.php;
    add_header 'Access-Control-Allow-Origin' '*';


   }
   sendfile off;
 }

【问题讨论】:

  • 据我所知,PHP 负责自己的标头。你可以使用github.com/barryvdh/laravel-cors
  • 这并不完全正确,您可以配置 Web 服务器来处理 CORS 或 PHP 可以完成这项工作。使用 Web 服务器的好处是您不必为了生成 CORS 响应而启动 Laravel 应用程序。这是一个很好的configuration for nginx

标签: laravel http nginx cross-domain


【解决方案1】:

我已经从@DigitalDrifter 发布的链接中获取了信息。但似乎只是添加Access-Control-Allow-Origin 不足以让它工作。虽然我不关心访问方法等。 所以这让交易成功了:

server {
   listen       *:443 ssl;

   server_name  foo.bar.com ;
   ssl on;

   ssl_certificate           /etc/nginx/nxv_bhxwewp1idzm.crt;
   ssl_certificate_key       /etc/nginx/nxv_bhxwewp1idzm.key;
   ssl_session_cache         shared:SSL:10m;
   ssl_session_timeout       5m;
   ssl_protocols             TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers               "...";
   ssl_prefer_server_ciphers on;
   client_max_body_size 1m;
     index  index.html index.htm index.php;

   access_log            /var/log/nginx/ssl-nxv_bhxwewp1idzm.access.log;
   error_log             /var/log/nginx/ssl-nxv_bhxwewp1idzm.error.log;


   root /var/www/share/foo.bar.com;
   location ~ ^/index\.php(/|$) {


     set $path_info $fastcgi_path_info;
     root  /var/www/share/foo.bar.com/public/;
     fastcgi_index index.php;
     fastcgi_split_path_info ^(.+\.php)(/.*)$;
     try_files $uri $uri/ /index.php$is_args$args;
     include /etc/nginx/fastcgi_params;
     fastcgi_pass 127.0.0.1:9000;
     add_header 'Access-Control-Allow-Origin' '*';
     add_header 'X-Frame-Options' 'ALLOW-FROM *';
     add_header 'Access-Control-Allow-Credentials' 'true';
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
     add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

   }
   location / {

    root  /var/www/share/foo.bar.com/public/;
    try_files $uri $uri/ /index.php$is_args$args;
    autoindex off;
    index  index.html index.php;
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'X-Frame-Options' 'ALLOW-FROM *';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';


   }
   sendfile off;
 }

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 2012-11-03
    • 2012-04-27
    • 2011-10-08
    • 2013-03-24
    • 2015-10-11
    相关资源
    最近更新 更多