这是我当前的服务器块(如下)。我在 /blog 上安装了一个单独的 wordpress 博客,需要将 /blog 路由到目录“/home/forge/example.com/public/blog”。
我尝试了几种方法,但都没有成功,因此非常感谢大家的建议。
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/before/*;
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
root /home/forge/example.com/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/example.com/server.crt;
ssl_certificate_key
/etc/nginx/ssl/example.com/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'REMOVED FOR DEMO';
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/after/*;
以下是我尝试添加的内容:
location /blog/ {
root /home/forge/example.com/public/blog;
try_files $uri $uri/ /index.php?$query_string;
}
# the images need a seperate entry as we dont want to concatenate that with index.php
location ~ /blog/.+\.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
root /home/forge/example.com/public/blog;
}
# pass the PHP scripts to FastCGI server
location ~ /blog/.+\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
allow 127.0.0.1;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
答案1
以下是可能有效的代码片段(我没有 Forge 来测试它)...
location /blog {
try_files $uri $uri/ /blog/index.php$args$query_string;
location ~ .+\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
}
# the following location is not needed - so commented out.
# location ^~ /blog/.+\.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
# expires max;
# }
回到为什么你的配置不起作用的问题......
当你提到这样的事情时......
location /blog {
root /home/forge/example.com/public/blog;
# ... other configuration lines
}
Nginx 将在名为“/home/forge/example.com/public/blog/blog”的文件夹中查找“index.php”,这将导致错误(如果为空,则返回 404 或 403 - 尝试创建这样的文件夹并尝试在其中设置 index.php 文件<?php phpinfo();
)。我已经嵌套了 PHP 块。可以像这样将它们解除嵌套...
location /blog {
try_files $uri $uri/ /index.php$args$query_string;
}
# pass the PHP scripts to FastCGI server
location ^~ /blog/.+\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
allow 127.0.0.1;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
请注意第二个位置块中的修饰符的变化^~
。如果没有它,主站点 (Laravel) 的 PHP 位置块可能会优先,从而带来不良结果。有关位置块如何工作以及优先顺序的更多信息,请访问官方文档。
对于子目录安装,静态文件应该正常工作,而不需要单独的位置块。