Nginx Does Redirect, Not Proxy

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:

==>

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 
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
    }
}

4 Answers

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: The directive
 will rewrite this string to “Location:  

Example:

 proxy_redirect  

Source:

9

You can setup nginx like this if you do not want the server to do redirects:

server
{
    listen 80;
    server_name YOUR.OWN.DOMAIN.URL;
    location / {
        proxy_pass 
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

For me, this config was sufficient:

events {
}

http {
    server {
        location / {
            resolver 8.8.8.8;
            proxy_pass 
        }
    }
}

(Note that the resolver directive has nothing to do with the problem in the OP, I just needed it to be able to proxy an external domain such as example.com)

The problem for me was just that I was missing the www. in . In the Firefox developer's console, I could see the GET request to localhost coming back with a 301, and so I thought that NGINX was issuing 301s instead of just mirroring example.com. Not so: in fact the problem was that example.com was returning 301s to redirect to , NGINX was dutifully mirroring those 301s, and then Firefox "changed the URL" (followed the redirect) straight from localhost to .

2

I was having a similar issue. In my case, I was able to resolve the issue by added a trailing slash to the proxy_pass URL:

before

server {
  location / {
    proxy_pass 
  }
}

after

server {
  location / {
    # added trailing slash
    proxy_pass 
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest