0

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
4
  • Where is this config file located? Please post 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.
    – David W
    Oct 17, 2020 at 11:17
  • I posted tree, but if you want ls options like -a I can post that, too. I just thought it might be useful to include subdirectories, namely sites-available and sites-enabled. Oct 17, 2020 at 11:26
  • What changes did you make to the DNS? Oct 17, 2020 at 12:16
  • I updated some A records. They were pointing to the wrong IP, so I changed the values in domain B to match domain A. This was after transferring data from one VPS instance to a new one. It's possible I could've skipped a step; looking back, I was a bit too hasty with this migration. Oct 17, 2020 at 12:20

2 Answers 2

0

I just finished chatting with a Namecheap rep. It turned out to be DNS not propagating yet, which I incorrectly ruled out earlier. As you may likely know already, it can take up to 24, even 48 hours. Speaking with someone who had not recently connected to my site allowed me to get things running as they should.

0

I can't see any relation between the DNS and the Nginx redirection issue. Nevertheless, when you are debugging redirections, try not to use a desktop web browser like Firefox, Chrome, etc. as they use to cache redirections and fool you.

It's better to test the redirection using curl:

curl -I https://www.yoursite.com

If you don't have curl then you'd better close the web browser between each test.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .