Nginx 无法通过 gunicorn 代理 django 应用程序
Nginx unable to proxy django application through gunicorn
我正在尝试使用 Nginx 和 Gunicorn 在 Ubuntu 服务器 18.04 中部署 Django 应用程序。至少从日志和状态的角度来看,每个工具似乎都能正常工作。
关键是,如果我使用 SSH 登录我的服务器并尝试使用 curl,gunicorn 能够看到请求并处理它。但是,如果我直接写我的IP,我得到的只是典型的Welcome to nginx home page 并且请求对于gunicorn是完全不可见的,所以nginx似乎无法定位并传递请求到 gunicorn 插座。
我正在使用 nginx 1.14.0
、Django 2.2.1
、Python 3.6.7
、gunicorn 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 存储库(但是,它已经过测试并且代码可以正常工作)
我期待 Django 管理员可以从我的浏览器访问已经有了这个配置,但事实并非如此。我尝试从我的浏览器访问我的 IP,我得到欢迎使用 nginx,但如果我访问 /admin
,也会得到 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 服务器,即 returns "welcome to 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
例如。
终于,我让它正常工作了。你对 nginx 的配置是正确的,虽然我真正的问题 不是 delete/modify 默认配置文件 用于 sites_enabled
文件夹中的 nginx。因此,当我设置 listen 80 default_server
时,出现以下错误
[emerg] 10619#0: a duplicate default server for 0.0.0.0:80 in
/etc/nginx/sites-enabled/mysite.com:4
无论如何,我对静态文件有问题,我仍然不知道为什么它会那样工作。我需要设置 DEBUG = True
才能看到管理模块的静态文件。
我将继续研究在生产环境中为管理面板提供静态文件的正确方法。
非常感谢您的帮助!
我正在尝试使用 Nginx 和 Gunicorn 在 Ubuntu 服务器 18.04 中部署 Django 应用程序。至少从日志和状态的角度来看,每个工具似乎都能正常工作。
关键是,如果我使用 SSH 登录我的服务器并尝试使用 curl,gunicorn 能够看到请求并处理它。但是,如果我直接写我的IP,我得到的只是典型的Welcome to nginx home page 并且请求对于gunicorn是完全不可见的,所以nginx似乎无法定位并传递请求到 gunicorn 插座。
我正在使用 nginx 1.14.0
、Django 2.2.1
、Python 3.6.7
、gunicorn 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 存储库(但是,它已经过测试并且代码可以正常工作)
我期待 Django 管理员可以从我的浏览器访问已经有了这个配置,但事实并非如此。我尝试从我的浏览器访问我的 IP,我得到欢迎使用 nginx,但如果我访问 /admin
,也会得到 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 服务器,即 returns "welcome to 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
例如。
终于,我让它正常工作了。你对 nginx 的配置是正确的,虽然我真正的问题 不是 delete/modify 默认配置文件 用于 sites_enabled
文件夹中的 nginx。因此,当我设置 listen 80 default_server
时,出现以下错误
[emerg] 10619#0: a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/mysite.com:4
无论如何,我对静态文件有问题,我仍然不知道为什么它会那样工作。我需要设置 DEBUG = True
才能看到管理模块的静态文件。
我将继续研究在生产环境中为管理面板提供静态文件的正确方法。
非常感谢您的帮助!