2016-07-23 71 views
0

我有这个配置我的虚拟主机:为什么nginx conf中的其他位置返回404代码?

server { 
    listen 80; 
    root /var/www/home; 
    access_log /var/www/home/access.log; 
    error_log /var/www/home/error.log; 

    index index.php index.html index.htm; 

    server_name home; 

    location/{ 
     try_files $uri $uri/ /index.php?$args; #if doesn't exist, send it to index.php 
    } 

    error_page 404 /404.html; 
    error_page 500 502 503 504 /50x.html; 

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/run/php/php-fpm.sock; 

     fastcgi_index index.php; 
     include fastcgi_params; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     fastcgi_param PHP_VALUE "error_log=/var/www/home/php_errors.log"; 
    } 

    location ~* /Admin { 
     allow 127.0.0.1; 
     deny all; 
    } 
} 

当我试图访问网页/联系Nginx将返回404代码由PHP生成成功的HTML内容。当/ Admin删除位置时,一切都很顺利。

如何得到附加位置的问题?

回答

1

您应该阅读this document以了解各种位置块的优先顺序。

所以,你可以把你的正则表达式位置location ~ \.php$块以上,使其能够优先考虑,或将其更改为:

location ^~ /Admin { ... } 

这是一种前缀位置是优先于任何正则表达式的位置(其中它在文件中的顺序变得无关紧要)。

第二个问题是allow 127.0.0.1声明的目的。您是否期望在客户端127.0.0.1处执行.php文件,其前缀为/Admin

您的管理位置块不包含执行.php文件的代码。如果目的是执行.php文件与/Admin前缀,你可以尝试:

location ~ \.php$ { 
    try_files $uri =404; 
    fastcgi_pass unix:/run/php/php-fpm.sock; 

    include fastcgi_params; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    fastcgi_param PHP_VALUE "error_log=/var/www/home/php_errors.log"; 
} 

location ^~ /Admin { 
    allow 127.0.0.1; 
    deny all; 

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_pass unix:/run/php/php-fpm.sock; 

     include fastcgi_params; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    } 
} 

您可能需要使用include指令来共同声明移到一个单独的文件。

请参阅how nginx processes a request

相关问题