Nginx – Proxy HTTPS requests to a HTTP backend with NGINX

httpsnginxreverse-proxy

I have nginx configured to be my externally visible webserver which talks to a backend over HTTP.

The scenario I want to achieve is:

  1. Client makes HTTP request to nginx which is redirect to the same URL but over HTTPS
  2. nginx proxies request over HTTP to the backend
  3. nginx receives response from backend over HTTP.
  4. nginx passes this back to the client over HTTPS

My current config (where backend is configured correctly) is:

server {
        listen       80;
        server_name  localhost;

        location ~ .* {
            proxy_pass http://backend;
            proxy_redirect http://backend https://$host;
            proxy_set_header Host $host;
            }
        }

My problem is the response to the client (step 4) is sent over HTTP not HTTPS. Any ideas?

Best Answer

The type of proxy you are trying to set up is called a reverse proxy. A quick search for reverse proxy nginx got me this page:

http://intranation.com/entries/2008/09/using-nginx-reverse-proxy/

In addition to adding some useful features like an X-Forwarded-For header (which will give your app visibility into the actual source IP), it specifically does:

proxy_redirect off

Good luck! :)