使用docker-compose运行时如何在nginx配置中引用Web?

问题描述 投票:0回答:1

我尝试使用Nginx,Certbot和我的应用程序配置服务:

docker-compose.yml

version: '3'

services:
 nginx:
  image: nginx:1.15-alpine
  restart: unless-stopped
  volumes:
   - ./data/nginx:/etc/nginx/conf.d
   - ./data/certbot/conf:/etc/letsencrypt
   - ./data/certbot/www:/var/www/certbot
  ports:
   - "80:80"
   - "443:443"
  command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
 certbot:
  image: certbot/certbot
  restart: unless-stopped
  volumes:
   - ./data/certbot/conf:/etc/letsencrypt
   - ./data/certbot/www:/var/www/certbot
  entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
 web:
  image: example_app:latest
  restart: unless-stopped
  ports:
   - "5005:5005"

nginx/app.conf

server {
  listen 80;
  server_name my.app;
  server_tokens off;
  location /.well-known/acme-challenge/ {
    root /var/www/certbot;
  }
  location / {
    return 301 https://$host$request_uri;
  }
}
server {
  listen 443 ssl;
  server_name my.app;
  server_tokens off;
  ssl_certificate /etc/letsencrypt/live/my.app/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/my.app/privkey.pem;
  include /etc/letsencrypt/options-ssl-nginx.conf;
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
  location / {
    proxy_pass http://web;
  }
}

[每当我通过https://my.app请求服务时,我总是502网关。

我浏览了许多材料,但我不知道我在做什么错?

nginx docker-compose docker-networking certbot
1个回答
0
投票

问题出在upstream服务上,我将restart: unless-stopped中的restart:alwaysweb更改为docker-compose.yml

nginx/app.conf

upstream flask {
  server web:5005;
}

server {
    listen 80;
    server_name my.app;
    server_tokens off;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name my.app;
    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/patricia.app/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/patricia.app/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass  http://flask;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.