【问题标题】:nginx setting up variable upstream per virtual hostnginx为每个虚拟主机设置上游变量
【发布时间】:2016-08-29 18:53:54
【问题描述】:

我的目标是拥有可配置的上游,因此我可以使用每个项目/虚拟主机所需的任何 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"

如您所见,我的目标是选择上游和 DRY 原则时的模块化。

【问题讨论】:

  • 你想让fastcgi_pass php;指向每台服务器的不同上游吗?
  • @AlexeyTen 是的,这是我的意图。

标签: nginx docker


【解决方案1】:

如果每个版本的 PHP 的上游名称 (php) 应该相同,那么您必须将上游块移动到外部文件中并包含您需要的文件。

示例:

移动

upstream php {
    server php7-fpm-alpine:9000;
}

到文件/etc/nginx/upstream-php7.conf

并将该文件包含在您的 /etc/nginx/nginx-wp-common.conf 中

可选择创建不同名称的上游(如上游 php7 {...})并在 fastcgi_pass 中使用所需的名称

编辑:

另一种选择:

定义不同的上游块:

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;

【讨论】:

  • 我正在尝试为每个虚拟主机设置不同的上游。您提出的任何解决方案都无济于事。
  • 我已经编辑了我的答案。我认为这现在应该可以解决您的问题。
  • 我记得我必须将包含上游的文件重命名为 00-my-upstreams.conf 之类的名称,以确保始终使用经典的 /etc/nginx/sites-enabled 配置首先加载它们
  • @PerroVerd 加载顺序无关紧要。
猜你喜欢
  • 2019-01-25
  • 2023-03-15
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 2014-11-12
  • 2012-01-02
  • 2012-12-24
  • 2018-01-09
相关资源
最近更新 更多