Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve django media files via nginx ?

I'm new at Nginx, I've successfully bound my django project to Nginx. However I can't serve my static files and I guess I set my media folder's location wrongly. Here is my file tree:

root_directory      my_django_project          ...          manage.py          app1          app2          media            admin            css            js            ... 

And my nginx.conf goes like :

        server {                 listen 192.168.1.9:80;                 server_name localhost;                 # site_media - folder in uri for static files                                                                                                              location /media/  {             root /home/nazmi/workspace/portal/media/;                                                                                                        }  location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {   access_log   off; # po co mi logi obrazków :)                                                                                                                 expires      30d; }                 location / {                         # host and port to fastcgi server                                                                                                                             fastcgi_pass 127.0.0.1:8080;             fastcgi_param PATH_INFO $fastcgi_script_name;                         fastcgi_param REQUEST_METHOD $request_method;                         fastcgi_param QUERY_STRING $query_string;                         fastcgi_param CONTENT_TYPE $content_type;                         fastcgi_param CONTENT_LENGTH $content_length;             fastcgi_pass_header Authorization;                         fastcgi_intercept_errors off;                         }                 access_log      /var/log/nginx/localhost.access_log main;                 error_log       /var/log/nginx/localhost.error_log;         } } 

When I open my admin page, all css pages give 404 error. Can you tell me that how can I set my media path correctly ?

like image 692
iva123 Avatar asked Dec 03 '11 20:12

iva123


People also ask

How do I serve media files in Django?

To make Django development server serve static we have to add a URL pattern in sitewide urls.py file. Now visit http://127.0.0.1:8000/media/python.png again, this time you should be able to see the image. Just as with static files, in the production, you should always use a real web server to serve media files.

Can nginx serve files?

To serve static files with nginx, you should configure the path of your application's root directory and reference the HTML entry point as the index file. In this example, the root directory for the snake deployment is /home/futurestudio/apps/snake which contains all the files.

Can Django run on nginx?

It takes you through the steps required to set up Django so that it works nicely with uWSGI and nginx. It covers all three components, providing a complete stack of web application and server software. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.


2 Answers

Here's a example of how I have my nginx servers setup

server {     server_name example.com www.example.com;     location /static {         autoindex on;         alias /home/myusername/myproject/static/;     }     location /media {         autoindex on;         alias /home/myusername/myproject/media/;     }     location / {         proxy_pass http://127.0.0.1:8000;     } } 

I serve django with Gunicorn on localhost port 8000. (that's what the proxy_pass is for)

The Nginx wiki example configuration may help you too. Notice in their static file serving they specify allowed filetypes and use 'root' instead of 'alias' but they are similar.

This ServerFault question may help.

like image 153
j_syk Avatar answered Sep 24 '22 05:09

j_syk


If the media file isn't serving, try to set media location as below in the conf file inside the sites-enabled directory. This works for me.

location /media {     root /home/username/projectname/; 
like image 42
Muhammmed Nihad Avatar answered Sep 22 '22 05:09

Muhammmed Nihad