Nginx没有重定向到404页面

时间:2013-06-21 06:16:43

标签: 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/

1 个答案:

答案 0 :(得分:0)

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

变化

index index.html index.htm index.php;

index index.php index.html;

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