Answer the question
In order to leave comments, you need to log in
Nginx redirect from http to https?
Good afternoon
I want to use only https and I want to redirect from http to https
I do this
server {<br/>
listen *:80;<br/>
server_name example.com;<br/>
rewrite ^(.*)$ https://$server_name$1 permanent;<br/>
}<br/>
<br/>
server {<br/>
listen *:443;<br/>
...<br/>
}<br/>
Answer the question
In order to leave comments, you need to log in
and if so?
server {
listen *:80;
server_name example.com;
proxy_set_header Host example.com;
location / {
rewrite ^(.*)$ https://example.com$1 permanent;
}
}
server {
listen *:443 ssl;
server_name example.com;
proxy_set_header Host example.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
According to the official documentation , it is recommended to use the following construction:
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri; # enforce https
# rewrite ^(.*) https://www.example.com$uri permanent;
}
another good option:
if ($ssl_protocol = "") {
rewrite ^/(.*) https://$server_name/$1 permanent;
}
Damn, I started writing about a redirect in the code, and for some reason I remembered about .htaccess. It turned out ugly :)
In general, you need to look for where 443 stands next to http://
Just for the sake of experiment, I did such a redirect. Moreover, with the letter-for-letter given config, with the exception of server_names. Everything works perfectly. Apparently the problem is not in this part, but further, for example, in the description of proxying to Apache.
proxy_redirect off; didn't forget?
>400 Bad Request The plain HTTP request was sent to HTTPS port
I think it's just that the browser itself doesn't know to send an encrypted request. and sends encrypted.
Maybe you should try to make a redirect through the PHP
engine itself :
header('Location: https://.........../');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question