简体   繁体   中英

Getting Nginx to serve static content in front of Django local server

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM