简介:
如何将Nginx配置为将调用转发到主机。
Long描述:
我们有一个web应用程序,它可以与两个后端通信(让我们说rest1、rest2和rest3)。我们对rest1负责。
让我们考虑一下,我在我的pc上手动启动rest1,并在2345端口上运行。我希望nginx (它在docker中运行)将对rest1的所有调用重定向到我自己正在运行的实例(注意,该实例在主机上运行,而不是在任何容器上,而不是在坞中)。而对于rest2和rest3到其他对接节点,或者可能是其他服务器(谁在乎)。
我要找的是:
docker-compose.yml配置(如果需要)。提前谢谢。
发布于 2017-02-24 15:29:21
配置nginx,如下所示(确保您替换了Docker的IP )并将其保存为default.conf
server {
listen 80;
server_name _;
location / {
proxy_pass http://<IP of Docker Host>;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}现在打开容器:
docker run -d --name nginx -p 80:80 -v /path/to/nginx/config/default.conf:/etc/nginx/conf.d/default.conf nginx发布于 2019-09-22 21:43:20
如果您使用的是3版本,则根本不需要对docker-compose.yml文件进行任何特殊配置,只需使用特殊的DNS名称host.docker.internal即可到达主机服务,如下面的nginx.conf示例所示:
events {
worker_connections 1024;
}
http {
upstream host_service {
server host.docker.internal:2345;
}
server {
listen 80;
access_log /var/log/nginx/http_access.log combined;
error_log /var/log/nginx/http_error.log;
location / {
proxy_pass http://host_service;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $realip_remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
}发布于 2018-08-03 08:50:03
解决方案1
使用network_mode: host,这将将nginx实例绑定到主机的网络接口。这可能导致在运行多个nginx容器时发生冲突:每个公开的端口都绑定到主机的接口。
解决方案2
我正在运行更多的nginx实例,用于我想要向外界公开的每一项服务。为了使nginx配置保持简单,并避免将每个nginx绑定到主机,请使用容器结构:
dockerhost -一个带有network_mode: host的虚拟容器
proxy - nginx容器用作主机服务的代理,
将dockerhost链接到proxy,这将在proxy contianer中添加一个/etc/hosts条目--我们可以在nginx配置中使用'dockerhost‘作为主机名。
docker-compose.yaml
version: '3'
services:
dockerhost:
image: alpine
entrypoint: /bin/sh -c "tail -f /dev/null"
network_mode: host
proxy:
image: nginx:alpine
links:
- dockerhost:dockerhost
ports:
- "18080:80"
volumes:
- /share/Container/nginx/default.conf:/etc/nginx/conf.d/default.conf:rodefault.conf
location / {
proxy_pass http://dockerhost:8080;该方法允许我们为我的服务器上运行的每一个服务自动生成encrtypt证书。如果有兴趣的话,我可以贴出解决方案的要点。
https://stackoverflow.com/questions/42438381
复制相似问题