![](/img/trans.png)
[英]nginx docker container as reverse proxy for services running in docker containers
[英]Implementation of Nginx Container for Reverse Proxying and SSL certificates for Django Containers inside Docker Swarm
我想用 Docker Swarm 部署 Django 應用程序。 I was following this guide where it does not use the docker swarm nor docker-compose, and specifically created two Django containers, one Nginx container, and a Certbot container for the SSL certificate. Nginx 容器反向代理和負載平衡跨兩個 Django 容器,這兩個容器在使用它們的 IP 的兩個服務器中
upstream django {
server APP_SERVER_1_IP;
server APP_SERVER_2_IP;
}
server {
listen 80 default_server;
return 444;
}
server {
listen 80;
listen [::]:80;
server_name your_domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name your_domain.com;
# SSL
ssl_certificate /etc/letsencrypt/live/your_domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your_domain.com/privkey.pem;
ssl_session_cache shared:le_nginx_SSL:10m;
ssl_session_timeout 1440m;
ssl_session_tickets off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
client_max_body_size 4G;
keepalive_timeout 5;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://django;
}
location ^~ /.well-known/acme-challenge/ {
root /var/www/html;
}
}
我想使用 Docker Swarm 實現所有這些相同的功能,這樣我就可以使用一個命令docker service update --replicas 3 <servicename>
來擴展容器
The problem is I am not able to understand How to use implement the Nginx container in this scenario, Docker Swarm provides its load balancing so I did not need Nginx for that but Nginx is still needed for SSL certificates. 那么如何在 Swarm 中實現 Nginx 以便它為所有節點提供 SSL 證書並反向代理到 Django 容器? I only used Nginx before for reverse proxying so I am not able to figure how to write the Nginx conf and make the Nginx Container work with the Django Container with SSL included all inside a Docker Swarm.
####################
# docker-stack.yml #
####################
version: '3.7'
services:
web:
image: 127.0.0.1:5000/django-image
deploy:
replicas: 3
command: gunicorn mydjangoapp.wsgi:application --bind 0.0.0.0:8000
expose:
- 8000
depends_on:
- nginx
nginx:
image: 127.0.0.1:5000/nginx-image
deploy:
replicas: 2
ports:
- 80:80
depends_on:
- web
nginx.conf 我用於撰寫文件以指向一個 Django 容器
upstream django {
server web:8000; #web is name of django service
}
server {
#SSL STUFF
listen 80;
location / {
proxy_pass http://django;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
因此,在 nginx 和世界之間,您可以選擇讓 docker 將負載平衡輸入到您的 nginx 實例,或使用外部負載平衡器。 如果您有一組外部負載均衡器指向的固定節點,那么
nginx:
image: 127.0.0.1:5000/nginx-image
ports:
- 443:443
networks:
- proxy
deploy:
mode: global
placement:
constraints:
- node.labels.myorg.lb==true
和 label 對應的節點myorg.lb=true
接下來,關於你的服務,docker 基本上有兩種廣告復制服務的方式:vip 和 dnsrr。 使用vip
模式 - 默認值 - docker 會將單個 ip 地址分配給名稱“web” - 這是您為 nginx 副本分配的地址,然后它將在該副本之間進行負載平衡。 您可以將服務切換到 dnsrr 模式,在這種情況下,dns 對 web 的查詢將是所有服務副本的當前 ips 的動態變化列表。 或者,您可以使用顯式名稱tasks.<service>
來獲取相同的 dnsrr 條目。
現在。 我不知道 nginx 是否支持負載平衡到 dnsrr 開箱即用。 但我知道它會長時間緩存條目,因此您需要使用顯式解析器 (127.0.0.11) 設置 nginx,刷新間隔較短。
web:
image: 127.0.0.1:5000/django-image
command: gunicorn mydjangoapp.wsgi:application --bind 0.0.0.0:8000
networks:
- proxy
deploy:
replicas: 3
endpoint_mode: dnsrr
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.