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

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