How to serve static file via nginx? - django

Static file can't serve via nginx. I didn't found any answer for this question so please help me to figure out.
settings.py
DEBUG = False
/etc/nginx/sites-available/example.com
upstream test {
server 127.0.0.1:8000;
keepalive 500;
}
server {
listen 80;
server_name example.com;
client_max_body_size 4G;
location /static/ {
alias /home/user/live/staticfiles/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://test;
}
}
cat /var/log/nginx/error.log shows me below output
2015/01/20 05:43:07 [error] 4480#0: *7 open() "/home/user/live/staticfiles/images/footer-logo.png" failed (13: Permission denied), client: 182.74.206.202, server: example.com, request: "GET /static/images/footer-logo.png HTTP/1.1", host: "example.com", referrer: "example.com/"
Update :
I just say it's not working ... Still getting same error i can't believe this happening because of file permission issue. I included below detail...
[myuserm#instance-4 home]$ namei -om /home/myuser/live/staticfiles/js/jquery.fancybox.js
f: /home/myuser/live/staticfiles/js/jquery.fancybox.js
dr-xr-xr-x root root /
drwxr-xr-x root root home
drwxrwxrwx myuser myuser myuser
drwxrwxrwx root root live
drwxrwxrwx myuser myuser staticfiles
drwxrwxrwx myuser myuser js
-rwxrwxrwx myuser myuser jquery.fancybox.js

First of all you check your setting for static file that should look like this setting
STATIC_ROOT = "/home/user/live/collected_static/"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
PROJECT_DIR.child("static"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
then you run ./manage.py collectstatic # this will collect all static
files to collected_static folder,
then in your nginx setting :
location /static {
alias /home/user/live/collected_static/;
}
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://test;
break;
}
}
Hope this will resolved your issue. and also check permission should we same as django project code. no need to give any extra permission.
For more setting and a very good doc you look here: Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL
For nginx permission issue you will find solution from these links:
https://serverfault.com/questions/628394/nginx-is-unable-to-open-and-thus-serve-files-despite-proper-filesystem-permiss
Nginx 403 forbidden for all files
Nginx is throwing an 403 Forbidden on Static Files

Related

Ubuntu 20.04, Django, nginx file permission error 403 -no solution in stackoverflow as of now works

I have deplloyed Django 1.8 application on ubuntu server with gunicorn
Ubuntu 20.04
Django 1.8
nginx 1.18.0
Python 2.7
The media files are having following permissions
# ls -l
total 4
drwxrwxrwx 5 www-data www-data 4096 Apr 18 2021 media
Error message I get is
2022/01/11 05:26:30 [error] 4094535#4094535: *152 open() "<full path to media>/media/cache/b3/de/b3def88cd24c122f0e1dcaf96e021ec2.jpg" failed (13: Permission denied), client: 172.68.127.176, server: server.com, request: "GET /media/cache/b3/de/b3def88cd24c122f0e1dcaf96e021ec2.jpg HTTP/1.1", host: "www.server.com", referrer: "http://www.server.com/"
the permissions for files are
media# ls -l cache/b3/de/b3def88cd24c122f0e1dcaf96e021ec2.jpg
-rwxrwxrwx 1 www-data www-data 24008 Jul 22 23:46 cache/b3/de/b3def88cd24c122f0e1dcaf96e021ec2.jpg
nginx config is
server {
server_name server.com www.server.com 192.175.118.100;
error_log /var/log/nginx/server_error_log error;
location = /favicon.ico { access_log off; log_not_found off; }
client_max_body_size 250M;
location /static/ {
autoindex on;
root /home/server/static/;
}
location /media {
autoindex on;
alias /home/server/media/media;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/server_gunicorn.sock;
proxy_set_header X-Forwarded-Protocol $scheme;
#proxy_set_header Host $host;
#proxy_set_header X-Scheme $scheme;
#proxy_set_header X-SSL-Protocol $ssl_protocol;
}
listen 80;
}
We are unable to upload files due to permission error
UPDATE
The static files works, not media files

django+gunicorn+nginx gives 404 while serving static files

django+gunicorn+nginx gives 404 while serving static files
I am trying to deploy a Django project using nginx + gunicorn + postgresql. All the configuration is done, my admin panel project static file will serve , but other static files; it returns a 404 error.(iam use run python manage.py collectstatic)
my error.log nginx :: "/blogpy/static/home/test.css" failed (2: No such file or directory)"
Structure:
blogpy
-blogpy
-config
-nginx
-nginx.conf
-docker-compose.yml
-Dockerfile
-home
-static
-home
-test.css(not working)
- requirements
-static
-templates
-.env
-docker-compose.yml
-Dockerfile
setting.py:
DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
nginx configuration:
---- nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
upstream blogpy{
server blogpy:8000;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_pass http://blogpy;
}
location /static/ {
alias /blogpy/static/;
}
}
}
Try
In ---- nginx.conf:
location /static/ {
autoindex off;
alias /home/ubuntu/blogpy/static/; #add full path of static file directry
}
To get /blogpy/home/static/ files been copied into /blogpy/static/ by collectstatic command, you need to specify STATICFILES_DIRS setting
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [
BASE_DIR / 'home' / 'static',
]
It is recommended to serve static files directly from nginx.
Add the following to your nginx site config.
location /static/ {
alias /path/to/static/directory/;
}

