By Matt Porter
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
}
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 @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
This comment has been deleted
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.