【问题标题】:nginx upstream config in a separate filenginx 上游配置在一个单独的文件中
【发布时间】:2017-03-04 00:41:59
【问题描述】:

我在 10.0.0.1 的 nginx 上有一个简单的配置文件:

default.conf

server {
    listen       80;
    server_name  server.com;

    location / {
        root /www;
        index index.html;
    }

我还想将请求重定向到http://10.0.0.1/app1 到 3 个在端口 8888 上具有相同应用程序的服务器,例如:

http://10.0.0.1/app1 -> http://10.0.0.(2,3,4):8888/app1

所以我必须在我的 default.conf 中添加这样的配置来平衡:

upstream app1 {
   server 10.0.0.2:8888;
   server 10.0.0.3:8888;
   server 10.0.0.4:8888;
}

server {
   listen 80;

   location /app1/ {
      rewrite ^/app1^/ /$1 break;
      proxy_pass http://app1;
   }
}

但我想将此平衡配置保存在单独的文件 - app1.conf 中。

如果我在 /etc/nginx/conf.d/ 文件夹中有这两个配置文件,我只能打开 URL http://10.0.0.1/

但是当我打开http://10.0.0.1/app1 时,我收到错误 404,因为 default.conf 它尝试在 /www 中查找 app1,甚至不尝试检查 app1.conf 的平衡规则。 所以它似乎只适用于 default.conf 配置文件。 怎么解决?

【问题讨论】:

  • 我会说原因是您有两个具有相同侦听端口的服务器块。它与两个配置文件/目录无关。
  • 感谢您的建议。但似乎我不能在服务器块之外使用位置指令,如果我在 app1.conf 中更改端口号 - nginx 开始监听不符合我条件的端口 - 我需要打开 URL“10.0.0.1/app1”

标签: nginx


【解决方案1】:

upsteam 部分无论如何都需要在 http 块中,它位于您的 nginx.conf/default.conf 中。
对于您可能可以使用的位置块:

default.conf

http {
...
    upstream app1 {
       server 10.0.0.2:8888;
       server 10.0.0.3:8888;
       server 10.0.0.4:8888;
    }
...
server {
    listen       80;
    server_name  server.com;

    include /path/to/app1.conf;

    location / {
        root /www;
        index index.html;
    }
...
include /etc/nginx/conf.d/*;
...
}

app1.conf

location /app1/ {
  rewrite ^/app1^/ /$1 break;
  proxy_pass http://app1;
}

在 default.conf 中编辑include 的路径。

编辑:
实际上我在这里犯了一个错误。 nginx 的指令是分层的。在文档中,您可以找到可以使用哪个块的位置。 server 块需要在 http 块中。 location 块可以在 server 和 location 块中。
根据您所在的区块,您可以使用include 在该特定上下文中导入区块。
因此,在server 块中使用include,您可以包含特定于应用程序的location 块,但不能包含server 块。这是因为server 块只能驻留在http 块中。
我希望这有助于澄清你的情况。

编辑2:
从您的评论中,我刚刚看到重写中的正则表达式可能是错误的。

app1.conf

location /app1/ {
  rewrite ^/[^\/]+)(/.*) $1 break;
  proxy_pass http://app1;
}

【讨论】:

  • 该重写将从原始 url /app1/some/more/ 中删除 /app1/。如果您不希望任何内容被切断,则应将其删除。
  • 谢谢,我刚刚检查过了。当我有位置 /app1/ 的 app1.conf { rewrite ^/app1^/ /$1 break; proxy_pass app1;我无法重新启动 nginx,我收到错误:/etc/init.d/nginx restart [....] 重新启动 nginx(通过 systemctl):nginx.service 的 nginx.serviceJob 失败,因为控制进程以错误代码退出。有关详细信息,请参阅“systemctl status nginx.service”和“journalctl -xe”。失败的!在 error.log: 2017/03/03 15:29:05 [emerg] 21787#0: "location" 指令在 /etc/nginx/app1.conf:1 中是不允许的
  • UPD:我已经解决了这个问题 - 需要将 app1.conf 重命名为 app1(不带 .conf)
【解决方案2】:

尝试以下方法:

  1. 创建文件/etc/nginx/upstream.conf

    server 10.0.0.2:8888;
    server 10.0.0.3:8888;
    server 10.0.0.4:8888;
    
  2. 将您的配置更改为:

    upstream app1 {
        include /etc/nginx/upstream.conf;
    }
    
    server {
        listen 80;
    
        location /app1/ {
        rewrite ^/app1^/ /$1 break;
        proxy_pass http://app1;
        } 
    }
    

【讨论】:

  • 感谢您的回答。我检查了这个变体,但我绝对需要将上游的所有配置保存在单独的文件中——这样我可以通过重命名这个文件来禁用这个配置。但似乎不可能,所以我将使用包含指令。
  • 它在 1.19.0 上失败为nginx: [emerg] directive "server" has no opening "{" in /etc/nginx/upstream.conf:1
猜你喜欢
  • 2015-12-29
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
相关资源
最近更新 更多