Quique1222
Quique1222

Reputation: 13

Nginx Reverse proxy not redirecting to HTTPS

So i have a Nginx server working as a reverse proxy with this configuration

http {
    server {
    listen 80;
    listen [::]:80;
    server_name www.example.tdl;
    access_log logs/reverse-access.log;
    error_log logs/reverse-error.log;

        location / {
                    proxy_pass https://127.0.0.1:443;
        }
    }

}

My only problem is that when i connect to example.tdl it redirects correctly, but not with HTTPS. But its redirecting to the https port of my Apache server so i dont know where is the problem. I never used nginx before so this is probably a configuration error.

How can i make that nginx redirects to that port with https?

Upvotes: 0

Views: 1440

Answers (1)

anemyte
anemyte

Reputation: 20296

This may sound dumb but what you've got is that nginx is working as a proxy for you. You connect to nginx via HTTP, nginx forwards your request to apache via HTTPS, then gets the answer back and transmit it to you using HTTP.

You need to make nginx listen for HTTPS with listen 443 ssl; and supply it with a certificate (ssl_certificate <path>;) and a key (ssl_certificate_key <path>;). After that add the following piece of code to the server block:

if ($scheme != "https") {
    return 301 https://$host$request_uri;
}

This will redirect everything to HTTPS.

Upvotes: 1

Related Questions