sac Dahal
sac Dahal

Reputation: 1241

Nginx error with configuration wile reverse proxy shows "Welcome to nginx" when starting nginx

This is my first time playing around with nginx. I have 2 express servers running in my localhost in port 3001 and 3002. Which is running perfectly. I am using ubuntu these are following steps I have taken for nginx.

All I get is Welcome to nginx If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

but it yield to same result. Please any guidance will be helpful. Thank you.

Upvotes: 0

Views: 5019

Answers (2)

NullDev
NullDev

Reputation: 7303

Try to to use proxy_pass with the local host and without quotes.

Say you want the application on port 3001 as your website, you need to configure it like that:

location / {
    proxy_pass http://localhost:3001/;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

This code needs to be in the server block of your default file.

It will pass all traffic from the location / (your web root) to the port 3001.

A complete example would look like this:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name example.com;

    location / {
        proxy_pass http://localhost:3001/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Explanation:

proxy_pass http://localhost:3001/;

This will pass all traffic to the port 3001.


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

This will pass informations such as the IP which made the call to the proxied server.


proxy_set_header X-Forwarded-Proto $scheme;

This will forward the used sheme/protocol information of the request.

Upvotes: 1

rijin
rijin

Reputation: 1759

Mostly your configuration file problem. see, below example different express apps running in 3000 and 3001 ports. this is how the config file looks like in my nginx config file

server {  
    listen 0.0.0.0:80;
    server_name stage.chat.in www.stage.chat.in;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $proxy_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass  http://127.0.0.1:3000;
    }
}
server {  
    listen 0.0.0.0:80;
    server_name api.chat.in www.api.chat.in;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $proxy_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass  http://127.0.0.1:3002;
    }
}

Upvotes: 1

Related Questions