简体   繁体   中英

Problems with nginx+gunicorn resolving static content

I am setting up an environment with a django application (askbot opensource project), gunicorn and nginx. With nginx and gunicorn in different docker containers.

This is my nginx configuration.

server {
       listen 80;

       location /static {
         alias /askbot/static;
       }

       location /m {
         alias /askbot/static;
       }

       location / {
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http://askbot:8080/;
       }
     }

If I run the django app on debug mode, everything is ok, I can go through nginx and I see how nginx is only invoking gunicorn for the dynamic content, and the static content is resolved locally.

But, when I run the django application with debug false, nginx does not revolve the static content, and if I see the source code for the web page, I can see all the paths for the static content have changed, using somethign like "/m/CACHE...". I guess this is the reason why nginx cannot resolve the static content anymore.

For example, when using debug mode equals true, this is one fragment of the html source code.

<link rel="stylesheet" href="/m/CACHE/css/9275d0e5b87f.css" type="text/css" /> <link href="//fonts.googleapis.com/css?family=Open+Sans+Condensed:700&amp;subset=latin-ext" rel="stylesheet" type="text/css" /> <link href="//fonts.googleapis.com/css?family=Open+Sans+Condensed:700&amp;subset=cyrillic-ext" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="/m/default/media/jslib/modernizr.custom.js?v=1"></script> <script type="text/javascript">

And when running with debug equals false, the same fragment is like.

<link href="/m/default/media/style/style.css?v=1" rel="stylesheet" type="text/css" /> <link href="/m/default/media/fa-4.3.0/css/font-awesome.css?v=1" rel="stylesheet" type="text/css" /> <link href="/m/default/media/bootstrap/css/bootstrap.css?v=1" rel="stylesheet" type="text/css" /> <link href="//fonts.googleapis.com/css?family=Open+Sans+Condensed:700&amp;subset=latin-ext" rel="stylesheet" type="text/css" /> <link href="//fonts.googleapis.com/css?family=Open+Sans+Condensed:700&amp;subset=cyrillic-ext" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="/m/default/media/jslib/modernizr.custom.js?v=1"></script> <script type="text/javascript">

I have been going around this for two days, could anyone explain to me why is that happening and how can be fixed?, configuring nginx on a different way, or disabling django to have that behaviour.

Thanks in advance, Esteban Collado

Ok, I was able to solve my problem,

It was caused by the compressor application included in the settings.py file.

INSTALLED_APPS = (
...
    'compressor'
...
)

The django application I was deploying needed that application, so what I did was disable the compression adding to the same file.

   COMPRESS_ENABLED = False

Now, I do not see the behaviour I described in my question, all the static filed added to the CACHE folder in the urls.

BR, Esteban Collado

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