By josephnilo
I am getting duplicate URLs on crawls of my Django site via my SEO tools and via Google tools. I want to ditch www.domain.com completely and redirect all traffic to https://domain.com
Below is my NGINX conf. Cerbot has added its https info. Curious what I’m doing wrong here.
server {
server_name domain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/domain/domain-django;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.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 {
server_name www.domain.com;
return 301 $scheme://domain.com$request_uri;
}
server {
if ($host = domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name domain.com;
return 404; # managed by Certbotserver
}
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there,
Currently, you don’t have a redirect rule for https://www.domian.com to https://domian.com. The redirects that you have are only for http://.
You can fix that by adding that redirect.
Here is an example:
server {
listen 80;
server_name www.yourdomain.com yourdomain.com;
return 301 https://yourdomain.com$request_uri;
}
server {
listen 443 ssl;
server_name www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.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
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
return 301 https://yourdomain.com$request_uri;
}
server {
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/domain/domain-django;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.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
}
Hope that this helps!
Best,
Bobby
Heya,
If you want to ensure that all requests to www.domain.com are redirected to https://domain.com and that all non-secure (http) requests are also redirected to their secure (https) counterparts, you’ll need a few modifications to your Nginx configuration.
Let’s break down the issues and correct them:
www to non-www:Your configuration for this appears mostly correct. But for clarity, let’s ensure it handles both http and https:
server {
listen 80;
listen [::]:80;
listen 443 ssl;
server_name www.domain.com;
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
return 301 https://domain.com$request_uri;
}
http) to secure (https):Your configuration is using a conditional inside a server block. Using if in Nginx can be problematic in some contexts, so it’s generally avoided when there’s a more straightforward method. Instead, use separate server blocks to handle the redirects more explicitly.
server {
listen 80;
server_name domain.com;
return 301 https://domain.com$request_uri;
}
https:This looks okay. Just make sure that you are only listening on port 443 with SSL for the main domain.
server {
listen 443 ssl;
server_name domain.com;
# ... (rest of your configurations)
# SSL configurations
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
With these configurations in place, you should have:
www.domain.com traffic redirected to https://domain.com.http) traffic redirected to its secure (https) counterpart.After making these changes, don’t forget to test your Nginx configuration:
sudo nginx -t
If there are no errors, reload Nginx to apply the changes:
sudo systemctl reload nginx
Lastly, you might also want to consider setting the canonical meta tag in your HTML to further hint to search engines about the preferred version of the URL.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.