nginx配置默认错误页面 [英] nginx configure default error pages

查看:818
本文介绍了nginx配置默认错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首次使用,首次使用nginx的用户.因此,我仍在学习与Apache和nginx的区别.

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

我有一份nginx的库存安装. (apt-get install nginx-full)我为安装程序修改了在'/etc/nginx/sites-enabled/default'中找到的默认配置.但是,错误页面不起作用.我的docroot是/server/www,我的错误页面位于目录/server/errors/中.下面是我的配置.

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

所有应生成403或404页面的页面,只需加载index.html文件.我相信可能会发生500个错误,但是这很难生成.

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.

此外,如果相关,我将错误页面移至docroot上方,但在此之前它们不起作用.我之所以采取这一举措,是因为内部"标志似乎并没有像文档所声称的那样使页面内部化,因为我仍然可以直接访问它们.

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!

推荐答案

错误问题与位置/的最终值为/index.html有关.本质上,nginx首先尝试将uri作为文件,然后作为目录,然后还原为index.html.相反,我希望它返回404.因此我将其更改为以下内容,现在可以使用了.

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配置默认错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