Ham*_*eni 6 php nginx nginx-location
我正在尝试配置nginx从2个不同的位置提供2个不同的PHP脚本.配置如下.
/home/hamed/laravel它public应该在其中提供目录. /home/hamed/www/blog.这是我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当尝试访问wordpress部分时example.com/blog仍然调用laravel installtion接管请求.
现在我尝试root在location块内替换指令alias,但无济于事.
根据本指南有index指令或try_files内部location触发器内部重定向,我怀疑导致此行为.
请有人帮我解决这个问题吗?
问题是它location ~ \.php$ { ... }负责处理您所有的 php 脚本,这些脚本分为两个不同的根。
一种方法是使用共同root的server容器和每个前缀位置块内执行内部重写。就像是:
location /blog {
rewrite ^(.*\.php)$ /www$1 last;
...
}
location / {
rewrite ^(.*\.php)$ /laravel/public$1 last;
...
}
location ~ \.php$ {
internal;
root /home/hamed;
...
}
Run Code Online (Sandbox Code Playgroud)
以上应该可以工作(但我没有用你的场景测试过)。
第二种方法是使用嵌套的位置块。该location ~ \.php$ { ... }块然后在每个应用程序的位置块复制。就像是:
location /blog {
root /home/hamed/www;
...
location ~ \.php$ {
...
}
}
location / {
root /home/hamed/laravel/public;
...
location ~ \.php$ {
...
}
}
Run Code Online (Sandbox Code Playgroud)
现在,一个已经过测试可以工作。
感谢@RichardSmith,我终于成功创建了正确的配置。这是最终的工作配置。我必须使用嵌套location块和反向正则表达式匹配的组合才能使其工作。
server {
listen 443 ssl;
server_name 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(.*)$ {
index index.php;
root /home/hamed/www/;
try_files $uri $uri/ /blog/index.php?do=$request_uri;
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;
}
}
location ~ ^((?!\/blog).)*$ { #this regex is to match anything but `/blog`
index index.php;
root /home/hamed/laravel/public;
try_files $uri $uri/ /index.php?$request_uri;
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;
}
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8376 次 |
| 最近记录: |