Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct proxy path in nginx.conf

Tags:

nginx

we have two servers, A and B. Server A is accessed worldwide. He has nginx installed. That's what I have in conf:

location /test {   proxy_pass http://localserver.com; } 

What it should do, is translate the addreess http://globalserver.com/test (that is server A) to internal server address http://localserver.com. However, it does append the location path, that is, itries to look for http://localserver.com/test, which is not available at all. How can I make the proxy pass to the correct address, throwing out the last part in the location?

like image 320
SPIRiT_1984 Avatar asked Mar 12 '13 05:03

SPIRiT_1984


People also ask

Where is Nginx proxy conf?

Using Nginx as a Reverse Proxy On Ubuntu and Debian based distributions, server block files are stored in the /etc/nginx/sites-available directory, while on CentOS in /etc/nginx/conf.

What is Nginx proxy conf?

Configure NGINX as a reverse proxy for HTTP and other protocols, with support for modifying request headers and fine-tuned buffering of responses.

Where is Nginx file path?

Every NGINX configuration file will be found in the /etc/nginx/ directory, with the main configuration file located in /etc/nginx/nginx. conf .


1 Answers

That should work. Nginx should strip the '/test' path on the upstream local server. So what I can say is that is not the cause. To make it a bit better, try this:

location /test/ {   proxy_pass http://localserver.com/; } 

The 2 slashes I added at the first 2 lines will avoid mistakenly match '/testABC' and send the wrong request to the upstream local server, for example.

Do you have a

proxy_redirect 

line in the same location block? If your upstream local server has redirects, then a mistake on that line will cause an issue like you described.

[UPDATE] Found the root cause why the original config didn't work and mine works: nginx does NOT replace URI path part if the proxy_pass directive does not have a URI path itself. So my fix of adding a slash (slash is treated as a URI path) at the end triggers the URI path replacement.

Reference: http://wiki.nginx.org/HttpProxyModule#proxy_pass

If it is necessary to transmit URI in the unprocessed form then directive proxy_pass should be used without URI part

location  /some/path/ {   proxy_pass   http://127.0.0.1; } 
like image 97
Chuan Ma Avatar answered Sep 22 '22 21:09

Chuan Ma