{"id":7842,"date":"2015-10-31T12:14:26","date_gmt":"2015-10-31T12:14:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/31\/cross-domain-configuration-issues-using-nginx-sails-and-upstart-open-source-projects-balderdashy-sails\/"},"modified":"2015-10-31T12:14:26","modified_gmt":"2015-10-31T12:14:26","slug":"cross-domain-configuration-issues-using-nginx-sails-and-upstart-open-source-projects-balderdashy-sails","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/31\/cross-domain-configuration-issues-using-nginx-sails-and-upstart-open-source-projects-balderdashy-sails\/","title":{"rendered":"cross domain configuration issues using nginx, sails and upstart-open source projects balderdashy\/sails"},"content":{"rendered":"<p>I&#8217;m having cross domain configuration issues using nginx and sails. The problem is that if I get the socket to connect properly via <code>http:\/\/domain-server.com\/socket.io\/<\/code> then the RESTful request will fail. If RESTful works the socket will not.<\/p>\n<p><strong>Setup<\/strong><\/p>\n<p>Client <code>http:\/\/domain.com<\/code><\/p>\n<p>Server <code>http:\/\/server-domain.com<\/code><\/p>\n<p>Client is an angular application using <code>\"sails.io.js\": \"0.10.3\"<\/code> and this config <code>io.sails.url = 'http:\/\/domain-server.com';<\/code>.<\/p>\n<p>Server is a sails application with both RESTful request and socket features.<\/p>\n<p><strong>Sails CORS config<\/strong><\/p>\n<pre><code>module.exports.cors = {\n    allRoutes: true,\n    origin: 'http:\/\/domain.com',\n    credentials: true,\n    methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',\n    headers: 'content-type,Access-Control-Allow-Origin'\n};\n<\/code><\/pre>\n<p><strong>Sails socket config<\/strong><\/p>\n<pre><code>module.exports.socket {\n    onConnect: function() {},\n    onDisconnect: function() {},\n    authorization: false,\n    'backwardsCompatibilityFor0.9SocketClients': false,\n    grant3rdPartyCookie: true,\n    origins: 'http:\/\/domain.com'\n};\n<\/code><\/pre>\n<p><strong>nginx config<\/strong><\/p>\n<p>Commented out are what I&#8217;ve fiddled with in the nginx configuration without much success (also tried the client address instead of *).<\/p>\n<pre><code>server {\n    listen 80;\n\n    server_name domain-server.com;\n#add_header Access-Control-Allow-Origin *;\n    location \/ {\n#       add_header Access-Control-Allow-Origin *;\n\n        proxy_pass http:\/\/localhost:1337;\n        proxy_http_version 1.1;\n        proxy_set_header Upgrade $http_upgrade;\n        proxy_set_header Connection 'upgrade';\n        proxy_set_header Host $host;\n#        proxy_set_header Access-Control-Allow-Origin *;\n        proxy_cache_bypass $http_upgrade;\n   }\n}\n<\/code><\/pre>\n<p><strong>angularjs service method making the RESTful calls<\/strong><\/p>\n<pre><code>function query(path, attributes) {\n\n    return $http({\n        url: domain + path,\n        params: attributes,\n        method: \"GET\",\n        widthCredentials: true\n    });\n}\n<\/code><\/pre>\n<p><strong>In the angular config function<\/strong><\/p>\n<pre><code>$httpProvider.defaults.useXDomain = true;\n<\/code><\/pre>\n<p>It is the current configuration and here are the results I am experiencing.<\/p>\n<p><strong>Browser console output<\/strong><\/p>\n<p><code>Resource interpreted as Script but transferred with MIME type text\/html: \"http:\/\/domain-server.com\/__getcookie\".<\/code> &#8211; Good<\/p>\n<p><code>|&gt;<br \/>\n\\___\/ sails.io.js:200 io.socket connected successfully.<\/code> &#8211; Good<\/p>\n<p><code>XMLHttpRequest cannot load http:\/\/domain-server.com\/login?password=test&amp;username=test. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http:\/\/domain.com' is therefore not allowed access.<\/code> &#8211; Not good<\/p>\n<p><strong>UPDATE<\/strong><\/p>\n<p>It seems like everything works wonder if I start sails directly using <code>sails lift<\/code> or <code>node app.js<\/code> yet when starting it using the upstart script in a <code>.conf<\/code> file the cross domain issue occurs.<\/p>\n<p><strong>upstart script<\/strong><\/p>\n<pre><code>#!upstart\ndescription \"demo\"\nauthor \"gillesc\"\n\nstart on (local-filesystems and net-device-up IFACE=eth0)\nstop on shutdown\n\nrespawn\nrespawn limit 5 60\n\nscript\n  exec \/usr\/bin\/node \/var\/www\/apps\/domain-server.com\/app.js &gt; \/var\/www\/apps\/domain-server.com\/server.log 2&gt;&amp;1\nend script\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m having cross domain configuration issues using nginx and sails. The problem is that if I get the socket to connect properly via http:\/\/domain-server.com\/socket.io\/ then the RESTful request will fail. If RESTful works the socket will not. Setup Client http:\/\/domain.com Server http:\/\/server-domain.com Client is an angular application using &#8220;sails.io.js&#8221;: &#8220;0.10.3&#8221; and this config io.sails.url = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7842","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7842"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7842\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}