首页 > 解决方案 > 如何使用 swag 反向代理域根目录中的 docker 映像和子域中的另一个映像?

问题描述

我正在使用“linuxserver”的 swag图像来反向代理我的 docker-compose 图像。我想将我的一张图片作为我的域根,另一张作为子域(例如root-image @ mysite.com 和subdomain-image @ staging.mysite.com)。以下是我经历的步骤:

  1. 将我的域名和子域名重定向到我在 Cloudflare 中的服务器(ping 它们会显示我服务器的 IP,这一步OK!)
  2. 为 swag 配置了 Cloudflare DNS 配置(这工作正常!)
  3. 配置 docker-compose 文件:
  swag:
    image: linuxserver/swag:version-1.14.0
    container_name: swag
    cap_add:
      - NET_ADMIN
    environment:
      - PUID=1000
      - PGID=1000
      - URL=mysite.com
      - SUBDOMAINS=www,staging
      - VALIDATION=dns
      - DNSPLUGIN=cloudflare #optional
      - EMAIL=me@mail.com #optional
      - ONLY_SUBDOMAINS=false #optional
      - STAGING=false #optional
    volumes:
      - /docker-confs/swag/config:/config
    ports:
      - 443:443
      - 80:80 #optional
    restart: unless-stopped
  root-image:
    image: ghcr.io/me/root-image
    container_name: root-image
    restart: unless-stopped
  subdomain-image:
    image: ghcr.io/me/subdomain-image
    container_name: subdomain-image
    restart: unless-stopped
  1. 为我的根映像定义了一个代理 conf(在 swag/config/nginx/proxy-confs/root-image.subfolder.conf)
location / {
    include /config/nginx/proxy.conf;
    resolver 127.0.0.11 valid=30s;
    set $upstream_app root-image;
    set $upstream_port 3000;
    set $upstream_proto http;
    proxy_pass $upstream_proto://$upstream_app:$upstream_port;
}
  1. 注释掉 nginx 的默认location / {}块(在 swag/config/nginx/site-confs/default)
  2. 为我的子域图像定义了一个代理 conf(在 swag/config/nginx/proxy-confs/subdomain-image.subdomain.conf)
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name staging.*;
    include /config/nginx/ssl.conf;
    client_max_body_size 0;
    
    location / {
        include /config/nginx/proxy.conf;
        resolver 127.0.0.11 valid=30s;
        set $upstream_app subdomain-image;
        set $upstream_port 443;
        set $upstream_proto https;
        proxy_pass $upstream_proto://$upstream_app:$upstream_port;

    }
}

两个图像都暴露了端口 3000。现在我的根图像工作正常(好的!),但我的子域图像出现 502 错误。我检查了 nginx 错误日志,但它没有显示任何对我有意义的东西:

*35 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxxx.xxx, server: staging.*, request: "GET /favicon.ico HTTP/2.0", upstream: "https://xxx.xxx.xxx:443/favicon.ico", host: "staging.mysite.com", referrer: "https://staging.mysite.com"

所有 3 个容器的 Docker 日志也​​很好(没有显示任何警告或错误)。

哪一步我出错了,或者我错过了什么?感谢您的帮助

标签: dockernginxlets-encrypt

解决方案


推荐阅读