python - Socket-Management in Nginx with Flask - Why is Cross-Origin Request Blocked? -
i want create socket in flask-socketio through nginx , unicorn. following configuration works on localhost without nginx.
when access web application through nginx in chrome, error:
failed load resource: not connect server. http://52.34.18.48:6419/socket.io/?eio=3&transport=polling&t=1454455363683-6
when access web application through nginx in firefox, error:
cross-origin request blocked: same origin policy disallows reading remote resource @ http://52.34.18.48:6419/socket.io/?eio=3&transport=polling&t=1454464333740-25. (reason: cors request failed).
this how initialize socket connection in javascript:
import io "socket.io-client" const socketurl = 'http://' + document.domain + ':6419' + '/flaskapp' const socket = io(socketurl); export default socket;
my nginx config file looks this:
server { listen 80; server_name 52.34.18.48; error_log /var/www/flaskapp/nginx_errorlog.log; access_log /var/www/flaskapp/nginx_accesslog.log; root /var/www/flaskapp; location /socket.io { proxy_pass http://127.0.0.1:6419/socket.io; proxy_redirect off; proxy_buffering off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $remote_addr; proxy_http_version 1.1; proxy_set_header upgrade $http_upgrade; proxy_set_header connection "upgrade"; } location / { proxy_pass http://127.0.0.1:6419; proxy_buffering off; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $remote_addr; proxy_set_header host $host; proxy_http_version 1.1; proxy_set_header upgrade $http_upgrade; proxy_set_header connection "upgrade"; }
the gunicorn command used is:
gunicorn --worker-class eventlet -w 1 -b 127.0.0.1:6419 flask_app:application
i wrapped socket in flask application this.
from flask_app import application flask_socketio import socketio socketio = socketio(application)
why error via nginx not when connecting via localhost? how can access socket.io via nginx correctly?
Comments
Post a Comment