这是我目前的服务器块(下面)。 我有一个单独的WordPress博客安装在/博客,需要路由/博客目录“/ 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; }
这是可能工作的片段(我没有Forgetesting它)…
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; # }
回到为什么你的configuration不工作…
当你提到这样的事情…
location /blog { root /home/forge/example.com/public/blog; # ... other configuration lines }
Nginx将在名为“/home/forge/example.com/public/blog/blog”的文件夹中寻找“index.php”,这将导致错误(404或403,如果它是空的 – 尝试创build文件夹和尝试使用<?php phpinfo();在其中设置index.php文件)。 我嵌套了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位置块可能优先,会带来不希望的结果。 有关位置块的工作原理和优先顺序的详细信息,请参阅官方文档 。
对于子目录安装,静态文件应该正常工作,不需要单独的位置块。