简体   繁体   中英

Django, nginx, + gunicorn: connection refused when serving media files

I have a Django app served by gunicorn with nginx handling incoming requests. Static files are served just fine, and media files are served in my local development environment- I have MEDIA_ROOT and MEDIA_URL set.

However, on my web host (running Ubuntu 14.04), media files aren't served. Even if I run the Django dev server instead of letting gunicorn serve the app, media files do not show up and the following errors are output to the console:

Chrome开发者控制台输出

Since taking gunicorn out of the equation results in the same behavior, I believe the issue is with my nginx configuration. This is my sites-available for the app:

server {
listen 443 ssl;

server_name <server name here>;

<ssl stuff here>

access_log off;

location /static {
    alias /opt/testenv/testenv/proj/static/;
}

location /media {
    autoindex on;
    alias /opt/testenv/testenv/proj/media/;
}

location ~ /.well-known {
    allow all;
}

location / {
    proxy_pass http://127.0.0.1:9900;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
 }
}

server {
listen 80;
server_name <server name here>;
return 301 https://$host$request_uri;
}

I was initially missing the location /media block, which I thought was the issue. However, even after adding it, restarting nginx, and running collectstatic , I still get CONNECTION REFUSED errors when attepting to retrieve the files.

I turned on autoindex to verify that the files can be navigated to manually, and that does work.

The nginx error log doesn't contain anything related to these connection errors, which is curious to me- could the request be getting rejected elsewhere? I recently setup HTTPS but this issue was occurring even before that.

Looks like you are making requests directly to gunicorn instead of nginx,from you configuration you should visit:

https://127.0.0.1

instead of:

http://127.0.0.1:9900

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