Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django : Serving static files through nginx

Tags:

nginx

django

I'm using apache+mod_wsgi for django.
And all css/js/images are served through nginx.
For some odd reason, when others/friends/colleagues try accessing the site, jquery/css is not getting loaded for them, hence the page looks jumbled up.

My html files use code like this -

<link rel="stylesheet" type="text/css" href="http://x.x.x.x:8000/css/custom.css"/> <script type="text/javascript" src="http://1x.x.x.x:8000/js/custom.js"></script> 

My nginx configuration in sites-available is like this -

    server {             listen   8000;             server_name  localhost;           access_log  /var/log/nginx/aa8000.access.log;              error_log  /var/log/nginx/aa8000.error.log;                 location / {                   index  index.html index.htm;                }               location /static/ {                 autoindex on;                 root   /opt/aa/webroot/;              }          }    

There is a directory /opt/aa/webroot/static/ which have corresponding css & js directories.

The odd thing is that the pages show fine when I access them.
I have cleared my cache/etc, but the page loads fine for me, from various browsers.

Also, I don't see 404 any error in the nginx log files.

Any pointers would be great.

like image 302
PlanetUnknown Avatar asked Mar 16 '10 02:03

PlanetUnknown


People also ask

Can nginx be used for serving static content?

Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.

Where do I put static files in nginx?

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.

Does Django work with 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

I think using root in location block is incorrect. I use alias and it works fine, even without re-configuring django.

# django settings.py MEDIA_URL = '/static/'  # nginx server config server {        ...     location /static {             autoindex on;             alias /opt/aa/webroot/;         } } 

Hope this makes things simpler.

like image 184
miki725 Avatar answered Oct 08 '22 04:10

miki725


  1. server_name must match hostname in link/script URLs. Either declare your configuration as default for this interface:port pair (listen 8000 default)
  2. Nginx must listen on the interface where your host's IP is bound (seems ok in your case)
like image 35
Alexander Azarov Avatar answered Oct 08 '22 02:10

Alexander Azarov