gpt4 book ai didi

django - Nginx 无法通过 gunicorn 代理 django 应用程序

转载 作者:行者123 更新时间:2023-12-04 19:10:58 24 4
gpt4 key购买 nike

我正在尝试使用 Nginx 和 Gunicorn 在 Ubuntu Server 18.04 中部署 Django 应用程序。至少从日志和状态的角度来看,每个工具似乎都能正常工作。

关键是,如果我使用 SSH 登录我的服务器并尝试使用 curl,gunicorn 能够看到请求并处理它。但是,如果我直接写我的 IP,我得到的只是典型的 Welcome to nginx 主页,并且该请求对 gunicorn 完全不可见,因此似乎 nginx 无法定位并将请求传递给 gunicorn 套接字。

我正在使用 nginx 1.14.0 , Django 2.2.1 , python 3.6.7 , unicorn 19.9.0和 PostgreSQL 10.8 .

这是我的 nginx 配置

server {
listen 80;
server_name localhost;

location /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/django/myproject/myproject;
}

location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}

这些是我的 gunicorn.sock
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

和 gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=django
Group=www-data
WorkingDirectory=/home/django/myproject/myproject
ExecStart=/home/django/myproject/myproject/myproject/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
MyProject.wsgi:application

[Install]
WantedBy=multi-user.target

我一直在关注本指南( https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04 ),其中大部分都按预期工作,但不同的是我的项目不是教程中的全新项目,而是从我的 git repo 克隆的(但是,它是测试并且代码正常工作)

我期待 Django 管理员已经可以通过我的浏览器使用这个配置进行访问,但事实并非如此。我尝试从浏览器访问我的 IP,如果我访问 /admin,我会收到 Welcome to nginx,但也会收到 404。 .此外,gunicorn 日志显示没有请求。

另一方面,如果我通过 SSH 登录到我的服务器并执行 curl --unix-socket /run/gunicorn.sock localhost ,我可以在 gunicorn 日志中看到 curl 完成的请求。

欢迎任何帮助.. 我已经在这里待了几个小时,但我什至无法从服务器外部获得 1 个请求。

PD:这也与服务器中的端口无关,因为当我访问我的 IP 的根目录时,我收到了 nginx 答案。似乎 Nginx 根本没有配置。

最佳答案

在您的 nginx 配置中,您应该使用正确的 server_name 而不是 localhost

  server_name mydomain.com;

如果没有,您将回退到默认的 nginx 服务器,它会返回“欢迎使用 nginx”消息。您可以通过更改服务器顺序、删除 nginx 默认值或使用 default_server 来更改默认虚拟服务器。范围。您还可以收听多个服务器名称。
  listen      80 default_server;
server_name mydomain.com localhost;

If the Host header field does not match a server name, NGINX Plus routes the request to the default server for the port on which the request arrived. The default server is the first one listed in the nginx.conf file, unless you include the default_server parameter to the listen directive to explicitly designate a server as the default.



https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#setting-up-virtual-servers

请记住,您必须在进行更改后重新加载 nginx 配置。 sudo nginx -s reload例如。

关于django - Nginx 无法通过 gunicorn 代理 django 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56415156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com