【问题标题】:Dynamic root folder doesn't work for *.php nginx动态根文件夹不适用于 *.php nginx
【发布时间】:2015-01-19 03:48:08
【问题描述】:

当我尝试浏览http://exmaple.dev.local/test.html Nginx 时,会提供静态文件“test.html”,但是当我尝试“index.php”时,它并没有提供服务。 我做错了什么?

错误是 404“未指定输入文件”。日志说:

test location: ~ "\.php$"
using configuration "\.php$"
http cl:-1 max:1048576
rewrite phase: 3
post rewrite phase: 4
generic phase: 5
generic phase: 6
generic phase: 7
access phase: 8
access phase: 9
post access phase: 10
try files phase: 11
http init upstream, client timer: 0
epoll add event: fd:13 op:3 ev:80000005
http script copy: "/var/www/"
http script capture: ""
http script copy: "/webroot/"
http script copy: "/var/www/"
http script capture: ""
http script copy: "/webroot/"
http script copy: "QUERY_STRING"
fastcgi param: "QUERY_STRING: "
http script copy: "REQUEST_METHOD"
http script var: "GET"
fastcgi param: "REQUEST_METHOD: GET"
http script copy: "CONTENT_TYPE"
fastcgi param: "CONTENT_TYPE: "
http script copy: "CONTENT_LENGTH"
fastcgi param: "CONTENT_LENGTH: "
http script copy: "/var/www/"
http script capture: ""
http script copy: "/webroot/"
http script copy: "SCRIPT_FILENAME"
http script var: "/var/www//webroot/"
fastcgi param: "SCRIPT_FILENAME: /var/www//webroot/"
http script copy: "SCRIPT_NAME"
http script var: "/index.php"
fastcgi param: "SCRIPT_NAME: /index.php"

我的 nginx 配置是:

server {
listen 80;
server_name "~^(.*)\.dev\.local$";

#if directory doesn't exist
if (!-d /var/www/$1/webroot/) {
    rewrite . http://dev.local redirect;
}
root /var/www/$1/webroot/;
index index.php index.html index.htm;

autoindex on; 
rewrite_log on;
open_file_cache off;

#cake beauty URLs
try_files $uri $uri/ /index.php;
location ~* \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param ENVIRONMENT "bar";
}
}

【问题讨论】:

    标签: php nginx configuration


    【解决方案1】:

    不要在 root 指令中使用位置变量 ($1)。定义命名变量。

    server_name "~^(?<domain>.+)\.dev\.local$";
    ...
    root /var/www/$domain/webroot/;
    ...
    

    这里是正则表达式位置的问题,$1 变量被重置为空字符串。

    【讨论】:

    • 并非如此。这就是变量在 nginx 中的工作方式。它们在最后一刻被扩展。而且,顺便说一句,如果你碰巧有location ~* ^/(.*)\.php$,你会对$1 变量有什么期望?
    • 确实如此。再次感谢您=)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多