views:

651

answers:

3

I'm trying to serve Django static media through nginx, Here's my nginx.conf

server {
    listen       7777;
    listen       localhost:7777;
    server_name  example.com;

    location / {
        proxy_pass http://localhost:7777;
        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;
        client_max_body_size 10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k; 
    }

    location /test-app-media/ {
        root /sites/mysite/staticmedia/;
        expires max;
      }
}

but give a 502 bad gateway error, the path to /sites/mysite/staticmedia/ is in the nginx root/ is that the problem..


Django running on Apache 2.2 + mod_wsgi

nginx 0.7.65

Thanks..

A: 
 502 bad gateway

is because apache has a problem (not restarted or something like that). You can check the apache server logs for info.

The problem is your /sites/mysite/staticmedia/ is being passed to apache and not served by nginx itself.

Your nginx media part has to be like this:

location /staticmedia/ {
    root /sites/mysite/;
    expires max;
    autoindex on
  }

This will access /sites/mysite/staticmedia/ on the filesystem.

That is, the path of location specified with location is a considered to be the part of the file system too. (I don't think this is good way either; but this is how nginx does it.)

You can leave the autoindex on, to help you during debug.

Lakshman Prasad
I have made the changes but still not working and sometimes gives the 502 error
MMRUser
+2  A: 

the better way would be to use nginx in front of apache and to serve static media:

eg: nginx:

server {
    listen 80;
    server_name media.example.com;
    access_log /var/log/nginx/media.example.com.media.access.log;
    location / {
        autoindex on;
        index index.html;
        root /var/www/media.example.com/media;
    }
}

server { 
        listen 80;
        server_name www.example.com;
        access_log /var/log/nginx/www.example.com.django.access.log;

    location / {
        proxy_pass http://wwwcluster;
        include /etc/nginx/proxy.conf;
    }
}

proxy.conf:

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;
client_max_body_size        10m;
client_body_buffer_size     128k;
proxy_connect_timeout       90;
proxy_send_timeout          90;
proxy_read_timeout          90;
proxy_buffer_size           4k;
proxy_buffers               4 32k;
proxy_busy_buffers_size     64k;
proxy_temp_file_write_size  64k;

nginx.conf:

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

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

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;

    upstream wwwcluster {
        server 127.0.0.1:8080;
    }

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

and configure apache to serve your website on 127.0.0.1:8080

i have this setup on multiple sites and its running perfect. another advantage is, that you can cluster/load-balance your app very easy by adding another apache-server to upstream wwwcluster in nginx.conf

renton
Hey I don't see any files called proxy.conf and nginx, so where do I put those configurations?
MMRUser
A little late but if you are on Ubuntu I put it in the conf.d folder, the `include /etc/nginx/conf.d/*.conf;` line in your nginx.conf looks for anything that has a .conf extension, IE proxy.conf and because of that you probably won't need the `include /etc/nginx/proxy.conf;` in your site's nginx conf.
TheLizardKing
Oh and to @renton, this is exactly what I needed and with those few modifications worked flawlessly. I redirect to the Django's development server for my non-production version and it's just perfect. Thanks!
TheLizardKing
A: 
server {
    listen       7777;
    listen       localhost:7777;
    server_name  example.com;

    location / {
        proxy_pass http://localhost:7777;
...

nginx listening on port 7777 and connecting to a proxy located at port 7777 on same host. No wonder it returns 502 error.

edogawaconan