2

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.

Lakshan Dissanayake
  • 521
  • 1
  • 4
  • 18
driftking9987
  • 1,673
  • 1
  • 32
  • 63
  • Do you have your conf file in sites-enabled folder? – Ozgur Akcali Apr 11 '19 at 19:15
  • I have `nginx.conf` in `/etc/nginx` folder. in sites-enables I have `dashboard` and `default` files. I just checked it by removing the `rewrite` directive and putting the location as just `/` and it Django app was running. So I think its an issue with Nginx configuration only. – driftking9987 Apr 11 '19 at 19:18
  • 1
    As far as I can see, in the /fdab block, you are redirecting the request by removing the /fdab parrt of the path, but you do not have a location block for /. So when a request arrives like this: /fdab/abc, it will be redirected to /abc, but here is no location block handling that – Ozgur Akcali Apr 11 '19 at 19:35
  • I don't have much expertise in Nginx conf. I tried the above mentioned SO answer and also one of the comment in the DigitalOcean tutorial, talks about the same. Do you have any idea as to how it can be achieved? – driftking9987 Apr 11 '19 at 19:39
  • Can you tey changing "location /fdab" to "location /", and keeping the rewrite? – Ozgur Akcali Apr 11 '19 at 19:40
  • I did that, but it's basically opening my dashboard directly. It's like if am trying `http://111.11.11.111/fdab/`, it's redirecting to `http://111.11.11.111/fdab_dashboard/`, what I want is, if I do `http://111.11.11.111/fdab/`, it should open `http://111.11.11.111/fdab/fdab_dashboard/`. – driftking9987 Apr 11 '19 at 19:46
  • As @Ozgur pointed out, your current configuration redirects `/fdab` to the Nginx welcome page. You should use `rewrite...break` to change the URI before passing it to the Gunicorn socket. – Richard Smith Apr 13 '19 at 07:56
  • Am fiddling with different conf but nothing is working out for me. Can you please elaborate a little bit? Thanks. – driftking9987 Apr 13 '19 at 07:58
  • You can also use approach with [`settings.FORCE_SCRIPT_NAME`](https://stackoverflow.com/a/53795805/952437) And as mentioned by others you should use `break` or `last` at the end of the rewrite rule – imposeren Apr 18 '19 at 02:36

2 Answers2

0

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

2ps
  • 15,099
  • 2
  • 27
  • 47
  • I deleted the `default` conf file from `site-enabled` and `sites-available`, resorted Nginx, checked for any symlinks , but still it gives me the default config page. Even deleted the cookies and etc, just in case I was getting a cached page, but it was not. – driftking9987 Apr 12 '19 at 08:25
0

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.

Mark Bailey
  • 1,617
  • 1
  • 7
  • 13