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.
Related
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
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/;
}
I have django+gunicorn+nginx running on 192.168.1.81:3000. The web app will not serve any static files; it returns a 404 error. This suggests that there is a problem with the nginx virtual server config file. I have tried several solutions offered on stack overflow with no success. What is wrong with the nginx virtual server file?
upstream app_server {
server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.81;
client_max_body_size 4G;
access_log /home/pi/door_site/logs/nginx-access.log;
error_log /home/pi/door_site/logs/nginx-error.log;
location /static/ {
alias /home/pi/door_site/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://app_server;
break;
}
}
}
In Nginx virtual server conf file inside server section you need to add to location sections for Nginx can serve files in /static/ and /media/ folders:
location /media {
alias /path/to/your/folder/media;
}
location /static {
alias /path/to/your/folder/static;
}
After that test Nginx configuration:
sudo nginx -t
and reload Nginx:
sudo nginx -s reload
(or restart - sudo /etc/init.d/nginx restart )
try this config using server root and try_files
upstream app_server {
server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.81;
client_max_body_size 4G;
access_log /home/pi/door_site/logs/nginx-access.log;
error_log /home/pi/door_site/logs/nginx-error.log;
root /path/to/root # place your static directories in root i.e /path/to/root/static
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
This will try to find your static file and then move onto your app server.
Make sure nginx is running as the right user to access your files and that your static files permissions are correctly set perhaps with:
chmod -R u=rwX,g=rwX,o=rX static_dir
I my case it was a permission issue on static directory and it worked after assigning proper permissions.
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
I am trying to setup a django production server on OSX (Mavericks).
This is my nginx server config file:
server {
listen 80;
server_name localhost;
error_log /pathtoerrorlog;
access_log /pathtoaccesslog;
# serve static files
location ~ ^/static/ {
root /Users/Hello/assets;
expires 30d;
}
# serve media files ~ ^
location ~ ^/media/ {
root /Users/Hello/assets;
expires 30d;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8000;
}
}
This is the nginx.conf config file
user www-data;
worker_processes 4;
error_log /var/log/nginx/error.log;
events {
worker_connections 4092;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
gzip on;
gzip_disable "msie6";
include /etc/nginx/sites-enabled/*;
}
I have created a user called www-data on the system and gave the folllowing access permissions:
sudo chown -R www-data:www-data /usr/local/etc/nginx
sudo chown -R www-data:www-data /etc/nginx
sudo chown -R www-data:www-data /var/log/nginx
I started gunicorn without any errors and so is the case with nginx too.
In a browser, localhost redirects me to the django app but static media is not displayed. This is a sample error (out of all for static content) I get, as I see in nginx error log:
2014/01/25 20:16:23 [error] 35068#0: *68 open() "/Users/Hello/assets/static/css/base.css" failed (13: Permission denied),
client: 127.0.0.1, server: localhost, request: "GET /static/css/base.css HTTP/1.1", host: "localhost", referrer: "http://localhost/"
I tried changing the permissions of /Users/Hello/assets using sudo chown -R www-data:www-data assets but it didn't help. -R 777 also didn't work.
Please suggest where I am going wrong. Thanks!
It turns out to be that read permissions have to be granted for all parent directories of /Users/Hello/assets. So, I set read access to all of Users, Hello, assets and not just assets as earlier.
Related articles are :
Nginx 403 forbidden for all files
http://nginxlibrary.com/403-forbidden-error/