Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

Nginx How do i route to reverse proxy by location

This is my first question on stackoverflow :D

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?



Problem solved thanks to comments??

question from:https://stackoverflow.com/questions/65948239/nginx-how-do-i-route-to-reverse-proxy-by-location

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...