Question

Custom Wordpress 404 page not loading, Nginx 404 page loading instead.

I’m not sure why, but my custom Wordpress 404 page will not load with an improper URL that should 404, instead, the default Nginx 404 page loads. All pages are working as expected, I just want a 404 to be my 404, not the default. I’ve tried changing my permalinks just to update them, no luck. I suspect it has to do with my sever block, any ideas?

server {
        # The below listen is moved / contained in a server block down below, controlled by CertBot.
        # Only ONE default_server is allowed.
        # listen 80 default_server;
        # listen [::]:80 default_server;

        root /var/www/porterbytes.com/html;
        index index.php index.html index.htm index.nginx-debian.html;

        server_name porterbytes.com www.porterbytes.com;

        location / {
            #try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?$args =404;
        }

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

        location ~ /\.ht {
            deny all;
        }

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires 7d;
            add_header Cache-Control "public, no-transform";
            log_not_found off;
        }


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


    if ($host = porterbytes.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80 default_server;
        listen [::]:80 default_server;

        server_name porterbytes.com www.porterbytes.com;
    return 404; # managed by Certbot




}

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

This comment has been deleted

Hi there @Porter,

I think that the problem is caused by this line in your config here:

        location / {
            #try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?$args =404;
        }

The =404 pice at the end tells nginx to handle the 404 pages rather than letting your WordPress handle that.

Try removing that the =404 part, run a config test with:

nginx -t

If you get Syntax ok restart Nginx and try again.

Let me know how it goes! Regards, Bobby