简体   繁体   中英

How to reverse proxy two times with nginx?

I have this configuration on nginx server_1:

upstream server_2 {
 server 127.0.0.1:8080;
}

server {
 listen 80;
 server_name *.mydomain.com;
 proxy_set_header X-Forwarded-Host $host;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_set_header X-Forwarded-Proto $scheme;
 proxy_set_header X-Real-IP $remote_addr;

 location / {
   proxy_redirect off;
   proxy_pass http://server_2;
 }
}

And this config on nginx server_2

proxy_set_header X-Forwarded-Host $host;
proxy_set_header Host $host;

server {
    listen 8080;
    server_name ~^subdomain\.mydomain\.com$;
    location / { proxy_pass http://127.0.0.1:2000; }
}

Now redirects are not working, for example

if /url should be redirected to /otherurl and the request was http://subdomain.mydomain.com/url the redirect url returned to the browser is http://server_1/otherurl instead of http://subdomain.mydomain.com/otherurl

Found a solution here .

Just remove proxy_redirect off , and add the following line to the location block

proxy_set_header HOST $host;

It will become

 location / {
   proxy_pass http://server_2;
   proxy_set_header HOST $host;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM