Zag Gol
Zag Gol

Reputation: 1076

CORS Error - running flak application with Nginx and uWSGI

I am running a Flask app. from the code, it runs great. But adding Nginx and uWSG, as the uWSGI starts the application, I get errors that seem to be CORS, (But from Postman all requests run OK)

besides the front dev-tools, the only place with log errors, are in the /etc/nginx/error.log:

2024/05/10 16:23:04 [error] 61984#61984: *8 upstream prematurely closed connection while reading response header from upstream, client: <CLIENT IP>, server: <SERVER IP>, request: "GET /socket.io/?EIO=4&transport=polling&t=OzZK8z3 HTTP/1.1", upstream: "http://unix://home/ubuntu/projects/tabular-wizard-server/app/config/sockets/uwsgi.sock:/socket.io/?EIO=4&transport=polling&t=OzZK8z3", host: "54.224.159.192:8080", referrer: "http://localhost:3000/"

No errors in the uWSGI or app (the request doesn't get to them)

uwsgi.ini:

[uwsgi]
module = run:app
master = true
processes = 4
socket = <MY PATH>/uwsgi.sock
protocol = uwsgi
vacuum = true
buffer-size = 32768
enable-threads = true
logto = /var/log/uwsgi/app.log

nginx.conf:

user ubuntu;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;
events {
    worker_connections 1024;
}

http {
    server {
        listen 8080;
        server_name <SERVER IP>;

        location / {
            
            include uwsgi_params;
            uwsgi_pass unix://<MY PATH>/uwsgi.sock;
            proxy_read_timeout 180s;
            uwsgi_read_timeout 180s;
            # Set CORS Headers
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;

            # Handle Preflight Requests (OPTIONS)
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                add_header 'Content-Length' 0;
                return 204;
            }
        }

        location /socket.io {
            proxy_http_version 1.1;
            proxy_buffering off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 180s;
            uwsgi_read_timeout 180s;
            proxy_pass http://unix://<MY PATH>/uwsgi.sock:/socket.io;

            # Set CORS Headers for WebSocket Connections
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;

            # Handle Preflight Requests (OPTIONS)
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                add_header 'Content-Length' 0;
                return 204;
            }
        }
    }
}

Thanks

Upvotes: 0

Views: 53

Answers (0)

Related Questions