我正在使用nginx和sails進行跨域配置問題。問題是 如果我通過http://domain-server.com/socket.io/
正確連接套接字,則RESTful請求將失敗。如果RESTful工作,套接字不會。使用nginx,sails和新貴跨域配置問題
設置
http://domain.com
客戶
服務器http://server-domain.com
客戶端是使用"sails.io.js": "0.10.3"
和這個配置io.sails.url = 'http://domain-server.com';
的角應用。
服務器是一個具有RESTful請求和套接字功能的sails應用程序。
帆CORS配置
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'
};
帆插座配置
module.exports.socket {
onConnect: function() {},
onDisconnect: function() {},
authorization: false,
'backwardsCompatibilityFor0.9SocketClients': false,
grant3rdPartyCookie: true,
origins: 'http://domain.com'
};
nginx的配置
註釋掉是什麼,我在nginx的配置不撥弄着很成功(也是用客戶端地址代替*)。
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;
}
}
angularjs服務方法使得基於REST調用
function query(path, attributes) {
return $http({
url: domain + path,
params: attributes,
method: "GET",
widthCredentials: true
});
}
在角配置功能
$httpProvider.defaults.useXDomain = true;
這是當前的配置和這裏的結果我遇到。
瀏覽器控制檯輸出
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
開始帆,但使用.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
託管在哪裏?某些雲主機(如OpenStack)似乎阻止發送CORS頭文件。 – sgress454 2014-08-29 17:56:29
我可以讓CORS爲RESTful正常工作,它只是當他們做的東西的socket.io端不再工作。聽起來就像某處有衝突。 – GillesC 2014-08-29 21:38:20
如果您在瀏覽器中導航到「http://domain-server.com/login?password = test&username = test」,您會得到什麼?它是一個nginx錯誤頁面嗎?你是否正確地從nginx向sails應用程序反轉代理請求?還是他們在不同的端口? – david 2014-08-31 10:43:46