子路径上的多个 Django 项目 + Nginx



我正在尝试运行用 Django 编写的多个仪表板以在我的服务器上运行,但没有启动并运行它。按照这个数字海洋教程并根据这个 SO 答案对其进行了修改。现在一切都已启动并运行,但是当我指向我的URL时,它会显示Nginx欢迎页面http://ipaddr/first_dashboard

以下是gunicorn_fdab.socket文件:

[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn_fdab.sock
[Install]
WantedBy=sockets.target

以下是gunicorn_fdab.service文件:

[Unit]
Description=gunicorn daemon for fdab
Requires= gunicorn_fdab.socket
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/opt/fdab
ExecStart=/opt/anaconda/envs/fdab/bin/gunicorn 
--access-logfile - 
--workers 3 
--bind unix:/run/gunicorn_fdab.sock 
fdab.wsgi:application
[Install]
WantedBy=multi-user.target

现在这是我的 Nginx conf 文件:

server {
listen 80;
server_name 111.11.11.111;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /opt/fdab/fdab;
}
location /fdab {
include proxy_params;
rewrite /fdab(.*) $1;
proxy_pass http://unix:/run/gunicorn_fdab.sock;
}
}

我无法理解我哪里做错了。

如果我做curl --unix-socket /run/gunicorn_fdab.sock localhost,它只是什么也没返回。

(base) root@virtualserver01:~# curl --unix-socket /run/gunicorn_fdab.sock localhost
(base) root@virtualserver01:~# 

项目存储在/opt/fdab.

附加信息:

基本上,我对这两个项目的项目结构是这样的:

/opt/fdab
/fdab
/fdab_dashboard

/opt/pdab
/pdab
/pdab_dashboard

该项目的结构是这样的,因为我打算在 fbad 和 fdab2(第二个项目名称)中有多个应用程序。

编辑

更新了 Nginx 的 conf 文件:

server {
listen 80;
server_name 111.11.11.111;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /opt/fdab/fdab;
}
location /fdab {
include proxy_params;
rewrite /fdab/(.*) /$1 break;
proxy_pass http://unix:/run/gunicorn_fbad.sock;
}

location /pdab/static/ {
alias /opt/pdab/pdab/static/;
}
location /pdab {
include proxy_params;
rewrite /pdab/(.*) /$1 break;
proxy_pass http://unix:/run/gunicorn_pdab.sock;
}
}

现在我已经在两个项目中都添加了FORCE_SCRIPT_NAME = '/exampleproject'

现在发生的事情是,如果我输入,http://<ipaddr>/fdab/fdab_dashboard它工作正常,但如果我在http://<ipaddr>/fdab/http://<ipaddr>/pdab/输入,我被重定向到http://<ipaddr>/fdab_dashboardhttp://<ipaddr>/pdab_dashboard,这不是必需的,而且,http://<ipaddr>/fdab_dashboard似乎工作正常。但是网址的fdab部分丢失了,一旦我登录后进入应用程序,网址似乎很好,可能是因为FORCE_SCRIPT_NAME = '/fdab',但网址http://<ipaddr>/pdab_dashboard给了我404 error页面。

所以好消息是你发布的 gunicorn 和 nginx 配置看起来是正确的。

(1) 问题 #1 默认网页显示:

这几乎总是由默认的nginx配置文件default.conf引起的。 只需删除该文件,您应该会看到您的网站弹出。 唯一要检查的另一件事是测试并重新加载nginx,以确保您的配置有效并已加载:

sudo nginx -t
sudo systemctl reload nginx

(2) 问题 #2 卷曲到 unix 套接字没有返回您所期望的内容。 curl 命令看起来略有不对劲:尝试如下操作:

curl -v --no-buffer --unix-socket /run/gunicorn_fdab.sock http://localhost/route/available/in/django

您可以在尾随枪角原木时将卷发与journalctl --since today -u gunicorn -f配对

我建议你尽量不要在nginx配置中进行任何URL重写。 根据需要对套接字进行proxy_pass,然后调整您的 Django URL 配置以匹配您要在不同应用程序中使用的 URL。

最新更新