2013-11-03 86 views
2

在我的网站我只有两个页面“index.php”和“page.php”,无论我键入我重定向到本地主机,例如,如果我去localhost/randompage而不是重定向到404.html我去到本地主机(即的index.php)nginx不重定向404错误

这里是我的conf:

server { 
    listen  80; 
    server_name localhost; 
root html; 
index index.php; 

    location/{ 

     if ($request_uri !~* '\/|page\.php\?.*') { 
      return 404; 
     } 

     try_files $uri $uri/ /index.php; 
    } 

    error_page 404    /404.html; 
    location = /404.html { 
     root html; 
    } 

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

    location ~ \.php$ { 
    try_files $uri =404; 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include  fastcgi_params; 
    } 

} 

我应该怎么改?

+0

我没有看到块'location =/50x.html'和'location =/404.html'的任何用法,您正在定义已经在服务器范围中定义的同一个根,那么您有一个'if'和'try_files'为什么不能同时使用'try_files'语句,如果你解释你到底想要完成什么,我可以帮你优化配置文件 –

+0

@MohammadAbuShady感谢你的帮助,我只是想向{mydomain.com,mydomain.com/index.php,mydomain.com/page.php?var1 =&var2 =}以外的网址发送请求,返回404错误,然后显示404.html页面 –

回答

3

我有一个类似的问题。注释掉这一行固定它:

try_files $uri $uri/ /index.php; 

try_files检查是否给定的URL的作品。如果不是,它会尝试查看给定的URL是否作为目录。如果没有,它会重定向到/index.php。如果没有这一行,它将提供用error_page表示的404文件。

希望能为你工作。