Nginx forwarding without proxy

Asked

Viewed 110 times

-1

I’m setting up Nginx to receive requests and delegate to a Nodejs server. But there is a url/route that needs to render a static html outside the Nodejs server. Case similar to this:

upstream node-upstream {
   least_conn;
   server localhost:3000;
   keepalive 64;
}

server {
   listen 80;
   server_name 0.0.0.0;
   charset utf-8;

    location / {
       proxy_pass http://node-upstream;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       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-NginX-Proxy true;
       proxy_cache_bypass $http_upgrade;
       proxy_redirect off;
    }
}


server {
   listen 80;
   server_name 0.0.0.0;

   charset utf-8;
   access_log /var/log/nginx/adm.access.log  main;

   location /adm {
      alias  /usr/share/nginx/html/adm;
       index  index.html index.htm;
   }
}

Is there any way this can be done? With the above rules not working, the Nodejs Server responds first.

  • Did you try to put both Locations inside the same server? Since both Servers are the same, it seems that you are just repeating things, just that both Ocations stay in the first, I believe that the Location /Adm go first, but it’s been a while since I use ngnix so I can’t say

  • It worked. Thank you! I’m a beginner in this part of Nginx

1 answer

1


You created two serve {} with the same parameters of port and server_name, then probably only the first is recognized, the second is ignored, to solve just put both location{} within the same serve {}, thus:

server {
   listen 80;
   server_name 0.0.0.0;
   charset utf-8;
   access_log /var/log/nginx/adm.access.log  main;

    location / {
       proxy_pass http://node-upstream;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       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-NginX-Proxy true;
       proxy_cache_bypass $http_upgrade;
       proxy_redirect off;
    }

    location /adm {
       alias  /usr/share/nginx/html/adm;
       index  index.html index.htm;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.