6

I'm trying to setup nginx to serve static files. Basically all I need is to have http://mydomain.com/site_media/ point to /var/django/myproject/site_media.

I have tried so many configurations and when I test it I always get a 404 error for static files.

Can anyone please tell me what I'm doing wrong or how I should be setting this up?

This is my current nginx configuration file.

 user www-data; worker_processes 1; #error_log /usr/local/nginx/logs/error.log; #pid /usr/local/nginx/logs/nginx.pid; events { worker_connections 1024; use epoll; } http { # Enumerate all the Tornado servers here upstream frontends { server 127.0.0.1:8000; server 127.0.0.1:8001; server 127.0.0.1:8002; server 127.0.0.1:8003; } include mime.types; default_type application/octet-stream; #access_log /usr/local/nginx/logs/access.log; keepalive_timeout 65; proxy_read_timeout 200; sendfile on; tcp_nopush on; tcp_nodelay on; gzip on; gzip_min_length 1000; gzip_proxied any; gzip_types text/plain text/html text/css text/xml application/x-javascript application/xml application/atom+xml text/javascript; proxy_next_upstream error; server { listen 80; # Allow file uploads client_max_body_size 50M; location ^~ /site_media/ { root /var/django/myproject/site_media; if ($query_string) { expires max; } } location = /favicon.ico { rewrite (.*) /site_media/favicon.ico; } location = /robots.txt { rewrite (.*) /site_media/robots.txt; } location / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_pass http://frontends; } } #include /usr/local/nginx/sites-enabled/*; } 

Thanks,

Cata

2 Answers 2

3

I looked at the nginx error logs and found that it was trying to serve the media from /var/django/myproject/site_media/site_media instead of /var/django/myproject/site_media --weird.

I changed root /var/django/myproject/site_media; to root /var/django/myproject; and it works now.

:)

4
  • Please set this as the answer to your question Commented Feb 18, 2010 at 20:58
  • I will, but it tells me that I need to wait 23 hours before I can accept my own answer. Commented Feb 19, 2010 at 0:25
  • Try again? <-- minimum length --> Commented Sep 13, 2010 at 23:41
  • @mods can someone set the Answer to be solved, the user seems to be deleted Commented Jul 30, 2021 at 12:50
1

If you don't include a trailing slash in your root /blah; directive it will append the location. If you do include the trailing slash, it won't.

You must log in to answer this question.