在使用nginx的Linux计算机上部署Flask和React应用程序时出现cors错误

u4dcyp6a  于 2023-01-29  发布在  Nginx
关注(0)|答案(1)|浏览(146)

我已经开发了这个应用程序与 flask 作为后端和Reactjs作为前端.我的文件夹结构:

mydomain/
    /server
        __init__.py
        ...
    /client (react)
        /dist
            index.html
            /assets (contains js and css)
    /venv

我的nginx配置:

server {
        server_name <mydomain> www.<mydomain>;
        root /home/sabbir/<mydomain>/client/dist/;
        index index.html;

        location / {
                try_files $uri $uri/ =404;
        }
    location /api {
            include proxy_params;
            proxy_pass http://localhost:5000;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/<mydomain>/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/<mydomain>/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



}
server {
    if ($host = www.<mydomain>) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

当我尝试注册一个用户时,它给出这个错误:同源策略不允许读取位于http://localhost:5000/auth/login的远程资源。(原因:CORS请求未成功)。状态代码:(无)
任何帮助都将不胜感激。

vvppvyoh

vvppvyoh1#

您可以尝试安装flank-cors:pip install flask-cors,然后将其应用于您的应用,如下所示:

from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app)

相关问题