nginx设置每个虚拟主机的上游variables
我的目标是具有可configuration的上游,所以我可以使用任何PHP版本我需要每个项目/虚拟主机。
我试过了:
upstream php { server php7-fpm-alpine:9000; } server { listen 80; server_name somesite.com; root /www/somesite.com; include /etc/nginx/nginx-wp-common.conf; }
nginx-wp-common.conf有fastcgi_pass php;
我的设置适用于1个站点,但是一旦我开始为其他域添加更多虚拟主机,nginx会抱怨:
duplicate upstream "php"
正如你所看到的,我的目标是select上游和干燥原则的模块化。
如果PHP的每个版本的上游名称(php)应该相同,则必须将上游块移到外部文件中,并包含所需的一个。
〔实施例:
移动
upstream php { server php7-fpm-alpine:9000; }
到一个文件/etc/nginx/upstream-php7.conf
并将该文件包含在/etc/nginx/nginx-wp-common.conf中
可选地创build不同名称的上游(如上游php7 {…}),并在fastcgi_pass中使用所需的上游
编辑:
另外一个select:
定义不同的上游块:
upstream php5 { server php5-fpm-alpine:9000; } upstream php7 { server php7-fpm-alpine:9000; }
修改您的服务器块,为不同的虚拟主机设置$ upstream的不同值
server { listen 80; server_name somesite.com; root /www/somesite.com; set $upstream php7; include /etc/nginx/nginx-wp-common.conf; } server { listen 80; server_name othersite.com; root /www/othersite.com; set $upstream php5; include /etc/nginx/nginx-wp-common.conf; }
修改nginx-wp-common.conf
fastcgi_pass $upstream;