Nginx does redirect, not proxy

mitchkman picture mitchkman · Jul 2, 2014 · Viewed 56.2k times · Source

I want to set up Nginx as a reverse proxy for a https service, because we have a special usecase where we need to "un-https" a connection:

http://nginx_server:8080/myserver ==> https://mysecureservice

But what happens is that the actual https service isn't proxied. Nginx does redirect me to the actual service, so the URL in the browser changes. I want to interact with Nginx as it was the actual service, just without https.

This is what I have:

server {
    listen 0.0.0.0:8080 default_server;
    location /myserver {
        proxy_pass https://myserver/;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
    }
}

Answer

Tan Hong Tat picture Tan Hong Tat · Jul 2, 2014

You have to use the proxy_redirect to handle the redirection.

 Sets the text that should be changed in the “Location” and “Refresh” header fields of a 
 proxied server response. Suppose a proxied server returned the header field 
 “Location:https://myserver/uri/”. The directive
 will rewrite this string to “Location: http://nginx_server:8080/uri/”. 

Example:

 proxy_redirect https://myserver/ http://nginx_server:8080/;

Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect