为DRF,Django和React前端配置Nginx和Gunicorn

问题描述 投票:0回答:1

我有一个应用程序,其前端在React中,后端在Django中,API使用DRF为前端开发。现在我使用Nginx作为Web服务器和Gunicorn。以下是我在站点中的Nginx conf文件 - 可用:

server {
    listen 8000;
    server_name localhost;


 location = /favicon.ico { access_log off; log_not_found off; }
 location /static/ {
    root /home/nokia-ui/build/;
}

location /nokia-sdn/api/v1/ {
    proxy_pass http://127.0.0.1:8000/nokia-sdn/api/v1/;

}

location / {
proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://unix:/home/nokia-sdn/nokia.sock;
}

}

现在当我访问http://localhost:8000/时,我得到了登录页面,但是我无法向后端发出任何api请求。我们需要在nginx文件中定义API的路径,如果是这样,格式是什么?

django nginx django-rest-framework gunicorn
1个回答
1
投票

我怀疑问题是由于Host标头不匹配,Django拒绝了400响应的API请求。那些ajax请求可能使用127.0.0.1作为他们的Host标题,而你使用localhost启动主页面。

您应该告诉Nginx在/nokia-sdn/api/v1/的位置配置中设置标头 - 例如:

location /nokia-sdn/api/v1/ {
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:8000/nokia-sdn/api/v1/;
}
© www.soinside.com 2019 - 2024. All rights reserved.