当我尝试从 Django 应用程序调用在端口 5000 上使用 docker-compose 部署的服务时遇到此错误,该应用程序也在端口 8000 上使用 docker-compose 部署。我也在使用 nginx。
ConnectionError at /documents/
HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url:
/api/documents (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7f976607f290>: Failed to establish a new connection: [Errno 111] Connection refused'))
这是发出请求之前的请求对象
kwargs
{'data': None,
'files': {'file': ('XXX',
<InMemoryUploadedFile: XXX.pdf (application/pdf)>,
'application/pdf',
{})},
'headers': {'Authorization': 'Token token=XXX',
'Content-Type': 'application/pdf',
'PSPDFKit-API-Version': '2020.1.3'},
'json': None}
method
'post'
session
<requests.sessions.Session object at 0x7f954da972d0>
url
'http://127.0.0.1:5000/api/documents'
这里是相关文件
docker 组成
version: '3.7'
services:
web:
build:
context: ./www
dockerfile: Dockerfile.prod
command: gunicorn app.wsgi:application --bind 0.0.0.0:8000
volumes:
- ./www:/usr/src/app
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
expose:
- 8000
env_file: env.prod
depends_on:
- db
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file: env.prod
nginx:
build: ./nginx
volumes:
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
ports:
- 1337:80
depends_on:
- web
pspdfkit:
image: "pspdfkit/pspdfkit:2020.1"
environment:
PGUSER: XXX
PGPASSWORD: XXX
PGDATABASE: XXX
PGHOST: db
PGPORT: 5432
# Activation key for your PSPDFKit Server installation.
ACTIVATION_KEY: XXXX
# Secret token used for authenticating API requests.
API_AUTH_TOKEN: XXXX
# Base key used for deriving secret keys for the purposes of authentication.
SECRET_KEY_BASE: XXXX
# Public key used for verification of JWTs from web clients. It has to be in the PEM format.
JWT_PUBLIC_KEY: |
-----BEGIN PUBLIC KEY-----
XXXXTYYYYY
-----END PUBLIC KEY-----
JWT_ALGORITHM: RS256
# Credentials to access the admin dashboard.
DASHBOARD_USERNAME: XXX
DASHBOARD_PASSWORD: YYY
depends_on:
- db
restart: always
ports:
- "5000:5000"
volumes:
- asset_storage:/srv/asset_storage
volumes:
postgres_data:
static_volume:
media_volume:
asset_storage:
nginx.conf
upstream app {
server web:8000;
}
server {
client_max_body_size 20M;
listen 80;
location / {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /staticfiles/ {
alias /home/app/web/staticfiles/;
}
location /mediafiles/ {
alias /home/app/web/mediafiles/;
}
}
设置.py
PSPDFKIT_SERVER_HOST = "http://127.0.0.1:5000"
PSPDFKIT_EXTERNAL_SERVER = "http://127.0.0.1:5000"
我尝试将 url 更改为 localhost
、0.0.0.0
、pspdfkit
(来自 docker-compose 文件),以及服务器的公共(public) ip,但我最终在所有情况下都会收到上述错误,除非我使用公共(public) ip(它无法建立连接)
我不太精通 nginx,所以我可能遗漏了一些导致此问题的重要配置步骤。帮助将不胜感激!
最佳答案
错误来自于使用0.0.0.0
Docker introduces a virtual network, so each docker-isntance sees itself as 127 not the host machine
(感谢@Jmons)
要在本地启动您的代码,使用您的固定 IPv4 地址。
对于 Windows 用户:打开命令提示符 (cmd) 并运行 ipconfig
命令,检索位于 IPv4 地址的 @IP。 . . . . . . . . . . . . .:
一般为192.168.x.x
类型
IPv4 address in command prompt
关于django - HTTPConnectionPool(主机 ='0.0.0.0',端口=5000): Max retries exceeded with url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61070764/