Nginx will only show the 404 error if the code itself returns 404. Does it?
Mar 16, 2016
5
Level 4
Custom 404 page - NGINX
Hello!
I am having trouble getting a 404 page to show, any incorrect URL simply loads the index.php home page. I am using Perch CMS on Forge / DO. Perch isn't doing any strange routing stuff since it only serves from php files without a MVC framework or the like.
Here is my NGINX configuration for the site:
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/theoldgeorgeickleford.com/before/*;
server {
listen 80;
server_name theoldgeorgeickleford.com;
root /home/forge/theoldgeorgeickleford.com/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/theoldgeorgeickleford.com/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/theoldgeorgeickleford.com-error.log error;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
try_files $uri /index.php?$query_string;
expires -1;
access_log /var/log/nginx/example.com-access.log;
}
location ~* \.(?:rss|atom)$ {
try_files $uri /index.php?$query_string;
expires 1h;
add_header Cache-Control "public";
}
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
try_files $uri /index.php?$query_string;
expires 1M;
add_header Cache-Control "public";
}
location ~* \.(?:css|js)$ {
try_files $uri /index.php?$query_string;
expires 10m;
add_header Cache-Control "public";
}
error_page 404 /404.php;
location = /404.php {
root /home/forge/theoldgeorgeickleford.com/public;
internal;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/theoldgeorgeickleford.com/after/*;
As you can see I have declared the 404 error page near the bottom. I tested the nginx config and restarted it, but accessing a page that doesn't exist still returns the site's index.php homepage.
Any ideas? Thanks
Please or to participate in this conversation.