linux - Nginx 没有重定向到 404 页面

标签 linux nginx http-status-code-404

尝试使用以下代码,但网站仅重定向回默认页面,而不是 404 错误页面

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

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

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /404.html;
                allow 192.168.1.127;
                allow 127.0.0.1;
                deny all;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;
        #}

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page 500 502 503 504 /50x.html;
        #location = /50x.html {
        #       root /usr/share/nginx/html;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
        #       fastcgi_index index.php;
        #       include fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

对于服务器来说是全新的,非常感谢所有帮助。另外,我正在遵循本指南:http://arstechnica.com/gadgets/2012/11/how-to-set-up-a-safe-and-secure-web-server/

最佳答案

首先我建议更改index规则,在开头调出index.php,现在你的配置中没有任何php处理引擎,你的服务器上安装了fastcgi或fpm吗?

改变

index index.html index.htm index.php;

index index.php index.html;

确定你拥有哪个 php 引擎后,我可以告诉你取消注释哪些行,现在没有 php 引擎,nginx 应该将 php 作为普通文件进行服务器,要么在浏览器上打印它的数据,要么提示你下载 php 文件。

关于linux - Nginx 没有重定向到 404 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17228820/

相关文章:

ruby-on-rails - 服务器重启后如何重启 RoR 服务

nginx - http2模块nginx不工作

ruby-on-rails - Rails上的自定义错误页面

mysql - 如何将值直接插入到zabbix Mysql数据库中

linux - Unix shell 脚本错误

regex - Nginx 对变量的字符串操作

xml - 当 Spring MVC 项目在 Eclipse 上运行时,Tomcat 试图打开 xml 文件

.htaccess - 重新设计网站 (SEO) 后出现多个 301 重定向

node.js - Node JS Object.assign 方法不存在

linux - 了解 vi 编辑器的内部结构