我正在使用nginx和sails跨域configuration问题。 问题是,如果我通过http://domain-server.com/socket.io/
正确连接套接字,那么RESTful请求将失败。 如果RESTful工作,套接字将不会。
build立
客户端http://domain.com
服务器http://server-domain.com
客户端是一个angular度的应用程序使用"sails.io.js": "0.10.3"
和这个configurationio.sails.url = 'http://domain-server.com';
"sails.io.js": "0.10.3"
io.sails.url = 'http://domain-server.com';
。
Server是一个具有RESTful请求和套接字function的sails应用程序。
启动CORSconfiguration
module.exports.cors = { allRoutes: true, origin: 'http://domain.com', credentials: true, methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD', headers: 'content-type,Access-Control-Allow-Origin' };
风帆套接字configuration
module.exports.socket { onConnect: function() {}, onDisconnect: function() {}, authorization: false, 'backwardsCompatibilityFor0.9SocketClients': false, grant3rdPartyCookie: true, origins: 'http://domain.com' };
nginxconfiguration
注释掉了,我在nginxconfiguration中弄了个没有太多成功(也尝试了客户端地址而不是*)。
server { listen 80; server_name domain-server.com; #add_header Access-Control-Allow-Origin *; location / { # add_header Access-Control-Allow-Origin *; proxy_pass http://localhost:1337; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; # proxy_set_header Access-Control-Allow-Origin *; proxy_cache_bypass $http_upgrade; } }
创buildRESTful调用的angularjs服务方法
function query(path, attributes) { return $http({ url: domain + path, params: attributes, method: "GET", widthCredentials: true }); }
在angular度configurationfunction
$httpProvider.defaults.useXDomain = true;
这是目前的configuration,这是我正在经历的结果。
浏览器控制台输出
Resource interpreted as Script but transferred with MIME type text/html: "http://domain-server.com/__getcookie".
– 很好
|>
– 很好
\___/ sails.io.js:200 io.socket connected successfully.
XMLHttpRequest cannot load http://domain-server.com/login?password=test&username=test. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://domain.com' is therefore not allowed access.
– 不好
UPDATE
看起来好像一切正常,如果我直接使用sails lift
或node app.js
启动node app.js
但是在.conf
文件中使用upstart脚本启动它时会出现跨域问题。
暴发户脚本
#!upstart description "demo" author "gillesc" start on (local-filesystems and net-device-up IFACE=eth0) stop on shutdown respawn respawn limit 5 60 script exec /usr/bin/node /var/www/apps/domain-server.com/app.js > /var/www/apps/domain-server.com/server.log 2>&1 end script
尝试将以下内容添加到您的Sails应用的app.js文件的顶部:
process.chdir(__dirname);
not found
你所看到的可能是默认的Express 404消息。 如果您尝试在其自己的目录之外提取Sails应用程序,则API文件(例如控制器)将无法正确加载。 process.chdir
被添加到了默认的app.js中 ,以防止这个问题,但是看起来在这个process.chdir
中有一个回归。