Hi everyone I am relatively new to deploying websites with Django. I’m in the process of deploying my 2nd website on the same server as my 1st website. I’m using this tutorial: https://www.youtube.com/watch?v=Td3lirXIeRI and they show you how to use the following setup:
- Django
- Gunicorn
- Nginx
- Supervisor
My first website works perfectly fine. The static files are served and the user does not receive any 404’s in the console. I set up my 2nd website the exact same way as my first, and the user receives 404’s for the static files.
Here is some of my code for both sites:
site1.settings.py
STATIC_URL = 'static/'
MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'site1/static/')
]
DEBUG = False
ALLOWED_HOSTS = ['www.site1.com', 'site1.com']
CSRF_TRUSTED_ORIGINS = ['https://site1.com']
site1-nginx.conf
upstream site1_app_server {
server unix:/webapps/site1/run/gunicorn.sock fail_timeout=0;
}
server {
server_name site1.com www.site1.com;
access_log /webapps/site1/logs/access.log;
location /static/ {
alias /webapps/site1/static/;
}
location /media/{
alias /webapps/site1/media;
}
location / {
proxy_pass http://127.0.0.1:8000;
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://site1_app_server;
}
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.site1.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.site1.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = site1.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name site1.com;
return 404; # managed by Certbot
}
site2 settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'site2/static/')
]
STATIC_ROOT = BASE_DIR / "staticfiles"
DEBUG = False
ALLOWED_HOSTS = ['*']
CSRF_TRUSTED_ORIGINS = ['https://site2.com']
site2-ngnix.conf
pstream site2_app_server {
server unix:/webapps/site2/run/gunicorn.sock fail_timeout=0;
}
server {
server_name site2.com www.site2.com;
access_log /webapps/site2/logs/access.log;
error_log /webapps/site2/logs/error.log;
location /static/ {
autoindex on;
alias /webapps/site2/static/;
}
location /media/{
alias /webapps/site2/media;
}
location / {
proxy_pass http://127.0.0.1:8000;
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://site2_app_server;
}
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.site1.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.site1.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.site2.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name site2.com www.site2.com;
return 404; # managed by Certbot
}
Let me know if you need more information. Any help would be much appreciated.
Thank you.