Question
Redirect non www to www on https
I am trying to redirect https:// to https://www. Here is the block of code I am using:
server {
# root /home/user/www;
index index.html index.htm index.nginx-debian.html;
server_name ookma-kyi.tech www.ookma-kyi.tech;
location / {
root /home/user/www;
try_files $uri $uri/ =404;
}
location /demo {
root /home/user/app;
rewrite /demo/(.*) /$1 break;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.ookma-kyi.tech) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = ookma-kyi.tech) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name ookma-kyi.tech www.ookma-kyi.tech;
return 404; # managed by Certbot
}
I tried adding a server block at the top with a 301 redirect like so:
server {
listen 443;
server_name ookma-kyi.tech
ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/privkey.pem; # managed by Certbot
return 301 https://www.ookma-kyi.tech$request_uri;
}
server {
# root /home/user/www;
index index.html index.htm index.nginx-debian.html;
server_name ookma-kyi.tech www.ookma-kyi.tech;
location / {
root /home/user/www;
try_files $uri $uri/ =404;
}
location /demo {
root /home/user/app;
rewrite /demo/(.*) /$1 break;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.ookma-kyi.tech) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = ookma-kyi.tech) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name ookma-kyi.tech www.ookma-kyi.tech;
return 404; # managed by Certbot
}
All that did was show the Nginx welcome page. Any ideas?
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×