0

I'm trying to get a custom 404 page to show up on a specific server. The 404 error page is located at /www/example/html/404.php and the website root is /www/example/html/

Currently, any non-existant page yields this result https://i.sstatic.net/4ojd5.png, and does not seem to be loading my custom 404 page (which has some custom styles and menus). I've been struggling with this for a few days now with different .conf settings, my current nginx.conf is below. I am on Ubuntu 12, Nginx 1.1.19 and using php-fpm.

user       www-data;
worker_processes  5;
error_log  logs/error.log;
worker_rlimit_nofile 8192;

events {
    worker_connections  4096;
}

http {
    include    mime.types;
    include    fastcgi_params;
    index    index.php index.html index.htm;

    default_type application/octet-stream;
    log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log   logs/access.log  main;
    sendfile     on;
    tcp_nopush   on;
    server_names_hash_bucket_size 128;

    server {
            listen 80;

            server_name example.com;
            client_max_body_size 20M;
            root /www/example/html;

            access_log logs/example.access.log;

            location / {
                    index index.php index.html index.htm;
                    try_files $uri $uri/ @rewrite;
            }

            location @rewrite {
                    rewrite ^/(.*)$ /index.php?q=$1;
            }

            location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
                    access_log off;
                    expires max;
            }

            location /404.php {
                    internal;
            }

            location ~ \.php$ {
                    include fastcgi_params;
                    fastcgi_intercept_errors on;
                    error_page 404 /404.php;
                    fastcgi_pass   127.0.0.1:9000;
            }

            location ~ /\.ht {
                    deny  all;
            }
    }

2 Answers 2

1

I think you should do the following:

error_page 404 /www/example/html/404.php;

location /404.php {
  return 404;
}

What do your access and error logs show?

Second option. Define a custom 404 page for everything your "server" context:

server {
...

error_page 404 /www/example/html/404.php;

...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Most recent access log pastebin.com/CqjHWKvb - still didn't load the correct error page. I put the "error_page" code in the "PHP" block, should it be in the "/" block instead?
If all you need to do is to show an specific custom page on 404, do the following: server { ... error_page 404 = /www/example/html/404.php; ... } Whie trouble shooting please comment every single "location" block. Then try to access a non existent resource. You should see the custom error page. BTW, ensure the nginx daemon have the appropriate permissions on the "404.php" file.
0

Have you tried in the location ~ \.php$ block to use:

error_page 404 = /404.php;

Or

location ~ \.php$ {
    error_page 404 @fallback;
}

location @fallback {
    proxy_pass http://example.com/html/404.php
}

1 Comment

proxy_pass failed with error "proxy_pass" may not have URI part in location given by regular expression, or inside named location, or inside the "if" statement, or inside the "limit_except" block in /etc/nginx/nginx.conf:181"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.