我需要在单个域上拥有多个 PHP-FPM。
例如:services.local/notification -> notification:9000 services.local/auth -> auth:9000
因此,我有一个 vhost 配置:
server {
server_name services.local;
root /www;
include /etc/nginx/conf.d/services/*.conf;
error_log /var/log/nginx/symfony_error.log;
}
我有一个notification.conf
location /notification {
root /www/;
index app.php index.html;
#try_files $uri $uri /notification/web/app.php?$is_args$args;
rewrite ^(.*)$ /notification/web/app.php last;
}
location /notification/web/app.php {
fastcgi_pass notification-upstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /www/notification/web/app.php;
fastcgi_param HTTPS on;
}
问题是每次重写 app.php 都会导致下载其内容,而不是将其传递给 FPM。
当我直接访问 app.php 时,它会被传递给 PHP-FPM,唯一的问题是子目录 + PHP-FPM + 重写的组合
我想我试了两天,但还是没有其他办法。这个配置重建了 50 次 :)
答案1
最后我找到了解决方案。我不知道它是否适用于静态资产,可能不行,但这不是我的情况。请随意美化和简化此代码,因为我知道它看起来有点丑陋,但它运行完美。
location ~ /notification {
set $new_request_uri /;
if ($request_uri ~ ^/notification(.+)$) {
set $new_request_uri $1;
}
if ($request_uri ~ ^/notification\?(.+)$) {
set $new_request_uri /?$1;
}
set $new_document_uri /;
if ($document_uri ~ ^/notification(.+)$) {
set $new_document_uri $1;
}
fastcgi_pass wolnosciowiec_notification:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param REQUEST_URI $new_request_uri;
fastcgi_param DOCUMENT_URI $new_document_uri;
fastcgi_param SCRIPT_NAME /www/notification/web/app.php;
fastcgi_param SCRIPT_FILENAME /www/notification/web/app.php;
fastcgi_param DOCUMENT_ROOT /www/notification/web;
fastcgi_param HTTPS on;
}