子路径上有多个Django Project + Nginx

问题描述 投票:2回答:2

我正在尝试运行用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 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页面。

django nginx gunicorn
2个回答
0
投票

所以好消息是你的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尾随gunicorn原木


0
投票

我建议你尝试不在nginx配置中进行任何URL重写。根据需要对代理插件执行proxy_pass,然后调整Django URL配置以匹配您要在不同应用程序中使用的URL。

© www.soinside.com 2019 - 2024. All rights reserved.