nginx 配置默认错误页面

标签 nginx

首先,第一次使用 nginx 用户。所以,我仍在学习 Apache 和 nginx 的区别。

我有一个 nginx 的库存安装。 (apt-get install nginx-full)我修改了在“/etc/nginx/sites-enabled/default”中找到的默认配置。然而,错误页面根本不起作用。我的文档根目录是/server/www,我的错误页面位于目录/server/errors/中。下面是我的配置。

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 个错误,但是生成起来有点困难。

此外,如果相关的话,我将错误页面移至 docroot 上方,但在此之前它们无法正常工作。我之所以采取这一行动,是因为“内部”标志似乎并未像文档声称的那样使页面成为内部页面,因为我仍然可以直接访问它们。

您能提供的任何建议/帮助都会很棒!

最佳答案

该错误问题与位置/结束值/index.html 有关。本质上,nginx 首先尝试将 uri 作为文件,然后作为目录,然后恢复为 index.html。相反,我希望它返回 404。所以我将其更改为下面的内容,现在它可以工作了。

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

关于nginx 配置默认错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22162274/

相关文章:

django - 如何在 Nginx 和 uWSGI 上运行多个 Django 站点?

amazon-web-services - 使用 Amazon S3 配置 Nginx 并使用请求的 url 的位置从 S3 获取 html

nginx: "/root/index.html"禁止 (13: 权限被拒绝)

authentication - Nginx 身份验证请求意外状态 404

nginx - 外部 http 连接失败(端口打开)

PHP-FPM 和 Nginx : 502 Bad Gateway

php - 使用 Docker 容器部署 Laravel

php - php5-fpm 错误

django - 长时间运行请求的 nginx 502 错误网关

docker Nginx : host not found in upstream