反向代理 Nginx Docker 容器

时间:2021-01-30 12:05:58

标签: docker nginx nginx-reverse-proxy

我在运行多个代理并将 nginx 反向代理连接到它时遇到问题。

图片显示我要存档的内容 This is what I want to archive

当我直接连接到代理时有效

# proxy 1
print(requests.get("https://api.ipify.org?format=json", proxies={
    "http": "127.0.0.1:9000",
    "https": "127.0.0.1:9000"
}).content)

# proxy 2
print(requests.get("https://api.ipify.org?format=json", proxies={
    "http": "127.0.0.1:9001",
    "https": "127.0.0.1:9001"
}).content)

但是当我使用nginx反向代理时它不起作用

# nginx
print(requests.get("https://api.ipify.org?format=json", proxies={
    "http": "127.0.0.1:8080",
    "https": "127.0.0.1:8080"
}).content)

回复:

requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.ipify.org', port=443): Max retries exceeded with url: /?format=json (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 400 Bad Request')))

这是我的 docker 容器 yml 文件

docker-compose.yml

version: "2.4"
services:

  proxy:
    image: qmcgaw/private-internet-access
    cap_add:
      - NET_ADMIN
    restart: always
    ports:
        - 127.0.0.1:9000-9001:8888/tcp
    environment:
      - VPNSP=Surfshark
      - OPENVPN_USER=${user}
      - PASSWORD=${pass}
      - HTTPPROXY=ON
    scale: 2

  nginx:
    image: nginx
    volumes:
        - ./default.conf:/etc/nginx/conf.d/default.conf
    ports:
        - "8080:80"

和我的 nginx 配置

default.conf

server {
    listen       80;
    server_name  localhost;
    
    location / {
        proxy_pass http://proxy:8888;
    }
}

如果您能给我任何建议,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

实际上这不是我想要的,但它适用于扭曲而不是 nginx。也许有人会找到更好的解决方案。

docker-compose.yml

version: "2.4"
services:
  proxy:
    image: qmcgaw/private-internet-access
    cap_add:
      - NET_ADMIN
    restart: always
    environment:
      - VPNSP=Surfshark
      - OPENVPN_USER=${user}
      - PASSWORD=${pass}
      - HTTPPROXY=ON
    scale: 2

  twisted:
    container_name: twisted
    build: .
    restart:
      always
    ports:
      - 127.0.0.1:8080:8080/tcp
    healthcheck:
      test: ["CMD-SHELL", "curl https://google.de --proxy 127.0.0.1:8080"]
      interval: 20s
      timeout: 10s
      retries: 5

Dockerfile

FROM stibbons31/alpine-s6-python3:latest

ENV SRC_IP="0.0.0.0"
ENV SRC_PORT=8080
ENV DST_IP="proxy"
ENV DST_PORT=8888

RUN apk add --no-cache g++ python3-dev

RUN pip3 install --no-cache --upgrade pip
RUN pip3 install service_identity twisted

WORKDIR /app
ADD ./app /app

CMD [ "twistd", "-y", "main.py", "-n"]
    

ma​​in.py

import os

from twisted.application import internet, service
from twisted.protocols.portforward import ProxyFactory

SRC_PORT = int(os.environ["SRC_PORT"])
DST_PORT = int(os.environ["DST_PORT"])

application = service.Application("Proxy")
ps = internet.TCPServer(SRC_PORT,
                        ProxyFactory(os.environ["DST_IP"], DST_PORT),
                        50,
                        os.environ["SRC_IP"])
ps.setServiceParent(application)