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

Configuring Nginx and Gunicorn for DRF, Django and React Frontend

我有一个应用程序,其前端在 React 中,后端在 Django 中,API 的前端使用 DRF 开发。现在我将 Nginx 与 Gunicorn 一起用作 Web 服务器。以下是我在 sites-available 中的 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 请求-end.Do 我们需要定义路径nginx文件中的API,如果是,格式是什么?

我怀疑问题是因为 mis-matching Host header,Django 拒绝了具有 400 响应的 API 请求。这些 ajax 请求可能正在使用 127.0.0.1 作为它们的 Host header,而您正在使用 localhost.

启动主页

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

location /nokia-sdn/api/v1/ {
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:8000/nokia-sdn/api/v1/;
}