M
M
Mr_Sinister2016-08-06 11:23:52
Nginx
Mr_Sinister, 2016-08-06 11:23:52

How to redirect from HTTP to HTTPS in Nginx?

I'm trying to redirect from HTTP to HTTPS. The actual config code:

server {
        listen  80;
        listen  443 ssl;

        ssl     on;
        ssl_certificate /home/certificate.pem;
        ssl_certificate_key     /home/key.pem;

        server_name     vk4.me;
        access_log      /var/log/nginx/nginx.vhost.access.log;
        error_log       /var/log/nginx/nginx.vhost.error.log;

        if ($scheme = "http"){
                rewrite ^/(.*)$ https://$host/$1 permanent;
        }

        location / {
                proxy_pass      http://127.0.0.1:8081;
                include /etc/nginx/proxy.conf;
        }
}

proxy.conf
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

When I go to the site under any of the protocols, an error flies
Sitename has redirected too many times.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nazar Mokrinsky, 2016-08-06
@nazarpc

Do not mix HTTP and HTTPS in the same block, nothing good will come of it.

server {
    listen      80;
    server_name website.com;
    return      301 https://$server_name$request_uri;
}

K
Konstantin Grachev, 2016-08-06
@Preemiere

stackoverflow.com/questions/3893839/how-do-i-redir...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question