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?


3 Answers 3


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:


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! :)

  • 1
    Thanks for your response - the link was indeed very helpful. I think I've solved my problem by splitting it up - I configured a redirect for all HTTP requests on port 80 to HTTPS on port 443. I then configured a new server in the config for 443. I was previously trying to do this all same place.
    – Mike
    May 26, 2010 at 9:58

I'm using the following config in production

server {
    listen xxx.xxx.xxx.xxx:80;
    server_name www.example.net;

    rewrite ^(.*) https://$server_name$1 permanent;

server {
    listen xxx.xxx.xxx.xxx:443;
    server_name www.example.net;

    root   /vhosts/www.example.net;

    ssl                  on;
    ssl_certificate      /etc/pki/nginx/www.example.net.crt;
    ssl_certificate_key  /etc/pki/nginx/www.example.net.key;

    ssl_prefer_server_ciphers on;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
    ssl_dhparam /etc/pki/nginx/dh2048.pem;

    # intermediate configuration. tweak to your needs.
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    location / {
        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;
  • 1
    +1 Adding proxy_set_header X-Forwarded-Proto https; did the trick for me. Nov 2, 2018 at 10:49
  • 2
    For newer versions of nginx you might want to remove the line ssl on
    – Constantin
    Sep 15, 2021 at 14:57
  • @Constantin why
    – Tobi
    Nov 18, 2021 at 18:48
server {

    listen 80;
    server_name www.example.net example.net;

   rewrite ^/(.*)$ https://$host$request_uri? permanent; 

server {
  listen 443;
  server_name www.example.net example.net;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .