简体   繁体   中英

nodejs nginx 502 gateway error

I am trying to use a nodejs app behind an nginx reverse proxy to handle the ssl

I have my app running on localhost:2000. I can confirm this as working with a curl command.

This is my nginx setup:

  # the IP(s) on which your node server is running. I chose port 3000.
upstream dreamingoftech.uk {
server 127.0.0.1:2000;
keepalive 16;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
server_name dreamingoftech.uk;
return 301 https://$host$request_uri;
}
#HTTPS
server {
listen 443 ssl http2;

server_name dreamingoftech.uk;
access_log /var/log/nginx/dreamingoftech.log;
error_log /var/log/nginx/dreamingoftech.error.log debug;

ssl_certificate /etc/letsencrypt/live/dreamingoftech.uk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/dreamingoftech.uk/privkey.pem;

include snippets/ssl-params.conf;

# pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
location / {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-Forwarded-Proto https;
  proxy_set_header X-NginX-Proxy true;
  proxy_pass http://dreamingoftech.uk/;
  proxy_redirect off;
  #proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "";
  proxy_ssl_session_reuse off;
  proxy_cache_bypass $http_upgrade;
}
}

if I now curl https://dreamingoftech.uk , it takes a while but I do get the webpage delivered. albeit with the message:

curl: (18) transfer closed with 1 bytes remaining to read

However when viewed from a browser I get a 502 gateway error.

I have checked the error log and this is the result: ERROR LOG

I can't understand why the reverse proxy is adding such a time delay into the process. Any ideas would be greatly appreciated.

PS: in the upstream config I have tried localhost instead of 127.0.0.1 to no avail

I have almost the same configuration. Can you try the following

You can redirect all http to https

server {
    listen  80;
    return 301 https://$host$request_uri;
}

or for a specific site like this

server {
    server_name dreamingoftech.uk;
    return 301 https://dreamingoftech.uk$request_uri;
}

but choose only one for your case

and then you make sure you node server is running on http mode and not https.

Also you mentioned that you run node on port 3000, then use port 3000 and not 2000 as I can see in your config.

After you confirm the above redirect all packets into localhost like this

server {

    listen 443;
    server_name dreamingoftech.uk;

    ssl_certificate           /etc/letsencrypt/live/dreamingoftech.uk/fullchain.pem;
    ssl_certificate_key       /etc/letsencrypt/live/dreamingoftech.uk/privkey.pem;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;

    location / {

      proxy_set_header        Host $host;
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;

      # Fix the “It appears that your reverse proxy set up is broken" error.
      proxy_pass          http://localhost:3000;
      proxy_read_timeout  90s;

      proxy_redirect      http://localhost:3000 https://dreamingoftech.uk;
    }
 }

Create a file and sum the above code put it in sites-available with a name like dreamingoftech.uk and the use ln -s to create a softlink into sites-enabled . go to your nginx.conf and make sure you include folder sites-enabled

Then must restart nginx to check if it works

@Stamos Thanks for your reply. I tried that but unfortunately it didn't work. I decided to try the most basic node app I could still using the basic modules I am using.

I tried this and it worked straight away.

The problem is with my app therefore. I will spend time rebuilding and testing step by step until I find the issue,

Thanks for your time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM