learn_drf_with_images icon indicating copy to clipboard operation
learn_drf_with_images copied to clipboard

Static files

Open waqarsqureshi opened this issue 9 years ago • 6 comments

Hi,

I am a newbiew to Django, python,Rest Framework. I tried the example using vagrant, and after resolving minor issues, I made it working.

However, I wanted to try on my physical machine without virtual environment. I would like to ask, what difference would it be in terms of setting I have to do. I made changes in ngix. The only problem running on a pyhsical device is the server cannot find the static files on the run respite there are available.

waqarsqureshi avatar Dec 04 '15 12:12 waqarsqureshi

It's probably a permissions problem. Make sure that the user on which nginx runs has access to the static files.

abarto avatar Dec 04 '15 13:12 abarto

With Debug=True it works, so It looks like the request for admin app's static file is hitting Django app.

So it should be some bad setting in nginx

"Make sure that the user on which nginx runs has access to the static files." how?

Here is my nginx file

xyz-thinkpad.conf

upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8001; # for a web port socket (we'll use this first) }

server { listen 8000; server_name 127.0.0.1 localhost learn_drf_with_images xyz-thinkpad.local; charset utf-8;

client_max_body_size 75M; # adjust to taste

location /media { alias /home/xyz/learn_drf_with_images/learn_drf_with_images/media; autoindex off; }

location /static { alias /home/xyz/learn_drf_with_images/learn_drf_with_images/static; autoindex off; }

location / { proxy_pass http://django; 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:8000;

} }

waqarsqureshi avatar Dec 04 '15 13:12 waqarsqureshi

You need to make sure that the user on which nginx runs has access to the

/home/xyz/learn_drf_with_images/learn_drf_with_images/static

directory. This is rarely the case as nginx usually runs on a specific user (which depends on the linux distro you're using) and that path seems to be owned by a user named "xyz".

abarto avatar Dec 04 '15 14:12 abarto

This means, I should modify the access of folder so that is can be accessed by anyone? I did not faced the problem on vm because they are of the same ne as app? Let me try.. Regards Waqar On Dec 4, 2015 9:05 PM, "Agustin Barto" [email protected] wrote:

You need to make sure that the user on which nginx runs has access to the

/home/xyz/learn_drf_with_images/learn_drf_with_images/static

directory. This is rarely the case as nginx usually runs on a specific user (which depends on the linux distro you're using) and that path seems to be owned by a user named "xyz".

— Reply to this email directly or view it on GitHub https://github.com/abarto/learn_drf_with_images/issues/1#issuecomment-161975798 .

waqarsqureshi avatar Dec 05 '15 12:12 waqarsqureshi

No, just changing the user groups does not effect

  1. sudo chmod -R 2774 /path/to/static so that only developers can create/edit files (other/world can read)
  2. sudo chgrp -R www-data /path/to/static/ so that www-data (apache/nginx) can create uploads.

drwxrwsr-- 3 1000 33 4096 Dec 4 08:57 media drwsrwxrwt 4 1000 33 4096 Dec 4 08:44 static

In debug=true it works fine!

The NGINX conf file is as under:

xyz-thinkpad.conf

upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8000; # for a web port socket (we'll use this first) }

server { listen 80; server_name 127.0.0.1 localhost learn_drf_with_images xyz-thinkpad.local; charset utf-8;

client_max_body_size 75M; # adjust to taste

location /media { alias /home/xyz/learn_drf_with_images/learn_drf_with_images/media; autoindex off; }

location /static { alias /home/xyz/learn_drf_with_images/learn_drf_with_images/static; autoindex off; }

location / { proxy_pass http://django; 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:8000;

} }

regards

waqar


Waqar S. Qureshi Asian Institute of Technology(AIT) PO Box 4, Klongluang Pathumthani, 12120 Thailand Mobile:+66891771716 website:http://www.cs.ait.ac.th/~st111211

On Sat, Dec 5, 2015 at 5:52 PM, Waqar Shahid Qureshi <[email protected]

wrote:

This means, I should modify the access of folder so that is can be accessed by anyone? I did not faced the problem on vm because they are of the same ne as app? Let me try.. Regards Waqar On Dec 4, 2015 9:05 PM, "Agustin Barto" [email protected] wrote:

You need to make sure that the user on which nginx runs has access to the

/home/xyz/learn_drf_with_images/learn_drf_with_images/static

directory. This is rarely the case as nginx usually runs on a specific user (which depends on the linux distro you're using) and that path seems to be owned by a user named "xyz".

— Reply to this email directly or view it on GitHub https://github.com/abarto/learn_drf_with_images/issues/1#issuecomment-161975798 .

waqarsqureshi avatar Dec 05 '15 13:12 waqarsqureshi

Should I change something in the nginx.conf file, that looks like follows:

user www-data; worker_processes 4; pid /run/nginx.pid;

events { worker_connections 768; # multi_accept on; }

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json

application/x-javascript text/xml application/xml application/xml+rss text/javascript;

##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##

#include /etc/nginx/naxsi_core.rules;

##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##

#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

}

#mail {

# See sample authentication script at:

# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript

# auth_http localhost/auth.php;

# pop3_capabilities "TOP" "USER";

# imap_capabilities "IMAP4rev1" "UIDPLUS";

server {

listen localhost:110;

protocol pop3;

proxy on;

}

server {

listen localhost:143;

protocol imap;

proxy on;

}

#}


Waqar S. Qureshi Asian Institute of Technology(AIT) PO Box 4, Klongluang Pathumthani, 12120 Thailand Mobile:+66891771716 website:http://www.cs.ait.ac.th/~st111211

On Fri, Dec 4, 2015 at 7:05 PM, Agustin Barto [email protected] wrote:

You need to make sure that the user on which nginx runs has access to the

/home/xyz/learn_drf_with_images/learn_drf_with_images/static

directory. This is rarely the case as nginx usually runs on a specific user (which depends on the linux distro you're using) and that path seems to be owned by a user named "xyz".

— Reply to this email directly or view it on GitHub https://github.com/abarto/learn_drf_with_images/issues/1#issuecomment-161975798 .

waqarsqureshi avatar Dec 05 '15 13:12 waqarsqureshi