【发布时间】:2018-10-08 15:52:52
【问题描述】:
这是我第一次在 Nginx 工作。我知道为了轻松连接 Django 和 Nginx,我需要 uWSGI。
设置
uWSGI:
我认为我的 uWSGI 设置完全没问题,我用它在 HTTP 端口 80 上运行我的 Django 网站:
uwsgi --http :80 --root /root/Env/firstsite --chdir /root/mysite -w mysite.wsgi
但是由于这对于“最佳”安全性来说不是一个非常实用的方法,所以我不得不使用 Nginx。在这种情况下,我使用了“uWSGI Emperor”模式,然后我创建了一个新文件/etc/uwsgi/sites/mysite.ini:
[uwsgi]
project = mysite
base = /root
chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = 5
socket = %(base)/%(project)/%(project).sock
chmod-socket = 666
vacuum = true
在这种情况下,我使用的不是 HTTP 端口,而是使用 uwsgi 协议的 unix 套接字。
最后,我创建了/etc/systemd/system/uwsgi.service 文件(据我了解,它会在启动时启动 uwsgi):
[Unit]
Description=uWSGI Emperor service
After=syslog.target
[Service]
ExecStart=/usr/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
Nginx:
我的 Nginx 设置非常简单,我刚刚修改了现有的服务器块以适应 uWSGI 配置:
server {
listen 80;
server_name mysite.com www.mysite.com;
location = favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/mysite;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/mysite.sock;
}
}
启动:
sudo systemctl start nginx
sudo systemctl start uwsgi
sudo systemctl enable nginx
sudo systemctl enable uwsgi
这个过程不会输出任何错误。
问题:
Webserver 返回 Nginx 的默认 html 响应 - 当我希望它返回 Django 的默认 html 响应时。这有什么原因吗?我觉得我在这里遇到了一个非常简单的问题,因为我在 stackoverflow 上的任何地方都找不到它。
可能的原因:
/run/uwsgi/mysite.sock在 nginx 配置中由于某种原因不存在,这让我认为这可能是原因。在
systemctl status nginx中有一个错误Failed to read PID from file /run/nginx.pid: Invalid argument应该是导致上述问题的原因? (systemctl status wsgi没有错误)。从上面的问题来看,我认为这些问题是我的Nginx配置造成的,这样对吗?
-
它指向
李>50x.html,用于错误 500、502、503、504(在这种情况下是 HTTP 502,网关错误 - 这与配置有关)。
主要原因:
在systemctl status nginx 中有一个错误Failed to read PID from file /run/nginx.pid: Invalid argument 应该是导致上述问题的原因? (systemctl status wsgi 没有错误)。
检查了this problem & solution,但它不起作用。
请确保/run/uwsgi/目录为空,可能是这个原因。
谢谢!
【问题讨论】:
-
可能在某个地方 nginx 仍指向其默认值。检查是否定义了任何服务器块。
-
django 设置是否在您的 ALLOWED_HOSTS 列表中有“mysite.com”和“www.mysite.com”。
-
@HashSplat 是的
mysite.com在 ALLOWED_HOSTS 数组中。 -
@NishanthSpShetty 我不这么认为,我检查了一下,只有一个服务器块。顺便返回'50x.html`,可能是服务器内部错误。
-
假设您在进行更改后重新加载或重启了 nginx