如何使用 nginx 容器proxy_pass端口 80 上的节点 docker 容器



简而言之,我正在尝试设置一个nginx容器以proxy_pass端口80上的其他容器。

我按照本教程进行操作:https://dev.to/domysee/setting-up-a-reverse-proxy-with-nginx-and-docker-compose-29jg

他们描述了有一个看起来像这样的 docker 组合文件:

version: '3'
services:
nginx: 
image: nginx:latest
container_name: production_nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./nginx/error.log:/etc/nginx/error_log.log
- ./nginx/cache/:/etc/nginx/cache
- /etc/letsencrypt/:/etc/letsencrypt/
ports:
- 80:80
- 443:443
your_app_1:
image: your_app_1_image:latest
container_name: your_app_1
expose:
- "80"
your_app_2:
image: your_app_2_image:latest
container_name: your_app_2
expose:
- "80"
your_app_3:
image: your_app_3_image:latest
container_name: your_app_3
expose:
- "80"

然后在nginx配置中,他们根据路径执行如下proxy_pass:

proxy_pass http://your_app_1:80;

这对我来说都很有意义,但是当我制作测试节点服务器以侦听端口 80 时,我收到错误:错误:侦听 EACCES:权限被拒绝 0.0.0.0:80。在节点服务器的 Dockerfile 中,我使用的是其他用户:

USER node

我知道我收到此错误是因为非 root 用户不应该能够在端口 1024 或其他端口下绑定。而且我知道在容器中以 root 身份运行是一种不好的做法......那么,这样的事情到底是怎么回事呢?我觉得我在这里错过了一些东西。每次在nginx中执行proxy_pass时,不必记住服务器正在运行的某些自定义高端口会很好...或者这只是生活中的事实?

只要我们不发布端口,我就看到在端口上进行公开时没有问题。

EXPOSE 不允许通过定义的端口与同一网络外部的容器或主机进行通信。要允许这种情况发生,您需要发布端口。

但它的代价是通过在 Docker 客户端或 Docker-Composecap_add上使用--add-cap标志授予内核功能来增加安全漏洞。NET_BIND_SERVICE是我们应该增加的能力。

最新更新