我正在尝试将 nginx 配置为从 2 个不同的位置提供 2 个不同的 php 脚本。配置如下。
- 我有一个位于
/home/hamed/laravel
的 Laravel 安装,它的public
目录应该在其中提供服务。 - 我在
/home/hamed/www/blog
中安装了 Wordpress。
这是我的 nginx
配置:
server {
listen 443 ssl;
server_name example.com www.example.com;
#root /home/hamed/laravel/public;
index index.html index.htm index.php;
ssl_certificate /root/hamed/ssl.crt;
ssl_certificate_key /root/hamed/ssl.key;
location /blog {
root /home/hamed/www/blog;
try_files $uri $uri/ /blog/index.php?do=$request_uri;
}
location / {
root /home/hamed/laravel/public;
try_files $uri $uri/ /index.php?$request_uri;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
问题是,当尝试通过调用 example.com/blog
访问 wordpress 部分时,laravel 安装仍然会接管请求。
现在我尝试用 alias
替换 location
block 中的 root
指令但无济于事。
根据 this guide在 location
中使用 index
指令或 try_files
会触发内部重定向,我怀疑这会导致此行为。
有人可以帮我解决这个问题吗?
最佳答案
问题是 location ~\.php$ { ... }
负责处理您所有的 php 脚本,这些脚本分为两个不同的根。
一种方法是为 server
容器使用一个通用的 root
并在每个前缀位置 block 中执行内部重写。像这样的东西:
location /blog {
rewrite ^(.*\.php)$ /www$1 last;
...
}
location / {
rewrite ^(.*\.php)$ /laravel/public$1 last;
...
}
location ~ \.php$ {
internal;
root /home/hamed;
...
}
以上应该可行(但我没有用你的场景测试它)。
第二种方法是使用嵌套的位置 block 。 location ~\.php$ { ... }
block 随后被复制到每个应用程序的位置 block 中。像这样的东西:
location /blog {
root /home/hamed/www;
...
location ~ \.php$ {
...
}
}
location / {
root /home/hamed/laravel/public;
...
location ~ \.php$ {
...
}
}
现在已经过测试可以正常工作。
关于php - 具有多个位置 block 的 nginx 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34082831/