简体   繁体   中英

How to redirect all traffic from nginx to nodeJS with proxy-pass

When I setup simple nginx location to catch all traffic to root and proxy-pass to nodeJS on port 3000, when I request http://example.com/something

When I enter mydomain.net I got result from nodeJS, but when I tried example.com/something, I'm getting 502 Bad Gateway.

server {
        listen xxx.xxx.xxx.xxx:80;
        server_name example.com  www.example.com;

        location / {
                proxy_pass http://xxx.xxx.xxx.xxx:3000/;
        }

}

I expect nodeJS to handle rest of url (after /) as this working without nginx, when nodeJS is alone on port 80.

aste the following code in the file and make sure to change example.com to your domain (or IP), and 1337 to your Node.js application port:

server {
listen 80;
server_name example.com;

location / {
    proxy_set_header   X-Forwarded-For $remote_addr;
    proxy_set_header   Host $http_host;
    proxy_pass         "http://127.0.0.1:3000";
}
}

than

sudo nginx -t 
sudo service nginx restart

I figure out that nginx code was ok, my SSL configuration seems to make problem here and after checked error log of nginx, started again and get successfully all locations to NodeJS. Thanks all.

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