Nginx not proxying correctly nodejs

2019-07-26 04:21发布

问题:

I currently have ssl set up on my nginx sever. I also have a node app running on port 80. I'm trying to reverse proxy all requests to the /api route of nginx but its not doing so correctly.

When I do a curl localhost:8080 it correctly shows Welcome to hooq.

However when I do a curl command to the direct webserver ip address with which is hooq.digitalfolks.sg/api, It's throwing a 404 error which I have set up in node when a route is not found.

Here is my nginx conf at the moment

server {
        listen 443 ssl;

        server_name hooq.digitalfolks.sg;

        ssl_certificate /etc/letsencrypt/live/hooq.digitalfolks.sg/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/hooq.digitalfolks.sg/privkey.pem;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_dhparam /etc/ssl/certs/dhparam.pem;
        ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
        ssl_session_timeout 1d;
        ssl_session_cache shared:SSL:50m;
        ssl_stapling on;
        ssl_stapling_verify on;
        add_header Strict-Transport-Security max-age=15768000;

        location / {

        }

        location ~ /api {
                proxy_pass http://127.0.0.1:8080;
        }

        location ~ /.well-known {
                allow all;
        }

}


server {
    listen 80;
    server_name hooq.digitalfolks.sg;
    return 301 https://$host$request_uri;
}

Can someone help me out?

回答1:

If your Node app doens't expect the /api in the route then try changing this:

    location ~ /api {
            proxy_pass http://127.0.0.1:8080;
    }

to this:

    location ~ /api {
            rewrite ^/api/(.*)$ /$1 break;
            proxy_pass http://127.0.0.1:8080;
    }

You didn't include the code of your Node app so it's impossible to tell for sure if it's the case here but it is a common problem with reverse proxy configuration - see this answer for more details:

  • Cannot run multiple NodeJs server on one subdomain


标签: node.js nginx