django serving static file with nginx

I couldn't seem to get routing to work properly. I've also included the collectstatic when i run.
In my settings.py i have the follow code
STATIC_URL = '/static/'
STATIC_ROOT = "/code/static"
in my nginx config file
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name example.org;
access_log /dev/stdout;
error_log /dev/stdout info;
location /static/ {
autoindex on;
root /code;
}
location / {
proxy_pass http://web:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
Error Log
2017/05/04 13:14:54 [error] 5#5: *1 open() "/code/static/css/bootstrap.min.css" failed (2: No such file or directory),client: 172.18.0.1, server: example.org, request: "GET /static/css/bootstrap.min.css HTTP/1.1", host: "localhost", referrer: "http://localhost/polls/"
Server Directory
root#729dc46f5760:/# cd /code/static
root#729dc46f5760:/code/static# ls -l
total 16
drwxr-xr-x 6 root root 4096 May 4 13:40 admin
drwxr-xr-x 2 root root 4096 May 4 13:40 css
drwxr-xr-x 2 root root 4096 May 4 13:40 fonts
drwxr-xr-x 2 root root 4096 May 4 13:40 scripts
root#729dc46f5760:/code/static#
Try update your nginx config file at location:static section to this
...
location /static/ {
autoindex on;
alias /code/static/;
}
...
FYI deploy django with nginx
Your STATIC_ROOT is "/code/static" but your nginx configuration maps /static/ to "/code". These should be the same.

django+nginx+uwsgi 404 on static files

I have a django app, using nginx and uwsgi to serve it. My static files return a 404. I have set ownership of the files to correspond to the nginx username. The path to the static folder was copied directly from terminal, and I ran collectstatic (the static files are successfully served via the django development server). I have also run
chown -R username:username /home
and
chmod -R ug+r /home
I have tried both alias and root.
STATIC_ROOT is in my settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
Here is my nginx.conf:
user username;
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
# Configuration containing list of application servers
upstream app_servers {
server 127.0.0.1:8080;
# server 127.0.0.1:8081;
# ..
# .
}
# Configuration for Nginx
server {
# Running port
listen 80;
# Settings to serve static files
location /static/ {
# Example:
# root /full/path/to/application/static/file/dir;
alias /home/myapp/myapp/static/;
}
# Serve a static file (ex. favico)
# outside /static directory
location = /favico.ico {
root /app/favico.ico;
}
# Proxy connections to the application servers
# app_servers
location / {
proxy_pass http://app_servers;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
It is a bad idea to chown the /home directory to some user. That directory should belong to root. Change it back to what it was:
chown root:root /home
chmod u=rwx,g=rx,o=rx /home
(The chmod you've run has probably not altered its permissions, but better be certain, therefore the second command above, which reverts it to what it was.)
You say that your project is under /home/username, but the nginx configuration says /home/myapp. Fix that. I assume it's /home/username. The /home/username needs some permissions to be fixed:
chmod o+rx /home/username
See also (my article) http://djangodeployment.com/2016/11/21/how-django-static-files-work-in-production/.

Django static files in live server using nginx

I have a live server running django, the address is http://179.188.3.54/ . As you can see, the app works but looks like the static files arent working. Also if I click in any other link, doesnt work.
This website is running without any problems in development version. Im not sure what I should do to fix this problem.
Here is my nginx config file and my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/cinegloria/cinegloria/cinegloria/static/'
PS: I tried to run collectstatic ;)
server {
root /usr/share/nginx/www;
index index.html index.htm;
access_log /var/log/nginx/domain-access.log;
server_name 0.0.0.0;
location / {
try_files $uri $uri/ /index.html;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://0.0.0.0:8000/;
}
}
Any ideas or sample code will be appreciated!
Add the static serving to the nginx conf before the / pattern:
location /static {
alias /cinegloria/cinegloria/cinegloria/static/;
}
location / {
...
}
Or set the STATIC_ROOT to the directory under the www root:
STATIC_ROOT = '/usr/share/nginx/www/static'
Or add the symlink from www root to you static dir:
$ ln -s /cinegloria/cinegloria/cinegloria/static /usr/share/nginx/www/static
Add another nginx directive for the static files. Static files should be served by nginx, not the Django server.
location /static/ {
alias /cinegloria/cinegloria/cinegloria/static/;
}
If that still doesn't work, you may need to add the mime type directive. I had to do that yesterday, because for some reason nginx wasn't serving the correct mime type when I used an alias.
As a helpful pointer, whenever you run into problems like this, take a look at your nginx error log and paste the last few lines for debugging. It is located at /var/log/nginx/error.log or a similar path.