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.
server_name
must match hostname inlink
/script
URLs. Either declare your configuration as default for this interface:port pair (listen 8000 default
)Fim & Alexander - Thanks for the hints those helped.
Here is how I solved it for anyone stuck in the same boat -
settings.py -
In my html -
In my views.py -
nginx inside the sites-available config file -
Restarted nginx
Restarted apache
I also struggled with this. However, following trick worked for me:
I just marked static as a regex with
^
and nginx started serving static files. No modification on Django side was needed.MEDIA_URL shall not be used to serve the Static content like js etc. Django provides a separate STATIC_URL settings option that can be used.
So this can be changed as
Also, its more standard to use staticfile app templatetag like this:
Docs Here
I think using
root
in location block is incorrect. I usealias
and it works fine, even without re-configuring django.Hope this makes things simpler.