我在让 Nginx 与 Python Flask-socketio 库(基于 gevent)很好地配合时遇到了一些麻烦。目前,由于我们正在积极开发,我正试图让 Nginx 仅作为代理工作。对于发送页面,我可以通过直接运行 flask-socketio 应用程序或通过 gunicorn 运行来实现这一点。一个障碍:websocket 消息传递似乎不起作用。页面已成功托管和显示。但是,当我尝试使用 websockets 时,它们不起作用。它们足够活跃,以至于 websocket 认为它已连接,但它们不会发送消息。如果我删除 Nginx 代理,它们确实可以工作。当我尝试发送消息时,Firefox 给我这个错误:
Firefox can't establish a connection to the server at ws:///socket.io/1/websocket/.
网址是服务器所在的位置,唯一 ID 只是一堆随机数字。它似乎足以保持连接处于事件状态(例如,客户端认为它已连接),但无法通过 websocket 发送消息。我不得不认为这个问题与代理的某些部分有关,但是在调试问题可能是什么时遇到了很大的麻烦(部分原因是这是我第一次使用 Flask-socketIO 和 nginx)。我用于 nginx 的配置文件是:
user <user name>; ## This is set to the user name for the remote SSH session
worker_processes 5;
events {
worker_connections 1024; ## Default: 1024
}
http {
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
server_names_hash_bucket_size 128; # this seems to be required for some vhosts
server {
listen 80;
server_name _;
location / {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
}
我将配置文件作为一般示例和 websocket 特定示例的混合体,但试图摆弄它并没有解决问题。另外,当我在 wsgi 模式下使用 Flask app.wsgi_app 时,我正在使用 werkzeug Proxy_Fix 调用。然而,我已经尝试过,不管有没有,都无济于事。如果有人有一些见识,我将全神贯注。
最佳答案
我设法解决了这个问题。这些问题并非特定于 flask-socketio,而是特定于 Ubuntu、NginX 和 gevent-socketio。存在两个重大问题:
如果尝试在此配置中调试 websocket,我建议执行以下步骤:
刚刚检查了所有这些东西,有外卖。
sudo apt-get install python-software-properties
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx
在 Windows 等系统中,您可以使用 installer并且不太可能获得坏版本。
user <user account, probably optional>;
worker_processes 2;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
# tcp_nopush on;
keepalive_timeout 3;
# tcp_nodelay on;
# gzip on;
client_max_body_size 20m;
index index.html;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
# Listen on 80 and 443
listen 80 default;
listen 443 ssl; (only needed if you want SSL/HTTPS)
server_name <your server name here, optional unless you use SSL>;
# SSL Certificate (only needed if you want SSL/HTTPS)
ssl_certificate <file location for your unified .crt file>;
ssl_certificate_key <file location for your .key file>;
# Optional: Redirect all non-SSL traffic to SSL. (if you want ONLY SSL/HTTPS)
# if ($ssl_protocol = "") {
# rewrite ^ https://$host$request_uri? permanent;
# }
# Split off basic traffic to backends
location / {
proxy_pass http://localhost:8081; # 127.0.0.1 is preferred, actually.
proxy_redirect off;
}
location /socket.io {
proxy_pass http://127.0.0.1:8081/socket.io; # 127.0.0.1 is preferred, actually.
proxy_redirect off;
proxy_buffering off; # Optional
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
}
from flask import Flask, render_template, session, request, abort
import flask.ext.socketio
FLASK_CORE_APP = Flask(__name__)
FLASK_CORE_APP.config['SECRET_KEY'] = '12345' # Luggage combination
SOCKET_IO_CORE = flask.ext.socketio.SocketIO(FLASK_CORE_APP)
@FLASK_CORE_APP.route('/')
def index():
return render_template('index.html')
@SOCKET_IO_CORE.on('message')
def receive_message(message):
return "Echo: %s"%(message,)
SOCKET_IO_CORE.run(FLASK_CORE_APP, host=127.0.0.1, port=8081)
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>
<script type="text/javascript">
var url = window.location.protocol + document.domain + ':' + location.port,
socket = io.connect(url);
socket.on('message', alert);
io.emit("message", "Test")
</script>
关于sockets - Nginx 和 Flask-socketio Websockets : Alive but not Messaging?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22927060/