我在同一主机上运行了多个 ruby 应用程序:
~/app1
~/app2
~/app3
我想让 nginx 使用如下子目录代理这些应用程序:
http://example.com/app1
http://example.com/app2
http://example.com/app3
我很好奇 nginx 是否支持我在多个文件中定义这些位置,这样我就可以保留每个配置与应用程序,而不是为所有应用程序使用一个单片配置文件:
~/app1/nginx.conf
~/app2/nginx.conf
~/app3/nginx.conf
我天真地尝试在 3 个配置文件中分别使用单个位置指令来定义服务器,结果conflicting server name "example.com" on [::]:80, ignored
得到的配置如下所示:
upstream app1 { server 127.0.0.1:4567; }
server {
listen [::]:80;
listen 80;
servername example.com
location /app1 {
proxy_pass http://app1;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Request-Start $msec;
}
}
有没有办法以这种方式组织配置?
答案1
您可以通过 include 包含外部配置:
include /path/to/config1.conf;
include /path/to/config2.conf;
include /path/to/confdir/*.conf;
server {
server_name example.com;
listen [::]:80;
listen 80;
}
在单独的配置中,您可以使用任何有效的代码块:
upstream app1 {
server 127.0.0.1:8080;
}
location /app1 {
proxy_pass http://app1;
}
答案2
我相信您可以使用以下配置:
server {
server_name example.com;
listen [::]:80;
listen 80;
include /path/to/applications/*/nginx.conf;
}
然后在每一个应用程序的目录中配置重定向如下:
location /app1 {
proxy_pass http://app1;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Request-Start $msec;
}