Raymii.org
Quis custodiet ipsos custodes?Home | About | All pages | Cluster Status | RSS Feed
NGINX: Proxy folders to different root
Published: 04-04-2013 | Author: Remy van Elst | Text only version of this article
❗ This post is over eleven years old. It may no longer be up to date. Opinions may have changed.
This tutorial shows you how to have NGINX use different folders as different upstream proxy's.
Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below. It means the world to me if you show your appreciation and you'll help pay the server costs:
GitHub Sponsorship
PCBWay referral link (You get $5, I get $20 after you've placed an order)
Digital Ocea referral link ($200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!)
By default, if you have a location
block which has a proxy pass, and the
location
block is a folder, for example /wiki
, the folder is sent back to
the proxied server:
location /nagios/ {
proxy_pass http://10.0.21.8:80/;
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-Forwarded-Proto https;
proxy_redirect off;
}
The above block will, when browsed, sent you to http://10.0.21.8/nagios/
,
because that is the nginx location. However, if you want it to go to
http://10.0.21.8/
you either have to rewrite or use the /
location.
The below example has the correct rewrite rule:
location /nagios/ {
rewrite ^/nagios(/.*)$ $1 break;
proxy_pass http://10.0.21.8:80/;
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-Forwarded-Proto https;
proxy_redirect off;
}
As you can see, this line:
rewrite ^/collectd(/.*)$ $1 break;
fixes the above problem, and sends you to http://10.0.21.8/
instead of
http://10.0.21.8/nagios
.