Nginx 代理传递配置 docker

标签 nginx docker proxypass

问题来了:

主机有多个 docker 应用程序在不同的端口上运行,例如。 App1 @ 3001,App2 @ 3002...3100 等

现在我想访问这种格式的应用程序 http://hostname.com/app1 , http://hostname.com/app2 ..

为此,我在主机上运行 nginx,以根据 sub-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 并进行基于名称的路由?

最佳答案

在您想要的 server {} block 内:

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;
}

这里的重写规则会将正确的uri传递给端口

关于Nginx 代理传递配置 docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26305162/

相关文章:

apache - 设置 HTTP 代理以插入 header

node.js - 如何使用 ExpressJS 在代理上传递 session ?

nginx - Nginx conf模板无法与Dokku一起使用

docker - 使用 nginx 镜像进行多阶段 docker 构建

docker - 对 https 上下文中的所有服务器 block 强制使用 https

apache - mod_proxy/proxypass 错误

nginx - 在 CentOS 7 上使用 php7 和 nginx 安装 Typo3

django - uWSGI找不到python3插件 - 打开("./python3_plugin.so"): No such file or directory

docker - 需要 docker 网络做什么?

Docker 未知标志 --mount