4

My Nginx server is not displaying my 404 page. Instead, whenever trying to access a non-existent page or directory, it merely serves my index(.php) in the root of my web folder (without the corresponding stylesheet).

Here's my own 'default' file under /etc/nginx/sites-available:

server {
listen 80;
listen [::]:80 ipv6only=on;
listen 443 ssl;
listen [::]:443 ipv6only=on ssl;

add_header Strict-Transport-Security max-age=15768000;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4';
ssl_prefer_server_ciphers on;

root /usr/share/nginx/html;
index index.html index.htm;

location / {
    index index.php;
}

if (!-e $request_filename) {
    rewrite ^.*$ /index.php last;
}

try_files $uri $uri/ =404;

error_page 403 404 405 /error/404.html;
error_page 500 501 502 503 504 /error/50x.html;

location ^~ /error/ {
    internal;
    root /usr/share/nginx/html;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
}

This is undoubtedly down to me being new to this and fiddling with it too much, so there are likely other problems here too (on that note, a tip as to how - once the rest is fixed - I can force HTTPS connections would be swell!). Help and constructive input appreciated, thanks!

1 Answer 1

4

you are rewriting to index.php if the file doesn't exist, so it never makes it to your try_files or errorpage...

if (!-e $request_filename) {
    rewrite ^.*$ /index.php last;
}

^ should be removed, unless you have a specific purpose for it

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

2 Comments

Excellent! Now only one minor issue: If the 404 is from a non-existent .PHP file it says "File not found." instead of serving the 404.html file specified. (It now correctly serves 404.html with every other incorrect request.)
And that did it too. Thanks ever so much guys!

Your Answer

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