docker - 反向代理 Nginx Docker 容器
问题描述
我在运行多个代理并将 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 文件
码头工人-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;
}
}
我很感激你能给我的任何建议。
解决方案
实际上这不是我想要的,但它适用于 twisted 而不是 nginx。也许有人找到了更好的解决方案。
码头工人-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)
推荐阅读
- amazon-web-services - 如何创建指向 S3 对象的私有可下载链接
- python - 如何在 gitpod 中导入 pygame?
- r - 向 CRAN 提交包不接受函数内部的 set.seed 但不知道如何避免它
- javascript - react.js 文件中的多个导出
- json - 如何在firemonkey,delphi rio中获得列表框最大位置?
- sql - 基于条件的sql server中可能的记录组合
- java - 签名后更新令牌中的 jwt 范围
- android - 如何添加密钥库以响应本机应用程序以使用 Firebase 身份验证?
- elasticsearch - 如何加入两个弹性搜索插入?
- javascript - 新添加的元素复选框不起作用