当我访问用gunicorn和nginx服务的django时,我得到了404错误。

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

我有一个Angular应用,它在 https:/xxx.xxx.xxx.xxx.xxx。 在代理服务器(Nginx)后面提供服务。

在同一台服务器上,我托管了我的后台(django),当我试图用域名-ipapiusers访问任何 "API "服务时,我得到的是

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

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

  1. 下面是我的etcnginxsites-availabledefault的情况

    server { listen 80 default_server; listen [::]: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个回答
0
投票

由于某些原因,我的问题是这样的。

在开发环境中,我的django端点是通过以下方式访问的

localhost:8000/cars/

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

在我的nginx配置文件中,我希望位置区块是

location /api/ {
...
}

将只从domain.comapi获取请求并将其发送到我的django应用程序。

但我的django应用返回的是domain.comcars而不是domainapicars。

答:我的答案是:我必须在我的尿液中添加'api'。

我必须把'api'添加到我的urls中,这样我的后台服务器(django)就会返回responds为

localhost:8000/api/resources

而非

localhost:8000/resources
© www.soinside.com 2019 - 2024. All rights reserved.