I tried many solutions but nothing worked. I am trying to redirect https://example.com to https://www.example.com. If I write https://example.com in the address bar, I get an insecure website prompt. I have ssl certificate installed for www.example.com. Here's the code from nginx server:
server {
server_name exmaple.com www.exmaple.com;
location ~ ^/.well-known {
root /home/xyz/xyz-dir;
allow all;
}
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/xyz/xyz-dir;
}
location /media/ {
root /home/xyz/xyz-dir;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.exmaple.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.exmaple.com/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.exmaple.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = exmaple.com) {
return 301 https://www.$host$request_uri;
} # managed by Certbot
listen 80;
server_name exmaple.com www.exmaple.com;
return 404; # managed by Certbot
}
use
location ~instead oflocation /as you commented it's not work: updated
use bellow links:
https://nginx.viraptor.info/
https://nginxconfig.org/
as you can see here
although browsers redirect http to https, you should add:
maybe your problem will solved by this:
https://stackoverflow.com/a/54859776/5987259