Nginx doesn't redirect on https
6,761
Solution 1
Your port 80 server block is not the default. Try adding default_server
in your port 80 definition.
server {
listen 80 default_server;
....
}
Solution 2
Below is the tested and properly working configuration:
server {
listen 80; # Default listen port
server_name mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
ssl on ;
server_name mydomain.com;;
....
}
One more way to do this :
server {
listen 80; # Default listen port
if ( $host = domain.com ) { rewrite ^/(.*)$ https://domain.com/$1 permanent; }
server_name domain.com;
......
}
server {
listen 443;
ssl on;
server_name domain.com;
ssl_certificate /etc/nginx/conf.d/certificates/domain.com.crt;
ssl_certificate_key /etc/nginx/conf.d/certificates/domain.com.key;
.......
}
Solution 3
The main problem to my eyes is that your not using the server_name directive at all so nginx doesn't know what its rewriting. With the SSL bit removed:
server {
listen 80;
server_name *.mydomain.com mydomain.com
return 301 https://$host$request_uri;
}
server {
listen 443;
#include snippets/certloc.conf
server_name my.domain.com;
location / {
root /home/uploader/datadir/;
index index.html index.htm;
}
}
Related videos on Youtube
Author by
Denis
Updated on September 18, 2022Comments
-
Denis over 1 year
I want to redirect all requests to https. It works for main page now, so then I'm trying to get http://domain.com it redirects me to https. But then I go on sublink like http://domain.com/somematerials/presentation it never redirects me. And here is my config.
server { listen 80; return 301 https://$host$request_uri; } server { listen 443; server_name my.domain.com; ssl on; ssl_certificate /path/ssl.crt; ssl_certificate_key /path/server.key; location / { root /home/uploader/datadir/; index index.html index.htm; } }
-
jedifans over 7 yearsThe first one is the better one, due it's lack of
if
. See "Server Name (If)" section of nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls