Multiple Django Project + Nginx on subpath - django

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.

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

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.

Related

Moving a Django app into production using Nginx and Gunicorn, there are no errors but the page doesn't show

I'm trying to setup an app on my Digital Ocean production server, I've followed these instructions, testing gunicorn and nginx,I could access the app in gunicorn and both services start fine with no errors logged. However when I go to the site it does not show anything. This is a subdomain of my main site. Mostly I'm looking to find a place to start troubleshooting this especially since everything looks fine.
Configs:
Nginx config for subdomain (Django) site:
server {
# use 'listen 80 deferred;' for Linux
# use 'listen 80 accept_filter=httpready;' for FreeBSD
listen 80;
# set the correct host(s) for your site
server_name subdomain.domain_name.com www.subdomain.domain_name.com;
location = /favicon.ico {access_log off; log_not_found off;}
location /static/ {
root /path/to/static/files;
}
location / {
include proxy_params;
proxy_pass http://unix:/path/to/.sock/file;
}
}
Nginx config for main (static) site:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/main_site_folder;
index index.html index.htm index.nginx-debian.html;
server_name domain_name www.domain_name;
location / {
try_files $uri $uri/ =404;
}
}
[Unit]
Description=Description of the app
After=network.target
[Service]
User=MyUserName
Group=www-data
WorkingDirectory=/var/www/app_directory/
ExecStart=/path/to/venv/and/gunicorn --access-logfile - --workers 3 --bind unix:/var/www/app_dir/.sock_filename app_name.wsgi:application
[Install]
WantedBy=multi-user.target
You could start by changing ALLOWED_HOSTS = ["*"] in settings.py. Also try accessing your URL through CURL.
Solved by adding an A record for the sub-domain 🙄, this was a classic case of not being able to find the answer because it was right in front of my face. 😅

Unable to serve static file in Django deployed to production in Digital ocean

Currently I deployed my django app in Digital ocean droplet. In localhost it works well but it cant serve js/css files in static folder when deployed to prod. Here are configs:
server {
server_name keywordprocessor.prodsite.com www.keywordprocessor.prodsite.com>
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/projects/backend/crawler;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
...
}
BY digital oceans default, the project resides inside root directory
`cd projects` `pwd` returns /root/projects/
Settings
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = "/static/"
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR, "/")
THis is how the project folder looks like
backend/
crawler/
static/
templates
.gitignore
requirements.txt
/etc/systemd/service/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/root/projects/backend/crawler
ExecStart=/usr/local/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
crawler.wsgi:application
[Install]
WantedBy=multi-user.target
All js and css files cant be served
`Failed to load resource: the server responded with a status of 404 (Not Found)`
It does load the page but css messed up. I did some googling for possible solutions, nothing works for me.
copy your static files to /var/www/static and change your nginx conf
1.Copy command in project directory cp -r ./static /var/www/static/
2.Permission to static folder which is in /var/www/static/command
sudo chmod 755 static
3.Change your nginx.conf like
server {
listen 80;
server_name Your-IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /var/www/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
4.Do this commands for nginx and gunicorn
a) sudo nginx -t
b) sudo systemctl restart nginx
c) sudo systemctl restart gunicorn
5.If you will get any error check logs using sudo tail -F /var/log/nginx/error.log and try again.

psycopg2 NotNullViolation on column "ip" for Django/Gunicorn/Nginx system

I have a Django system that has gunicorn hosting the dynamic content and nginx the static, nginx passing through to gunicorn as required via a Unix socket.
Every request that requires static content is generating an error like that below. Is this because the request comes from a Unix socket and not via IP? How do I fix this? A Quick "Google" hasn't helped but perhaps I've not found the right question yet :-(.
django.db.utils.IntegrityError: null value in column "ip" violates not-null constraint
DETAIL: Failing row contains (48, 302, GET, /, 2022-05-25 07:51:28.855717+00, f, f, null, , Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KH..., en-GB,en;q=0.9,en-US;q=0.8,mt;q=0.7, null).
Thanks for any suggestions.
Here is the configuration of NGINX using a Gunicorn socket:
server {
listen 80;
server_name YOUR_IP_OR_DOMAIN;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root WHERE YOUR STATIC FOLDER IS, DO NOT INCLUDE STATIC FOLDER IN THIS PATH;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock; <<<<<<====== HERE IS YOUR SOCKET FILE, I WILL EXPLAIN ITS CONTENT BELOW
}
client_max_body_size 100M; <= this is just something I added to not have timeout issues if the user uploads large files, you don't have to have this line
}
Gunicorn:
sudo nano /etc/systemd/system/gunicorn.service:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myproject
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/sammy/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
After saving the service file:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
You can check the socket working by typing : sudo systemctl status gunicorn

nginx 404 on root/homepage with Django + Gunicorn

I'm migrating my website from one host to another. The migration has been mostly 99% successful except for the fact that whenever I navigate to the home page of the site, I get nginx's 404 page. Every other page, static & media file is rendered properly. I've wracked my brain trying to find a solution but I haven't found any, let alone someone having a similar issue (instead, others have a working home page but 404 on all others).
I have two different domains (one that my company owns, the other is owned by the city we're in). I've updated the DNS on the domain I own to ensure it's working with the new host and it is. When I navigate using the host (example.com) or the server's IP address, the website loads all pages correctly - except the homepage, which - again - returns nginx's 404. All other 404 errors display the 404 page I've set up via Django.
Whenever this issue crops up, the gunicorn error log adds a line stating that the service is 'Booting worker with pid: ...'
nginx.conf --> save for the paths and port, this conf is identical to the conf running on my current host
http {
client_max_body_size 30M;
open_file_cache max=1000 inactive=300s;
open_file_cache_valid 360s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
upstream cgac_server {
server unix:/root/cgac/cinema_backend/cinema_backend.sock fail_timeout=0;
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80 default_server;
location /static/ {
expires 365d;
root /root/cgac/cinema_backend/static/;
}
location /media/ {
expires 365d;
root /root/cgac/cinema_backend/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://cgac_server;
break;
}
}
}
gunicorn.service --> gunicorn is setup differently from the active server but the parameters in this file are identical, and it's working.
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/root/cgac/cinema_backend
ExecStart=/root/cgac-venv/bin/gunicorn --access-logfile /root/logs/gunicorn-access.log --error-logfile /root/logs/gunicorn-error.log --workers 3 --bind unix:/root/cgac/cinema_backend/cinema_backend.sock cinema_backend.wsgi:application
[Install]
WantedBy=multi-user.target
Django URL patterns --> also 100% identical to the working site
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)), # NOQA
url(r'^fobi/', include('fobi.urls.view')),
url(r'^fobi/', include('fobi.urls.edit')),
url(r'^captcha/', include('captcha.urls')),
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap',
{'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^select2/', include('django_select2.urls')),
url(r'^', include('cms.urls')),
)
many option are there to fix it
put your upstream over http {}
reload your daemon
restart your gunicorn.service
your cinema_backend must be /run/cinema_backend.sock(what is normal)
try to use different config like site-availabe
The issue had absolutely nothing to do with the nginx or gunicorn configuration. It turns out that an image in a carousel on the home page somehow got corrupted or something and it was driving Django crazy. I deleted the entry in the CMS which fixed the issue, then proceeded to reupload the file and it's all good.

Django+gunicorn+nginx site runs perfect when using example.com:8000 but not when using example.com

I have a site on Digital Ocean VPS.
But I get a bad gateway when accessing example.com but not when example.com:8000.
Also when visiting the django-admin at example.com:8000/admin the default formatting is not coming. I guess nginx is not being able to serve the static content.
Below is the service file for gunicorn:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
Group=www-data
WorkingDirectory=/FINlit
ExecStart=/FINlit/venvforfinlit/bin/gunicorn --access-logfile - --workers 3 --bind unix:/FINlit/Finlit.sock Finlit.wsgi:application
[Install]
WantedBy=multi-user.target
and the nginx config:
server {
listen 80;
server_name my_ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /FINlit;
}
location / {
include proxy_params;
proxy_pass http://unix:/FINlit/Finlit.sock;
}
}
Thanks
After almost searching for a couple of hours, my instincts led me to replace the IP with the domain name in the nginx config and now it works fine.
Actually I don't know, there might be a possibility that something else did the job for me but anyways.