无法在 nginx 上禁用同源策略

Posted

技术标签:

【中文标题】无法在 nginx 上禁用同源策略【英文标题】:Can't disable same origin policy on nginx 【发布时间】:2014-01-29 01:34:24 【问题描述】:

我需要在服务器上禁用同源策略。作为背景:我已经通过禁用网络安全标志启动 chrome 来验证一切正常。一切正常。

这是我在 nginx 方面所做的:

upstream phpfcgi 
  server unix:/var/run/php5-fpm.sock; #for PHP-FPM running on UNIX socket


server 
listen 80;

root /var/www/yammi2;
    index index.html index.php index.htm;
server_name myserver.ch;

add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Credentials 'true';
    add_header Access-Control-Allow-Headers 'Content-Type,accept,x-wsse,origin';
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';

# strip app.php/ prefix if it is present
    rewrite ^/app\.php/?(.*)$ /$1 permanent;

location / 
    index app.php;
    try_files $uri @rewriteapp;


location @rewriteapp 
    rewrite ^(.*)$ /app.php/$1 last;


# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(app|app_dev|config)\.php(/|$) 
    fastcgi_pass phpfcgi;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS off;

   

当我进行 curl 调用时:curl -I myserver.ch,我得到以下结果:

HTTP/1.1 302 Found
Server: nginx/1.1.19
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.3.10-1ubuntu3.9
Set-Cookie: PHPSESSID=gvcl3v533ib91l2c6v888gl9d3; path=/
cache-control: no-cache
date: Fri, 10 Jan 2014 07:01:18 GMT
location: http://myserver.ch/admin/restaurant
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type,accept,x-wsse,origin
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE

所以至少看起来标题设置正确,但我进行 ajax 调用时的结果:

OPTIONS http://myserver.ch/api/v1/restaurant/closest?max=50&lat=47&lon=8 500 (Internal Server Error) jquery-2.0.3.js:7845
OPTIONS http://myserver.ch/api/v1/restaurant/closest?max=50&lat=47&lon=8 Origin http://localhost is not allowed by Access-Control-Allow-Origin. jquery-2.0.3.js:7845
XMLHttpRequest cannot load http://myserver.ch/api/v1/restaurant/closest?max=50&lat=47&lon=8. Origin http://localhost is not allowed by Access-Control-Allow-Origin. overview.html:1

我对“内部服务器错误”有点困惑,但我想既然它与标志一起工作,这一定与同源有关。

服务器应用程序是一个 Symphony 应用程序。我希望我没有错过任何东西。知道如何解决这个问题吗?甚至如何调试?

也许最后一个被剪断了,这是我拨打电话的方式(同样,这不应该是问题,因为使用禁用安全标志它可以按预期工作):

$.ajax(
          url: url,
          headers: "x-wsse": getWsseHeader(),
          beforeSend: function (request) 
                request.setRequestHeader("x-wsse", getWsseHeader());
          ,
          success: function() 

          ,
          error: function(error) 
            console.log(error.statusText);
          
        );

【问题讨论】:

尝试使用 curl STATIC 文件调用并检查是否有效。如果您的脚本出现内部错误 - 服务器配置中的问题导致其他结果。这是您的起点。 好提示。因此,将请求指向静态文件可以按预期工作,但是一旦我添加了额外的 x-wsse 标头,它就不再起作用(即使是静态文件)。当我通过 Postman(Rest 客户端)提交相同的请求时,它工作得很好。更多提示? 做了一些调整。我现在可以发送预检 OPTIONS 调用,成功返回,但是一旦我进行 GET 调用,请求就会被取消?!没有响应,服务器上也没有任何响应。 【参考方案1】:

改变这一行

add_header Access-Control-Allow-Origin *;

作为

add_header 'Access-Control-Allow-Origin' '';

【讨论】:

以上是关于无法在 nginx 上禁用同源策略的主要内容,如果未能解决你的问题,请参考以下文章

无法解决的 CORS 问题!如何在 MacOS 上禁用 Chrome 的同源策略?

webdriver.io 无法为 iframe 禁用同源策略

Chrome:禁用本地主机的同源策略

如何在 Safari(Windows 7)中禁用同源策略?

在 Mobile Safari 中禁用同源策略

在 Chrome 中禁用同源策略