2

So I was accessing my server at domain.com:3333 but just switched it it to domain.com/nodeapp using upstream and proxy pass in my nginx config. The redirect works just fine, but now the links to static files being used my index.html (/styles/style.css & socket.io/socket.io.ks) are broken and all that displays is raw html.

From index.js

app.get('/nodeapp', function(req,res){
 res.sendFile(path.join(__dirname, '/views', 'index.html'));
});

app.use('/styles', express.static(path.join(__dirname, '/views/styles')));

From index.html

<link rel="stylesheet" type="text/css" href="styles/style.css">
<script src="socket.io/socket.io.js"></script>

From .../nginx/sites-enabled/default

upstream nodeapp{
  server localhost:3333 fail_timeout=0;
}

server{
....

    location /nodeapp{
      proxy_pass http://nodeapp;
    }

....
}

How can I re-establish the link?

Note that in my index.js before the switch the first line shown was app.get('/', function(req,res){

0

2 Answers 2

2

So, this was kind of tricky to figure out, but painfully simple.

location /socket.io {
    proxy_pass http://nodeapp;
}

location /styles {
    proxy_pass http://nodeapp;
}

The paths that index.html links to must be set as locations in the nginx conf that prox_pass to the node server.

1
1

Add a trailing slash to your location and to the proxy_pass target and let your original index.js untouched. Your links however should point to /nodeapp/{something} in index.html.

location /nodeapp/ {
    proxy_pass http://nodeapp/;
}
5
  • Ok, so I took your suggestion on proxy_pass and reverted my index.js to only get '/'... the page is being served but my styles and socket.io are still inaccessible. In console I get this domain.com/socket.io/socket.io.js Failed to load resource: the server responded with a status of 404 (Not Found) I tried changing the paths in index.html to nodeapp/socket.io/socket.io.js but still get the same errors. Oct 11, 2014 at 22:56
  • @AllTheTime Post the full nginx configuration Oct 11, 2014 at 23:06
  • here's everything gist.github.com/allthetime/3be90d230508bc7500dc Oct 11, 2014 at 23:10
  • @AllTheTime So you are not serving any static resource from nginx ? What should serve the socket.io.js file ? If you modified the index.html paths and they aren't taken into account you have something cached in your browser. Oct 11, 2014 at 23:23
  • 1
    No sorry, I was unclear, they were taken into account. Instead the error was domain.com/nodeapp/socket.io... Failed to load resource And, no, at this point I am not. The way the socket resources are linked to is confusing to me... Because there is no folder that exists called socket.io where socket.io.js resides. It is actually deep in the node_modules file in a folder called socket.io-client. I can force style.css to load by making a direct link to its location with nginx, but this is very hacky, and doesn't work at all for socket.io.js because it has links that break using this method Oct 11, 2014 at 23:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .