1

I'm trying to use Nginx in front of Django's localhost webserver (127.0.0.1:8000) to serve the static content. I'd like Nginx to serve all files under '/static', and if not, pass the request onto the Django's webserver, but I'm stuck! Here's what I've done:

  1. Got Nginx running on my OSX, so the 'welcome to Nginx!' page shows on localhost.
  2. Changed my /etc/hosts file to add 'testdev.com':

    127.0.0.1 localhost

    127.0.0.1 testdev.com

  3. Made /sites-available and /sites-enabled files in /usr/local/src/nginx-1.2.6

  4. My nginx.conf file in /conf is the default plus the include statement:

    include /usr/local/src/nginx.1.2.6/sites-enabled/testdev.com

5.My testdev.com file is in sites-available, with a symlink in /sites-enabled.

server {
root /<path-to-my-django-project>/website/static;
server_name testdev.com;
gzip            off;
listen         8000;

location = /favicon.ico  {
rewrite "/favicon.ico" /img/favicon.ico;
}
proxy_set_header Host $host;
location / {
if (-f $request_filename) {
     add_header X-Static hit;
     access_log   off;
 }

 if (!-f $request_filename) {
     proxy_pass http://127.0.0.1:8000;
     add_header X-Static miss;
 }
 }
}

If I curl the testdev.com, it shows Nginx:

curl -I http://testdev.com
HTTP/1.1 200 OK
Server: nginx/1.2.6
Date: Mon, 22 Apr 2013 18:37:30 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 21 Apr 2013 19:39:47 GMT
Connection: keep-alive
Accept-Ranges: bytes

But if I try to access a static file, nothing:

curl -I http://testdev.com/static/css/style.css
HTTP/1.1 404 Not Found
Server: nginx/1.2.6
Date: Mon, 22 Apr 2013 18:38:53 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

All this is based from a Google search, and finding this.

I added in the

listen 8000

statement in my testdev.com conf file as I thought that was needed for the Nginx virtual host, but I'm super confused. The blog author used

127.0.1.1 testdev.com

In his hosts file, but if i add that, the first curl statement just hangs.

What am I doing wrong?

professorDante
  • 2,290
  • 16
  • 26
  • 1
    Duplicate of http://stackoverflow.com/questions/1474374/nginx-doesnt-serve-static which has plenty of answers. – Evgeny Apr 22 '13 at 18:50
  • @Evgeny - how so, isn't that a production deployment issue? I'm in localhost. – professorDante Apr 22 '13 at 19:03
  • @professorDante - I'm going to second Evgeny. Even though that is a production issue, it still is the same root cause. What you're asking for can't really be done. Django's web server only runs when you have "DEBUG=True" in your settings.py . You can't have a setup where it looks in nginx and to the django webserver because the django webserver only runs when debug is on. – Dave Apr 22 '13 at 19:21
  • nginx doesnt care if its production or localhost, the configuration for serving static files is the same. just copy-paste from the answer in the duplicate question. – Evgeny Apr 22 '13 at 19:53

1 Answers1

2

Thanks all - I've got it working, here's my working testdev conf:

server {
    root /<path-to-django-site>;
    server_name testdev.com;
    gzip            off;
    autoindex       on;

    proxy_set_header Host $host;

    location /static/ {
        add_header X-Static hit;
    }

    location / {
        proxy_pass       http://127.0.0.1:8000;

    }
}

Looks like the location block takes the server root path if you don't supply one. Now when I curl:

curl -I  http://testdev.com/static/js/utils.js
HTTP/1.1 200 OK
Server: nginx/1.2.6
Date: Tue, 23 Apr 2013 01:36:07 GMT
Content-Type: application/x-javascript
Content-Length: 2730
Last-Modified: Thu, 13 Dec 2012 18:54:10 GMT
Connection: keep-alive
X-Static: hit
Accept-Ranges: bytes

Many thanks @Evgeny - got me on the right lines. Miget be useful for others looking to do the same.

Josh K
  • 28,364
  • 20
  • 86
  • 132
professorDante
  • 2,290
  • 16
  • 26