nginx configure default error pages

2019-07-09 11:02发布

问题:

First off, first time nginx user. So, I'm still learning the differences from Apache and nginx.

I have a stock install of nginx. (apt-get install nginx-full) I modified the default configuration found at '/etc/nginx/sites-enabled/default' for my setup. However, error pages just don't work. My docroot is /server/www, and my error pages are located within the directory /server/errors/. Below is my config.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /server/www;
    index index.html;

    server_name website.com;

    location / {
        try_files $uri $uri/ /index.html;
    }

    error_page 403 404 405 /40x.html;
    location /40x.html {
        root /server/errors;
        internal;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /server/errors;
        internal;
    }

    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;
    }
}

All pages that should generate a 403 or 404 page, just load the index.html file. I believe the something is probably happening with 500 errors, however, that's a bit harder to generate.

Also, if it's relevant, I moved the error pages above docroot, but they weren't working before that. I made that move as the 'internal' flag didn't appear to make the pages internal as the documentation claimed, as I could still access them directly.

Any advise/help you could give would be great!

回答1:

The error issue was related to the location / having an end value of /index.html. Essentially, nginx was first trying the uri as a file, then as a directory, and then reverting to index.html. Instead I wanted it to return a 404. So I changed it to the below and now it works.

location / {
    try_files $uri $uri/ =404;
}


标签: nginx