![](/img/trans.png)
[英]Gunicorn not loading react frontend in django react nginx configuration digitalocean
[英]Configuring Nginx and Gunicorn for DRF, Django and React Frontend
我有一個應用程序,其前端在 React 中,后端在 Django 中,前端使用 DRF 開發 API。 現在我使用 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 由於Host
標頭不匹配而拒絕帶有 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/;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.