【问题标题】:I am getting error 404 when accessing django served with gunicorn and nginx访问使用 gunicorn 和 nginx 服务的 django 时出现错误 404
【发布时间】:2020-09-03 02:53:11
【问题描述】:

我有一个加载在 https://xxx.xxx.xxx.xxx/ 上的 Angular 应用程序,在代理服务器 (Nginx) 后面提供服务。

在同一台服务器上,我托管了我的后端 (django),当我尝试使用 domain-ip/api/users 访问任何“API”服务时,我得到了

Page not found (404)
Request Method: GET
Request URL:    http://xxx.xx.xx.xxx/api/users/

当我检查 nginx 日志时,没有记录最近的日志来突出显示任何错误。

  1. 这是我的 /etc/nginx/sites-available/default

    服务器{ 听 80 default_server; 听 [::]:80 default_server;

        #root points to my angular app
        root /home/bc_sparrow/bc_s/frontend/dist;
    
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
    
        server_name 138.xx.55.xxx;
    
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.html;
        }
    
        location /api/ {
                include proxy_params;
                proxy_pass http://unix:/run/gunicorn.sock;
                #proxy_set_header Host $http_host; #adding this line made things worse
                proxy_set_header X-Forwarded-Host $server_name;
                proxy_set_header X-Real-IP $remote_addr;
        }
    

    }

使用上面的配置,当我运行这两个命令时,我没有收到错误。

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled
sudo nginx -t

当我运行 sudo systemctl status gunicorn 时,我得到了

gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-05-17 03:31:33 UTC; 29min ago
 Main PID: 14011 (gunicorn)
    Tasks: 4 (limit: 1151)
   CGroup: /system.slice/gunicorn.service
           ├─14011 /home/bc_sparrow/bc_s/backend/merchant/env/bin/python /home/bc_sparrow/bc_s/backend/merchant/env           ├─14033 /home/bc_sparrow/bc_s/backend/merchant/env/bin/python /home/bc_sparrow/bc_s/backend/merchant/env           ├─14038 /home/bc_sparrow/bc_s/backend/merchant/env/bin/python /home/bc_sparrow/bc_s/backend/merchant/env           └─14039 /home/bc_sparrow/bc_s/backend/merchant/env/bin/python /home/bc_sparrow/bc_s/backend/merchant/env
May 17 03:31:33 SparrowsV1 systemd[1]: Stopped gunicorn daemon.
May 17 03:31:33 SparrowsV1 systemd[1]: Started gunicorn daemon.
May 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14011] [INFO] Starting gunicorn 20.0.4    
May 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14011] [INFO] Listening at: unix:/run/guniMay 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14011] [INFO] Using worker: sync
May 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14033] [INFO] Booting worker with pid: 140May 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14038] [INFO] Booting worker with pid: 140May 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14039] [INFO] Booting worker with pid: 140

其他消息来源建议我编辑我的默认 nginx 配置文件,在位置 /api/ 块上没有 (http://)

proxy_pass unix:/run/gunicorn.sock;

此更改引发错误

[emerg] 14127#14127: invalid URL prefix in /etc/nginx/sites-enabled/defaul

我想我已经用尽了所有可能的链接来解决这个问题,所以欢迎任何帮助。

【问题讨论】:

    标签: django nginx gunicorn digital-ocean


    【解决方案1】:

    出于某种原因/不是我的问题。

    在开发环境中,我的 django 端点可以通过

    访问
    localhost:8000/cars/
    

    这就是我推送到我的远程服务器的内容。

    在我的 nginx 配置文件中,我预计位置会被阻止

    location /api/ {
    ...
    }
    

    只会从 domain.com/api/ 获取请求并将它们发送到我的 django 应用程序。

    但我的 django 应用返回了 domain.com/cars/ 而不是 domain/api/cars/。

    答案:

    我必须将 'api/' 附加到我的网址,以便我的后端服务器 (django) 返回/响应

    localhost:8000/api/resources
    

    而不是

    localhost:8000/resources
    

    【讨论】:

      猜你喜欢
      • 2016-10-27
      • 2021-07-20
      • 1970-01-01
      • 2018-01-24
      • 2016-06-23
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多