0

I have a express.js app running at some port and this nginx configuration:

server {
  server_name example.com;
  access_log /var/www/example/log/nginx.access.log;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://localhost:3000;
  }
}

There is index.html served the /source route which includes scripts like this:

<script src="/source/assets/js/script.js"></script>

The problem is that it cannot load this script, request results in 502 Bad Gateway.

enter image description here

While

curl http://example.com/source/assets/js/script.js

works fine, as does entering this url in the browser... Just reference from index.html doesn't... the same with style.css.

In the log I see this:

90.100.44.55 - - [26/Mar/2016:22:47:13 +0000] "GET /source/assets/js/script.js HTTP/1.1" 502 574 "http://example.com/source" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"

Direct request (curl or browser) that works looks like this:

90.100.44.55 - - [26/Mar/2016:22:47:24 +0000] "GET /source/assets/js/script.js HTTP/1.1" 200 8358 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"

Any ideas?

UPDATE:

I enabled nginx error log:

2016/03/27 00:26:36 [error] 32079#0: *319759 connect() failed (111: Connection refused) while connecting to upstream, client: 90.100.44.55, server: example.com, request: "GET /source/assets/css/styles.css HTTP/1.1", upstream: "http://127.0.0.1:3011/source/assets/css/styles.css", host: "example.com", referrer: "http://example.com/source"

But when I do

http://127.0.0.1:3011/source/assets/css/styles.css

on the server, it works :/

davidhq
  • 215
  • 2
  • 9
  • Looks like hotlink protection but not set up quite right. Please post details of your full stack (what exactly is nginx proxying, is there a CDN, Apache, cache, etc) and your full nginx config. – Tim Mar 26 '16 at 23:39
  • this is all regarding the server actually... no CDN / Apache / cache... and I haven't played with hotlink protection at all... This is really strange :/ – davidhq Mar 27 '16 at 00:12
  • I enabled nginx error log, see UPDATE above... I'm closer now but still very strange – davidhq Mar 27 '16 at 00:33
  • I now changed `pm2` which was serving the node project on port 3011 to nodemon and the problem is gone. Will investigate why `pm2` is like that... – davidhq Mar 27 '16 at 00:36
  • Why don't you allow nginx to serve the static files? – Michael Hampton Mar 27 '16 at 01:00
  • Sure, good idea of course... I have set up `nginx` to serve `/source/assets` now and this part worked, the problem is that `/source/scan` REST endpoint now has the same problem as assets had before... and this is not static, `script.js` calls it via ajax get... `pm2` still won't work, I'll try `Passenger` and I'll serve static assets via `nginx` directly as you suggested. – davidhq Mar 27 '16 at 04:01

1 Answers1

0

This is pm2 issue: nginx: connect() failed (111: Connection refused) while connecting to upstream

this solution (127.0.0.1 instead of localhost) didn't work for me...

I looked into pm2 log and found that this is the problem (I'm also using node-config): https://github.com/lorenwest/node-config/issues/244

WARNING: NODE_APP_INSTANCE value of '0' did not match any instance config file names.
WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode

I guess pm2 is not good enough for production, I'm going to try Phusion Passenger for node.

I still don't know how pm2 managed to see any difference between curl and a request from browser... but anyway, the only solution seems to be not to use pm2

davidhq
  • 215
  • 2
  • 9