I’m using Gunicorn and Nginx on Ubuntu. I also installed Let’s Encrypt certificates for my example.com and www.example.com with option 2-Redirect to redirect all HTTP to HTTPS.
Now I have the issue that the redirection from www.example.com
to example.com
is not working properly - it seems to me this happens after I installed Let’s Encrypt certificates (but I’m not exactly sure).
When entering to browser, http://www.example.com/
is redirecting to https://www.example.com/
https://www.example.com/
stays https://www.example.com/
Here is the Nginx configuration:
server {
server_name example.com www.example.com;
location / {
include proxy_params;
proxy_pass http://unix:/run/exampleproject.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.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.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name example.com www.example.com;
listen 80;
return 404; # managed by Certbot
}
I’m really not Nginx expert so could you please suggest what to do - how to redirect all www.example.com to example.com and at the same time keep the functionality that HTTP is redirected to HTTPS ?
IMO, probably this part managed by Certbot is doing wrong - it’s just rewriting the whole $host
part when redirecting instead of leaving out www
:
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
Because this part is related to Gunicorn and I don’t see what could I do here but also I’m very far away of being Gunicorn expert to judge that :-)
location / {
include proxy_params;
proxy_pass http://unix:/run/exampleproject.sock;
}
Thank you !
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.
Hello,
Can you please tr to replace the following server block
with
and let me know how it goes?
Please remember to restart Nginx after you make the file changes.
Regards, KFSys