Prä*_*olf 9 django proxy nginx docker docker-compose
NGINX代理将HTTP GET请求而不是WebSocket握手传递给我的django应用程序.
事实:
相关日志:
通过容器化nginx代理连接时的Daphne日志:
`xxx.xxx.xxx.xxx:40214 - - [24/May/2017:19:16:03] "GET /flight/all_flight_updates" 404 99`
Run Code Online (Sandbox Code Playgroud)
Daphne在绕过容器化代理并直接连接到服务器时进行日志记录:
xxx.xxx.xxx.xxx:6566 - - [24/May/2017:19:17:02] "WSCONNECTING /flight/all_flight_updates" - -
xxx.xxx.xxx.xxx:6566 - - [24/May/2017:19:17:02] "WSCONNECT /flight/all_flight_updates" - -
Run Code Online (Sandbox Code Playgroud)
nginx(非容器化)配置的localhost测试工作:
[2017/05/24 14:24:19] WebSocket HANDSHAKING /flight/all_flight_updates [127.0.0.1:65100]
[2017/05/24 14:24:19] WebSocket CONNECT /flight/all_flight_updates [127.0.0.1:65100]
Run Code Online (Sandbox Code Playgroud)
配置文件:
我的docker-compose.yml:
version: '3'
services:
db:
image: postgres
redis:
image: redis:alpine
web:
image: nginx
ports:
- '80:80'
volumes:
- ./deploy/proxy.template:/etc/nginx/conf.d/proxy.template
links:
- cdn
- app
command: /bin/bash -c "envsubst '' < /etc/nginx/conf.d/proxy.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
cdn:
image: nginx
volumes:
- ./cdn_static:/usr/share/nginx/static
- ./deploy/cdn.template:/etc/nginx/conf.d/cdn.template
command: /bin/bash -c "envsubst '' < /etc/nginx/conf.d/cdn.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
app:
build: .
image: app
ports:
- '8000:8000'
links:
- redis
- db
volumes:
- ./cdn_static:/var/static
Run Code Online (Sandbox Code Playgroud)
我的proxy.templateNGINX配置模板:
upstream cdn_proxy {
server cdn:80;
}
upstream daphne {
server app:8000;
keepalive 100;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
location /static {
proxy_pass http://cdn_proxy;
}
location / {
proxy_buffering off;
proxy_pass http://daphne;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
我使用NGINX网站上的教程构建了一个更紧凑的问题示例,并将其放在github上,网址为https://github.com/c0yote/nginx-websocket-issue.
你得到426而不是404,但我相信这是因为简单的服务器不知道如何处理NGINX发送的GET.我在这个想法中得到了加强,因为如果你直接针对8000端口发出GET(例如从浏览器),你会得到相同的426.
因此核心问题仍然是NGINX正在发送GET.
更多信息:
tcpdump显示GET到websocket服务器有一个Upgrade字段,但GET反对NGINX没有.这是令人困惑的,因为该wscat命令与目标端口的例外相同.
GIGA更新:*
如果我从端口80关闭NGINX代理,8080它可以工作.我唯一的猜测是,js客户端对端口80做了一些假设.如果有人知道为什么会这样,我很想知道.
| 归档时间: |
|
| 查看次数: |
1616 次 |
| 最近记录: |