问题在于:
主机有多个docker应用程序在不同的端口上运行,例如。 App1 @ 3001,App2 @ 3002 ... 3100等
现在,我想以这种格式访问应用http://hostname.com/app1,http://hostname.com/app2 ..
为此,我在主机上运行nginx,根据子uri将请求代理到正确的端口
location = /app1 {
proxy_redirect http://hostname:3001/;
include /etc/nginx/proxy_params;
}
location ^~ /app1 {
proxy_redirect http://hostname:3001/app1;
include /etc/nginx/proxy_params;
}
但是,当网站的子uri更改或网站重定向时,这不起作用。 例如:
If I visit the site at hostname:3001 -> I can see the site
If I visit the site at http://hostname.com/app1 -> I can see the site
If the site page is at hostname:3001/static/index.html then when i access it as http://hostname.com/app1 the page changes to http://hostname.com/static/index.html -> I get 404.
有办法做到这一点吗?或者唯一的方法是将dns设置为app1.hostname.com并进行基于名称的路由?
答案 0 :(得分:2)
在您想要的server {}
区块内:
location /app1 {
rewrite ^/app1(.*) /$1 break;
proxy_pass http://hostname:3001/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /app2 {
rewrite ^/app2(.*) /$1 break;
proxy_pass http://hostname:3002/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
此处的重写规则会将正确的uris传递给端口
答案 1 :(得分:1)
您可以让每个应用都在一个单独的端口(例如3000和3001)上进行侦听,然后按如下方式配置您的nginx(将其包含在server {}
定义块中):
location /app1 {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
}
location /app2 {
proxy_pass http://localhost:3001;
proxy_set_header X-Real-IP $remote_addr;
}