Nginx部署的Angular前端未连接到DRF后端



我在同一个aws实例中部署了drf后端和angular前端,使用Nginx和gunicron作为drf,使用Ngin x作为angular。我使用poster测试了drf API,它运行良好,但棱角分明的前端无法连接到API,网站运行正常,但无法连接到后端API。显示

访问位于"的XMLHttpRequesthttp://3.129.126.13:8080/api/products?search=asus&page=1'来自原点'http://3.129.126.13'已被CORS策略阻止:请求的资源上不存在"Access Control Allow Origin"标头。

我已经在python应用程序设置中将前端地址列入白名单。

前端位于80,后端位于8080。

后端服务器块:

server {
listen 8080;
server_name 0.0.0.0;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/Sheradam_backend;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/Sheradam_backend/Sheradam_backend.sock;
}
}

前端的服务器块

server {
listen 80;
listen [::]:80;
server_name Sheradambd.com;
root /home/ubuntu/Sheradam_frontend/dist/SheraDam;
server_tokens off;
index index.html index.htm;
location / {
# First attempt to server request as file, then         
# as directory, then fall back to displaying a 404.          
try_files $uri $uri/ /index.html =404;
}
}

python应用程序设置:

STATIC_URL = '/static/'
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
'http://3.129.126.13',
)

解决方案位于enable-cors.org/server_nginx.html

最新更新