gpt4 book ai didi

nginx - 在 Nginx 配置文件中访问容器名称

转载 作者:行者123 更新时间:2023-12-02 18:31:48 25 4
gpt4 key购买 nike

在 Docker 中,我使用 docker network create usta_network 创建了一个新网络命令并在 docker-compose.yml 文件中的服务声明中引用此网络。

没关系,我现在可以使用容器名称 ping 服务,但是如何在 Nginx 上游声明中引用容器名称?在容器之间进行访问的最佳或更好的做法是什么?

用于 nginx 的 docker.compose

version: '2'

services:
nginx-main:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- /var/log/nginx:/var/log/nginx
container_name: nginx_main
networks:
default:
external:
name: usta_network
./nginx.conf

用户 nginx;
http {

server {
listen 80;
index index.php index.html index.htm;
server_name example.org www.example.org;

location / {

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://rahmanusta_upstream;
}
}

upstream rahmanusta_upstream {

server rahmanusta_wp:6565;

}
}

docker-compose for mysql + wordpress
version: '2'

services:
mysql-db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: mysql_db
MYSQL_USER: root
MYSQL_PASSWORD: pass
container_name: mysql_db

rahmanusta-wp:
depends_on:
- mysql-db
image: wordpress:latest
ports:
- "6565:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: pass
container_name: rahmanusta_wp
volumes:
db_data:
networks:
default:
external:
name: usta_network

docker 网络检查 usta_network
[
{
"Name": "usta_network",
"Id": "b971429c1ddcfa791bdfff0a6f9463ab9cfb9ae04ba6a3aa60ce08ca11d0a5ab",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.20.0.0/16",
"Gateway": "172.20.0.1/16"
}
]
},
"Internal": false,
"Containers": {
"16cc89ca548fe90fb8ed9431d8e9633c341373344304ec3320acee1a81090709": {
"Name": "rahmanusta_wp",
"EndpointID": "d310cbf9617958226d36d5f9e0d804c113093097669b1d5ae8bed90d31dfca2f",
"MacAddress": "02:42:ac:14:00:03",
"IPv4Address": "172.20.0.3/16",
"IPv6Address": ""
},
"5b0f7a7d95b23b6e69b17f85ebff5d652bdbcba305755169c6b9f78199fbf346": {
"Name": "mysql_db",
"EndpointID": "d2c304b450ce96d309ef51319aa42336bb742d99db095e98e129e547aee820c3",
"MacAddress": "02:42:ac:14:00:02",
"IPv4Address": "172.20.0.2/16",
"IPv6Address": ""
},
"addf3c475fcfd5566b3d1c4b4a951f78978c07265211746f79711f38c5cd9649": {
"Name": "nginx_main",
"EndpointID": "3f4106184dbe805c509011c3dc42c673c85fcf2eb08441a5ecf24b9c5c68e2d4",
"MacAddress": "02:42:ac:14:00:04",
"IPv4Address": "172.20.0.4/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]

谢谢。

最佳答案

在 docker-compose.yml 中,您将使用相关名称为每个服务命名。假设我在名为 jenkins1 的 nginx docker 容器后面创建了一个 jenkins 实例。

在 nginx.conf 文件的 http 部分,我将上游服务器命名为:

upstream jenkins {
server jenkins1:8080;
}

在服务器块中,我可以如下调用它;
location / {
proxy_pass https://jenkins;
}

这会将到达 nginx 容器端口的所有流量定向到 jenkins1 服务容器上的端口 8080。

编辑基于更新的问题

在 docker-compose.yml 中,使用外部链接从 .yml 文件外部链接两个容器。您的 nginx 服务部分应该阅读;
services:
nginx-main:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- /var/log/nginx:/var/log/nginx
external_links:
- rahmanusta_wp
- mysql_db
container_name: nginx_main

关于nginx - 在 Nginx 配置文件中访问容器名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41100513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com