使用 Docker Compose 将 Nginx 反向代理到 Transmission

使用 Docker Compose 将 Nginx 反向代理到 Transmission

我正在尝试让 Nginx 作为 Transmission Web 客户端的反向代理。两者都位于带有 Docker Compose 的单独 Docker 容器中。

我读过以前类似的问题,但这些问题对我没有用: https://unix.stackexchange.com/questions/64812/get-transmission-web-interface-working-with-web-server

https://stackoverflow.com/questions/15391073/nginx-transmission-daemon-url-rewrite

Nginx 给出了错误:

[错误] 6#6:*3 未找到“/etc/nginx/html/transmission/web/index.html”(2:没有此文件或目录),客户端:172.18.0.1,服务器:torrent.localhost,请求:“GET /transmission/web/HTTP/1.1”,主机:“torrent.localhost”

这让我怀疑 proxy_pass 无法正常工作,因为它仍在尝试从文件系统中获取 html 文件。

任何帮助都将非常感激!

docker-compose.yml:

version: "2"
services:
  nginx:
    image: nginx
    container_name: nginx-proxy
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./www:/data/www
    ports:
    - "80:80"
    - "443:443"

  transmission:
    image: linuxserver/transmission
    container_name: transmission
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Europe/London
      - USER=username
      - PASS=password
    volumes:
      - ./transmission/config:/config
      - ./transmission/downloads:/downloads
      - ./transmission/watch:/watch
    ports:
      - "9091:9091"
      - "51413:51413"
      - "51413:51413/udp"
    restart: unless-stopped

nginx.conf:

events {
}

http {
    server {
        listen 80;
        server_name localhost 127.0.0.1;
        root /data/www;
        location = / {
            index index.html;
        }
    }

    server {
        server_name torrent.localhost;
        location = / {
            proxy_read_timeout 300;
            proxy_pass_header  X-Transmission-Session-Id;
            proxy_set_header   X-Forwarded-Host $host;
            proxy_set_header   X-Forwarded-Server $host;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://transmission:9091;
        }
    }
}

索引.html:

hello world!

答案1

您的问题在于“location”后面的等号。如果 location 需要与 EXACT 请求 URI 匹配,则可以使用等号。

您可能想要删除等号(“=”),如下所示:

server {
    server_name torrent.localhost;
    location / { # <-- remove "=" sign
        proxy_read_timeout 300;
        proxy_pass_header  X-Transmission-Session-Id;
        proxy_set_header   X-Forwarded-Host $host;
        proxy_set_header   X-Forwarded-Server $host;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://transmission:9091;
    }
}

答案2

您可以通过将容器添加到单个网络来实现。您可以在 docker-compose 文件中定义网络。示例如下,

version: "2"
services:
  nginx:
    image: nginx
    container_name: nginx-proxy
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./www:/data/www
    ports:
    - "80:80"
    - "443:443"
    networks:
      NETWORKNAME:
        ipv4_address: 10.5.2.1

  transmission:
    image: linuxserver/transmission
    container_name: transmission
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Europe/London
      - USER=username
      - PASS=password
    volumes:
      - ./transmission/config:/config
      - ./transmission/downloads:/downloads
      - ./transmission/watch:/watch
    ports:
      - "9091:9091"
      - "51413:51413"
      - "51413:51413/udp"
    restart: unless-stopped
    networks:
      NETWORKNAME:
        ipv4_address: 10.5.2.2

networks:
  NETWORKNAME:
    driver: bridge
    ipam:
      config:
        - subnet: 10.5.0.0/16
          gateway: 10.5.0.1

根据需要替换 NETWORKNAME。另外,根据您的要求选择子网掩码(您也可以根据需要选择 IP)。然后按如下方式更改您的 nginx conf,

server {
    listen 80;
    server_name "abc.example.com";   #your domain name
 
    location / {

        limit_except GET POST PUT HEAD DELETE OPTIONS PATCH {
             deny   all;
        }

        proxy_pass https://10.5.2.2;     #IP of your web container
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_intercept_errors on;
    }
}

以下项目使用 Nginx 容器作为反向代理。您可以参考该项目并找到更多有用的信息。 OrangeHRM Docker化开发环境

相关内容