Multiple Django Project + Nginx on subpath

ε祈祈猫儿з 提交于 2019-12-02 02:05:51

问题


I am trying to run multiple dashboards written in Django to run on my server, but am not getting it up and running. Followed this digital ocean tutorial and modified it according to this SO answer. Now everything is up and running and but when am pointing to my URL, it shows Nginx welcome page http://ipaddr/first_dashboard

Below is the gunicorn_fdab.socket file :

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn_fdab.sock

[Install]
WantedBy=sockets.target

Below is gunicorn_fdab.service file :

[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

Now this is my Nginx conf file :

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;
    }
}

Am not able to understand where am doing it wrong.

If am doing curl --unix-socket /run/gunicorn_fdab.sock localhost , it just returning nothing.

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

Project is stored at /opt/fdab.

Additional information:

Basically, my project structure for both the project is like this :

/opt/fdab
    /fdab
    /fdab_dashboard


/opt/pdab
    /pdab
    /pdab_dashboard

The structure for the project is like this because I intend to have multiple apps in fbad and fdab2(second project name.

EDIT

Updated conf file for 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 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;
    }

}

Now I have added FORCE_SCRIPT_NAME = '/exampleproject' in both the project.

Now what's happening is that, if am typing in, http://<ipaddr>/fdab/fdab_dashboard it's working fine, but if am typing in http://<ipaddr>/fdab/ or http://<ipaddr>/pdab/, am getting redirected to http://<ipaddr>/fdab_dashboard and http://<ipaddr>/pdab_dashboard , this is not what is required and moreover, http://<ipaddr>/fdab_dashboard seems to be working properly. But the fdab part of the url is missing, once I get into the app after logging in, the url seems fine, maybe because of the FORCE_SCRIPT_NAME = '/fdab' , but the url http://<ipaddr>/pdab_dashboard gives me 404 error page.


回答1:


So the good news is that your gunicorn and nginx configs as posted look correct.

(1) Problem #1 default web page shows:

This is almost always caused by the default nginx config file default.conf. Just remove that file and you should see your site popping up instead. The only other thing to check for is to test and reload nginx to make sure your configuration is valid and loaded:

sudo nginx -t
sudo systemctl reload nginx

(2) Problem #2 curl to the unix socket doesn't return what you'd expect. The curl command looks slightly off: try something like:

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

You can pair that curl while tailing the gunicorn logs with journalctl --since today -u gunicorn -f




回答2:


I suggest you try not doing any URL rewrites in the nginx config. Do the proxy_pass to the sockets as required, and then adapt your Django URL configs to match the URLs you want to use in the different apps.



来源:https://stackoverflow.com/questions/55638962/multiple-django-project-nginx-on-subpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!