我正在尝试使用php代码向站点的/ blog子目录提供请求,该代码位于文档根目录之外的文件夹中 . 这是我的主机配置:
server {
server_name local.test.ru;
root /home/alex/www/test2;
location /blog {
alias /home/alex/www/test1;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
fastcgi_split_path_info ^(/blog)(/.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}
}
我得到的请求就像
wget -O - http://local.test.ru/blog/nonExisting
只是来自/ home / alex / www / test2 /文件夹的index.php文件的代码 .
但是,这个配置:
server {
server_name local.test.ru;
root /home/alex/www/test2;
location /blog {
alias /home/alex/www/test1;
try_files $uri $uri/ /blog$is_args$args;
index index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(/blog)(/.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}
}
从/ home / alex / www / test2 /给我index.html文件 . 请给我一个线索 - 为什么?我怎样才能强制NGINX处理/home/alex/www/test1/index.php呢?
3 回答
这是a long standing bug in nginx . 但是你可以再次使用
root
指令来解决 . 有点黑客,但至少它有效 .我们无法通过在位置块中指定root来使其工作 . 我们的解决方案是使用别名 . 请注意,必须在try_files指令中重复两次位置的路径,然后在.php配置块中重复:
来源:nginx/conf.d/app.conf来自debian-php-nginx stack the docker-stack project
还有另一种解决方法可以提供更大的灵活性 . 它由指向127.0.0.1的
proxy_pass
指令和另一个server
块组成 .在你的情况下,它应该是这样的: