簡體   English   中英

反向代理 Nginx Docker 容器

[英]Reverse Proxy Nginx Docker Container

我在運行多個代理並將 nginx 反向代理連接到它時遇到問題。

圖片顯示了我要歸檔的內容這是我要存檔的

當我直接連接到代理時有效

# 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 配置

默認.conf

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

我很感激你能給我的任何建議。

實際上這不是我想要的,但它適用於扭曲而不是 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"]
    

主文件

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM