【问题标题】:Multiple Django Project + Nginx on subpath子路径上的多个 Django Project + Nginx
【发布时间】:2019-09-02 11:18:34
【问题描述】:

我正在尝试运行多个用 Django 编写的仪表板以在我的服务器上运行,但没有启动并运行它。关注this digital ocean tutorial,根据this SO answer修改。现在一切都已启动并运行,但是当我指向我的 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 配置文件:

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 似乎工作正常。但是 url 的 fdab 部分丢失了,一旦我登录后进入应用程序,url 似乎很好,可能是因为 FORCE_SCRIPT_NAME = '/fdab' ,但是 url http://<ipaddr>/pdab_dashboard 给了我404 error 页面。

【问题讨论】:

  • 您的 conf 文件是否在启用站点的文件夹中?
  • 我在/etc/nginx 文件夹中有nginx.conf。在站点启用中,我有 dashboarddefault 文件。我刚刚通过删除rewrite 指令并将位置设置为/ 来检查它,并且Django 应用程序正在运行。所以我认为这只是 Nginx 配置的问题。
  • 据我所知,在 /fdab 块中,您通过删除路径的 /fdab 部分来重定向请求,但您没有 / 的位置块。所以当一个请求像这样到达时:/fdab/abc,它将被重定向到/abc,但是这里没有位置块处理
  • 我在 Nginx conf 方面没有太多专业知识。我尝试了上面提到的 SO 答案以及 DigitalOcean 教程中的评论之一,也谈到了同样的问题。您知道如何实现吗?
  • 你能把“location /fdab”改成“location /”,并保持重写吗?

标签: django nginx gunicorn


【解决方案1】:

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

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

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

sudo nginx -t
sudo systemctl reload nginx

(2) 问题 #2 curl 到 unix 套接字没有返回您期望的结果。 curl 命令看起来有点不对劲:尝试类似:

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

您可以在使用 journalctl --since today -u gunicorn -f 跟踪 gunicorn 原木时配对该卷曲

【讨论】:

  • 我从site-enabledsites-available 中删除了default conf 文件,使用了Nginx,检查了任何符号链接,但它仍然给了我默认的配置页面。甚至删除了 cookie 等,以防万一我得到一个缓存页面,但事实并非如此。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 2022-10-25
    • 2017-11-30
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 2012-07-02
    相关资源
    最近更新 更多