2

Currently i'm using nginx server and using nodejs server as reverse proxy. I want to make the nginx server proxy_pass different server by location.

So, lets say my domain is subdomain.domain.com.

When loading subdomain.domain.com/, this should serve static files.

When loading subdomain.domain.com/example1/req1/req2, this should route to 127.0.0.1:3000/req1/req2.

When loading subdomain.domain.com/example2/req1/req2, this should route to 127.0.0.1:8080/req1/req2.

But my configuration routes subdomain.domain.com/example1/req1/req2 to 127.0.0.1:3000/example1/req1/req2 resulting error. (nodejs returns Cannot GET /example1)

How should I write this nginx conf file properly?

Stefan
  • 17,448
  • 11
  • 60
  • 79
parkjbdev
  • 33
  • 2
  • 5
  • Any results from `nginx -t`? – ti7 Jan 29 '21 at 03:07
  • nginx -t test was successful – parkjbdev Jan 29 '21 at 03:26
  • If one of the Answers solves your Question, please mark it as such with the check to its left! If you found the answer on your own or with some combination, you can also answer your own question and mark it after 2 days or so! This adds to the body of knowledge and can help you gain reputation on the site! – ti7 Jan 29 '21 at 05:28

2 Answers2

3

Try use rewrite directive in location block.

Something like:

location /example1/ {
  rewrite     ^/example1/(.*)$ /$1 break;
  proxy_pass  http://xxxxxx;
}

You can check documents related to rewrite module at http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

kelgon
  • 391
  • 3
  • 6
1

You need to add below code snippet to your nginx.conf.

    server subdomain.domain.com;
        location / {
            rewrite     ^/example1/(.*)$ /$1 break;
            proxy_pass http://127.0.0.1:3000;
        }
    }