【发布时间】: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