7
7
7761662019-01-21 15:33:28
Nginx
776166, 2019-01-21 15:33:28

How to properly redirect nginx from http to https?

At some point after upgrading nginx to nginx/1.10.3, the old constructs stopped working. The server gives a 400 error.
Now even the simplest constructions:

server {
        listen 80;
        server_name example.com;
        proxy_set_header Host example.com:443; #С этой строчкий или без неё. С портом или без порта.
        return 301 https://$host$request_uri; #C такой конструкцией
        #location / {rewrite ^(.*)$ https://example.com$1 permanent;} #Или с такой
}

does not work.
There is nothing in the regular error_log. If you enable debug, then there is this entry:
2019/01/21 15:24:46 [info] 16330#16330: *1061 client sent plain HTTP request to HTTPS port while reading client request headers, client: [ip], server: example.com, request: "GET / HTTP/1.1", host: "example.com"

The old design with a protocol check and a redirect in a single one server {}also does not work.

These configs are regular, are registered in all manuals and in all answers to similar questions.
What chromosome is wrong?

UPD: It seems that the problem appears because there are two certificates:
- example.com (for one domain)
- *.dev.example.com (wildcard for all subdomains of dev.example.com)

Issued via letsencrypt exactly for .dev.example. com, not even *.example.com.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2019-01-21
@776166

if ($scheme = http) {
rewrite ^ https://$http_host$request_uri? permanent;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question