I recently updated my configuration because I was missing a .com
in all my redirects (whoops), so I updated my config file and ran sudo systemctl restart nginx
, saw it was still redirecting to the bad domain, ran sudo reboot
, and saw no changes still. I then waited over an hour for DNS to propagate in case that were the issue, but it wasn't.
Old config
server {
server_name domainA.com www.domainA.com
domainB.com www.domainB.com;
if ($host = www.domainB.com) {
return 301 https://domainA$request_uri;
} # managed by Certbot
if ($host = domainB.com) {
return 301 https://domainA$request_uri;
} # managed by Certbot
if ($host = www.domainA.com) {
return 301 https://domainA$request_uri;
} # managed by Certbot
location = /favicon.png { access_log off; log_not_found off; }
location /media {
alias /home/matt/project-dir/media;
}
location /static {
alias /home/matt/project-dir/static;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domainB.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domainB.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.domainB.com) {
return 301 https://domainA$request_uri;
} # managed by Certbot
if ($host = domainB.com) {
return 301 https://domainA$request_uri;
} # managed by Certbot
if ($host = www.domainA.com) {
return 301 https://domainA$request_uri;
} # managed by Certbot
if ($host = domainA.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name domainA.com www.domainA.com
domainB.com www.domainB.com;
listen 80;
return 404; # managed by Certbot
}
New config
Only difference: return 301 https://domainA$request_uri;
→ return 301 https://domainA.com$request_uri;
repeated six times
server {
server_name domainA.com www.domainA.com
domainB.com www.domainB.com;
if ($host = www.domainB.com) {
return 301 https://domainA.com$request_uri;
} # managed by Certbot
if ($host = domainB.com) {
return 301 https://domainA.com$request_uri;
} # managed by Certbot
if ($host = www.domainA.com) {
return 301 https://domainA.com$request_uri;
} # managed by Certbot
location = /favicon.png { access_log off; log_not_found off; }
location /media {
alias /home/matt/project-dir/media;
}
location /static {
alias /home/matt/project-dir/static;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domainB.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domainB.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.domainB.com) {
return 301 https://domainA.com$request_uri;
} # managed by Certbot
if ($host = domainB.com) {
return 301 https://domainA.com$request_uri;
} # managed by Certbot
if ($host = www.domainA.com) {
return 301 https://domainA.com$request_uri;
} # managed by Certbot
if ($host = domainA.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name domainA.com www.domainA.com
domainB.com www.domainB.com;
listen 80;
return 404; # managed by Certbot
}
Are there any changes I can make that will actually be reflected?
Edit 1: Add /etc/nginx
directory listing
# tree /etc/nginx
/etc/nginx
├── conf.d
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules-available
├── modules-enabled
│ ├── 50-mod-http-auth-pam.conf -> /usr/share/nginx/modules-available/mod-http-auth-pam.conf
│ ├── 50-mod-http-dav-ext.conf -> /usr/share/nginx/modules-available/mod-http-dav-ext.conf
│ ├── 50-mod-http-echo.conf -> /usr/share/nginx/modules-available/mod-http-echo.conf
│ ├── 50-mod-http-geoip.conf -> /usr/share/nginx/modules-available/mod-http-geoip.conf
│ ├── 50-mod-http-image-filter.conf -> /usr/share/nginx/modules-available/mod-http-image-filter.conf
│ ├── 50-mod-http-subs-filter.conf -> /usr/share/nginx/modules-available/mod-http-subs-filter.conf
│ ├── 50-mod-http-upstream-fair.conf -> /usr/share/nginx/modules-available/mod-http-upstream-fair.conf
│ ├── 50-mod-http-xslt-filter.conf -> /usr/share/nginx/modules-available/mod-http-xslt-filter.conf
│ ├── 50-mod-mail.conf -> /usr/share/nginx/modules-available/mod-mail.conf
│ └── 50-mod-stream.conf -> /usr/share/nginx/modules-available/mod-stream.conf
├── nginx.conf
├── proxy_params
├── scgi_params
├── sites-available
│ ├── production
│ └── staging
├── sites-enabled
│ └── production -> /etc/nginx/sites-available/production
├── snippets
│ ├── fastcgi-php.conf
│ └── snakeoil.conf
├── uwsgi_params
└── win-utf
ls
in the /etc/nginx directory so we can see a list of all your config files. I've sometimes seen multiple inclusion files where you change one file, but it turns out that a completely different included file was the source of the problem.tree
, but if you wantls
options like-a
I can post that, too. I just thought it might be useful to include subdirectories, namelysites-available
andsites-enabled
.