Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adammench's avatar

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

0 likes
5 replies
bashy's avatar

Nginx will only show the 404 error if the code itself returns 404. Does it?

adammench's avatar

No,

It is giving 200 as the status code.

Thanks, I will see what I can do to change the headers...

adammench's avatar

Can't seem to understand why ANY url returns the homepage.

The site works fine for all existing pages, but non-existing url's just return to index.php.

Looking at my nginx config - it is the standard Forge setup, nothing seems strange to me. Should I set the header in PHP somehow?

bashy's avatar

This is not really anything to do with nginx itself. It's passing a 200 response as it should (from PHP). If PHP sent a 404, it would read the 404 error page and return that.

adammench's avatar

Thanks @bashy it seems php never sends a 404... I guess my issue is that exactly. All sites on this server don't throw 404's, they simply redirect to the root page of that site whenever I try an invalid page url.

Please or to participate in this conversation.