Nginx virtual host only serves static file on www variant of domain

Nginx virtual host only serves static file on www variant of domain

I have a Nginx virtual host configured for my domain.co.nz and www.domain.co.nz website on my Ubuntu 18.04 VPS. The file for the virtual host sits within the /etc/nginx/sites-enabled directory.

It correctly serves the website with a root of /var/www/domain.co.nz/html and index of index.html for both the www and non-www variants of the website.

In the same directory (the root) as the index.html file there is a PDF file called file.pdf.

The file shows on www.domain.co.nz/file.pdf, the www variant of the domain, but when you navigate to the same URL but without the www subdomain, the file doesn't show and instead the index is served. It's not a redirect as the file name stills shows in the URL.

Please find my Nginx config for the virtual host below:

server {
        server_name domain.co.nz www.domain.co.nz;
        root /var/www/domain.co.nz/html;
        index index.html;

        location / {
                try_files $uri $uri/ /index.html;
        }

        location ~ /.well-known {
                allow all;
        }


        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/domain.co.nz/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/domain.co.nz/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 = domain.co.nz) {
                return 301 https://$host$request_uri;
        } # managed by Certbot


        server_name domain.co.nz;
        listen 80;
        return 404; # managed by Certbot
}

The second server block was automatically generated by Certbot.

I'm a bit stumped as to what might be causing this.

I've validated my config using sudo nginx -t and there are no errors or warnings.

答案1

So it looks to have been a caching issue as the PDF file is now showing for both www and non-www versions of the URL. I'm unsure however if this is a browser cache or a server-side cache.

相关内容