【发布时间】:2018-03-20 23:08:29
【问题描述】:
我要疯了才能理解这个 nginx vhost 配置。我的问题是 /v2 位置,当它在 /v2 之外正常工作时,它不会将 php 内容发送到 php-fpm。谁能指出我的错误?
server {
listen 443 ssl;
include ssl.conf;
include hardening.conf;
server_name myapp.domain.com myapp;
ssl_certificate /etc/pki/tls/certs/myapp.domain.com.crt;
ssl_certificate_key /etc/pki/tls/private/myapp.domain.com.key;
access_log /var/log/nginx/myapp.domain.com-access.log main;
error_log /var/log/nginx/myapp.domain.com-error.log notice;
root /var/www/html/myapp.domain.com;
location ~ /\.ht {
deny all;
}
location ~ /v2 {
alias /var/www/html/myapp.domain.com/version-2/web;
try_files $uri index.php$is_args$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass localhost:9000;
fastcgi_index index.php;
include fastcgi_params.conf;
}
}
根据 cmets,我正在尝试嵌套位置解决方案,但是当我尝试 https://myapp.domain.com/v2/index.php 而文件系统上存在 /var/www/html/myapp.domain.com/version-2/web/index.php 时,我现在收到 404。同样正如给出的链接所解释的,我将我的位置从^ 修改为^~。知道有什么问题吗?
server {
listen 443 ssl;
include ssl.conf;
include hardening.conf;
server_name myapp.domain.com myapp;
ssl_certificate /etc/pki/tls/certs/myapp.domain.com.crt;
ssl_certificate_key /etc/pki/tls/private/myapp.domain.com.key;
access_log /var/log/nginx/myapp.domain.com-access.log main;
error_log /var/log/nginx/myapp.domain.com-error.log notice;
root /var/www/html/myapp.domain.com;
location ~ /\.ht {
deny all;
}
location ^~ /v2 {
alias /var/www/html/myapp.domain.com/version-2/web;
try_files $uri index.php$is_args$args;
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass localhost:9000;
fastcgi_index index.php;
include fastcgi_params.conf;
}
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass localhost:9000;
fastcgi_index index.php;
include fastcgi_params.conf;
}
}
【问题讨论】:
-
您有两个具有不同根目录的 PHP 应用程序。您将需要两个不同的
location块来处理.phpURI。考虑使用嵌套的location块 - 例如 this answer。 -
@RichardSmith 我用你的建议更新了帖子,但现在我得到了一些 404,如帖子中所述,你能再看看吗?
-
$document_root$fastcgi_script_name不适用于alias,请改用:$request_filename。此外,您需要为SCRIPT_FILENAME设置一个值。另外,由于this issue,我会避免同时使用try_files和alias。 -
确实我没有看到那个细节,用
$request_filename替换$document_root$fastcgi_script_name并用重写规则替换try_files也重新排列了php 位置中的一些东西使其工作。 -
@RichardSmith 非常感谢!我需要发布工作版本吗?