Cross-domain configuration issues using nginx, sails and upstarts

I am having problems setting up cross domains using nginx and sails. The problem is that if I correctly connect the socket through http://domain-server.com/socket.io/ then the RESTful request will not be executed. If RESTful works, the socket will not.

Customization

Customer http://domain.com

Server http://server-domain.com

The client is the angular application "sails.io.js": "0.10.3" and this configuration io.sails.url = 'http://domain-server.com'; .

Server is a sails application with RESTful request functions and sockets.

Sails CORS Configuration

 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' }; 

Sails Socket Configuration

 module.exports.socket { onConnect: function() {}, onDisconnect: function() {}, authorization: false, 'backwardsCompatibilityFor0.9SocketClients': false, grant3rdPartyCookie: true, origins: 'http://domain.com' }; 

Nginx configuration

Commented on what I was looking for in nginx configuration without much success (also tried client address instead of *).

 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 method calling RESTful calls

 function query(path, attributes) { return $http({ url: domain + path, params: attributes, method: "GET", widthCredentials: true }); } 

In angular config function

 $httpProvider.defaults.useXDomain = true; 

This is the current configuration, and here are the results that I am experiencing.

Browser Console Output

Resource interpreted as Script but transferred with MIME type text/html: "http://domain-server.com/__getcookie". - Good

|>
\___/ sails.io.js:200 io.socket connected successfully.
- Good

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. - Not good

UPDATE

Everything seems to work if I start sails directly using sails lift or node app.js , but when running with upstart script in the .conf file there is a cross domain problem.

upstart script

 #!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 
+5
source share
1 answer

Try adding the following to the top of your app.js Sails file:

 process.chdir(__dirname); 

not found , which you see is most likely the default message is Express 404. If you try to raise the Sails application outside your own directory, API files (for example, controllers) will not load properly. At some point, process.chdir was added to app.js by default to prevent this problem, but it looks like there was a regression somewhere along the way.

+1
source

Source: https://habr.com/ru/post/1201403/


All Articles