Static is not loading properly after cloning a Django project from git - django

Actually, I am hosting a project to a domain using a Linode server.
The Project is on Bitbucket.
I have tried all the methods static root static dirs and collect static but nothing works half of the CSS is loading but not left Static.
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR / 'static/')]
#STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Nginx Config:
server {
server_name domain.in;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/web/tst/;
}
access_log /var/log/nginx/tst-access.log;
error_log /var/log/nginx/tst-error.log;
location / {
include proxy_params;
proxy_pass http://unix:/home/web/msg/msg.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.in/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.in/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 = domain.in) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name domain.in;
return 404; # managed by Certbot
}

You have to server your static files through nginx and pass your static dir path to it:
server {
...
location /static {
alias PATH_TO_YOUR_STATIC_DIR;
}
}

Related

How can I use Nginx server blocks and Django?

I'm following the guide from this article However, when I link my home.html file in my django app's template folder, it doesn't load the css files and it doesn't understand any of the "{% %}" syntax.
How can I configure my nginx server block to load my django app properly?
My /etc/nginx/sites-available/myonlinefp.com file:
server {
root /home/stelity/myonlinefp/foodpantry/templates/;
index index.html index.htm index.nginx-debian.html home.html;
server_name myonlinefp.com www.myonlinefp.com;
location / {
try_files $uri $uri/ =404;
}
location /media {
alias /home/stelity/myonlinefp/foodpantry/media/;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myonlinefp.com/fullchain.pem; # managed by
Certbot
ssl_certificate_key /etc/letsencrypt/live/myonlinefp.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.myonlinefp.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = myonlinefp.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name myonlinefp.com www.myonlinefp.com;
}
Updated, this is the edited file for a reply below:
server {
root unix:://run/gunicorn.sock;
server_name myonlinefp.com www.myonlinefp.com;
location / {
try_files $uri $uri/ =404;
}
location /media {
alias /home/stelity/myonlinefp/foodpantry/media/;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myonlinefp.com/fullchain.pem; # managed by
Certbot
ssl_certificate_key /etc/letsencrypt/live/myonlinefp.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.myonlinefp.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = myonlinefp.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name myonlinefp.com www.myonlinefp.com; }
Here is updated nginx configuration
server {
root unix:://run/gunicorn.sock;
server_name myonlinefp.com www.myonlinefp.com;
location / {
try_files $uri $uri/ =404;
}
location /media {
alias /home/stelity/myonlinefp/foodpantry/media/;
}
location /static {
autoindex on;
alias /home/stelity/myonlinefp/foodpantry;
}
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/myonlinefp.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myonlinefp.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
if ($host = www.myonlinefp.com) {
return 301 https://$host$request_uri;
}
if ($host = myonlinefp.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
server_name myonlinefp.com www.myonlinefp.com;
}
after adding this you've to run
python manage.py collectstatic
command & make sure you've configured your static files in settings.py like this
STATIC_URL = '/static/'
STATICFILES_DIR = ['/path/to/static/dir/']
STATIC_ROOT = '/path/to/static_root/dir/'
an make sure your STATIC_ROOT must be same as your nginx location name. For more information you can check Serving Static Content

I got Bad Request (400) when tryin to login to djngo website

I have Django website hosted to DigitalOcean , i'm using postgres db , nginx , gunicorn and ubuntu 18.04. I'm using django authentication system, i created normal user, but i could logged in successfully only few times after creating the new account after that i got Bad Request (400) , and the admin was running with css style but now only html in rendered , but the style in other pages that users can visit are working fine with style. the two problems appears in the same time.
when i turn debug to true i got :
SuspiciousFileOperation at /fr/account/login/
Storage can not find an available filename for "users/2020/07/03/users/2020/07/03/users/2020/07/03/users/2020/07/03/users/2020/07/03/users/2020/07/03/no__QKvIRQ8_2N3XJKL.png". Please make sure that the corresponding file field allows sufficient "max_length".
This is my nginx configuration
Please help me
Thanks in advance
server {
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/sammy/myprojectdir;
}
location /media/ {
root /home/sammy/myprojectdir;
image_filter_buffer 10M;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
client_max_body_size 100M;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/server_domain_or_IP/fullchain.pem; # managed $
ssl_certificate_key /etc/letsencrypt/live/server_domain_or_IP/privkey.pem; # manage$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = server_domain_or_IP) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = server_domain_or_IP) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name server_domain_or_IP;
return 404; # managed by Certbot
client_max_body_size 100M;
}

nginx looking for staticfiles in the wrong place

my Django deployment with ec2, nginx and gunicorn went well beside that my staticfiles are not loading, browser show a 404 error. And this is because nginx looks in a completely different place than statics.
At this point I have tried a lot of configurations and nothing does the trick. I am wondering if a pair of fresh eyes can spot a mistake here that I do not.
/sites-enabled/django.conf:
server {
server_name site.net www.site.net;
location /static/ {
autoindex on;
alias /home/ubuntu/saas/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/saas/app.sock;
}
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/site.net/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/site.net/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.site.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = site.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name site.net www.site.net;
return 404; # managed by Certbot
}
and my django settings.py look like this and collectstatics works fine.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'staticfiles')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
I have deleted the default file but it stills seems that nginx looks for the staticfiles in the wrong place according to this error.log:
2020/05/01 19:57:45 [error] 3502#3502: *6 open() "/usr/share/nginx/html/static/css/custom.css" failed (2: No such file or directory), client: 86.221.78.105, server: site.net, request: "GET /static/cs$
I have rebooted the server, reload nginx, nothing seems to work out. Would someone has an idea of what I could try at that point?
There's usually a default.conf file that gets installed with nginx when you install with yum or apt. It will either be in /etc/nginx/conf.d/default.conf or /etc/nginx/sites-enabled/default.conf. Delete that file and reload nginx with sudo systemctl reload nginx, and you should be good to go absent there being another configuration file on your server that you don't know about.
Here is what I did to resolve the problem. Getting rid of the default index.html in /usr/share/nginx/html/ and running the conf script as:
server {
server_name site.net www.site.net;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/site.net/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/site.net/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
location /static/ {
autoindex on;
alias /home/ubuntu/saas/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/saas/app.sock;
}
}
server {
if ($host = www.site.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = site.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name site.net www.site.net;
return 404; # managed by Certbot
}
Although this seems to be a workaround it makes the trick

Can't connect to website with LetsEncrypt certificate - nginx

I'd like to add ssl certificate to my django app. I've followed tutorial so the nginx config for domain was changed but now it looks like generated certificate is incorrect.
nginx conf before certbot modifications
server {
listen 80;
listen [::]:80;
server_name doamin.com www.domain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/poul3r/doamin.com;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
and after certbot action
server {
server_name doamin.com www.doamin.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/poul3r/doamin.com;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/doamin.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/doamin.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 = doamin.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name doamin.com www.doamin.com;
return 404; # managed by Certbot
}
What I'm doing wrong or what could went wrong during letsencypt implementation ?
I've already found solution for this problem. Based info from solution I realized, there is one more app on nginx that does not have ssl certification but redirect to 443. When I changed theirs config to listen only on 80, first domain works correctly.

django - nginx - "ERR: TOO MANY REDIRECTS"

I'm having an issue with my nginx configuration.
I receive the error ERR: TOO MANY REDIRECTS
If I change the var SECURE_SSL_REDIRECT = True to False the error goes away but I believe this is causing issue with my channels setup, my websockets are unable to complete handshake.
I found this link which I think is my problem but I don't know how to fix it.
I tried changing proxy_pass http://unix... to https://unix... which causes the redirects to stop but the page won't load.
server {
server_name myproject.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/xx/myproject/static/;
}
location /static/admin/ {
alias /home/xx/myproject/static/admin/;
}
location /media/ {
alias /home/xx/myproject/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/xx/myproject/myproject.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myproject.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myproject.com/privkey.pem; # managed by Certb$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = myproject.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name myproject.com;
return 404; # managed by Certbot
}