Reputation: 1241
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.
First I used sudo apt-get install nginx
delete default file in sites-enabled and sites-available sudo rm -f default
create default file in sites-available where I have these codes (just trying to run one server in port 3001 for now.) sudo vi default
server
{
listen 80;
location /
{
proxy_pass "http://192.168.100.5:3001";
}
}
symlink default from sites-available to sites-enabled : sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
start nginx sudo /etc/init.d/nginx start
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.
also I tried using this configuration in sites-enabled
upstream project { server http://localhost:3001; } server { listen 80; } }
but it yield to same result. Please any guidance will be helpful. Thank you.
Upvotes: 0
Views: 5019
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
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