nginx serving Django static media | 502 bad gatewa

2020-06-07 05:08发布

问题:

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..

回答1:

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



回答2:

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.



回答3:

if third party app included in your project then it should be installed on your server also like south is third party app. Consider south is included in your settings.py file then south should be installed on your server also. If that module consider south here, is already installed on server then try to upgrade it. Because it is possible that you are using upgraded version of module on local machine and older version is installed in the server.



回答4:

 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.



回答5:

Unless you're doing unusual things, there's no reason to run both apache and nginx.

Nginx + gunicorn is likely to work better than apache + modwsgi at this point in time.

Gunicorn deployment docs are here:

https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/gunicorn/

and the nginx config for deploying that is here:

http://gunicorn-docs.readthedocs.org/en/latest/deploy.html