Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cross domain configuration issues using nginx, sails and upstart

I'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 "sails.io.js": "0.10.3" and this config io.sails.url = 'http://domain-server.com';.

Server is a sails application with both RESTful request and socket features.

Sails CORS config

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 config

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

nginx config

Commented out are what I've fiddled with in the nginx configuration without much success (also tried the 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 service method making the RESTful calls

function query(path, attributes) {

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

In the angular config function

$httpProvider.defaults.useXDomain = true;

It is the current configuration and here are the results 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

It seems like everything works wonder if I start sails directly using sails lift or node app.js yet when starting it using the upstart script in a .conf file the cross domain issue occurs.

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
like image 315
GillesC Avatar asked Nov 10 '22 02:11

GillesC


1 Answers

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

process.chdir(__dirname);

The not found you're seeing is likely the default Express 404 message. If you try to lift a Sails app outside of its own directory, the API files (e.g. controllers) won't get loaded properly. The process.chdir was added to the default app.js at some point to prevent this problem, but it looks like there was a regression somewhere along the way.

like image 140
sgress454 Avatar answered Nov 15 '22 11:11

sgress454