question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Static files coming sometimes broken

See original GitHub issue

Hi, i have my Django application and i use nginx to proxy my static files only! and reason i use nginx-proxy because i can use it host multiple sites at my vps without much of hassle. At my website, that is https://bigtoolstation.com/ sometimes suddenly it shows 404 and if we see in console. we will see: image because its coming 404 😦. it happens mostly and is randomly on all browsers. Here is my nginx configuration file.

upstream web_server {
  server web;
}

server {
  listen 80;
  server_name 0.0.0.0;
  charset utf-8;
  client_max_body_size 1G;

  location /static/ {
    alias /app/static/;

    expires 365d;
    add_header Cache-Control public;
    tcp_nodelay off;

    open_file_cache max=3000 inactive=120s;
    open_file_cache_valid 45s;
    open_file_cache_min_uses 2;
    open_file_cache_errors off;
    access_log off;

  }

  location / {
    proxy_pass http://web_server;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_buffers 16 32k;
    proxy_buffer_size 64k;
    proxy_busy_buffers_size 128k;
    proxy_cache_bypass $http_pragma $http_authorization;
    proxy_connect_timeout 1d;
    proxy_hide_header X-Powered-By;
    proxy_ignore_headers Cache-Control Expires;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
    proxy_no_cache $http_pragma $http_authorization;
    proxy_pass_header Set-Cookie;
    proxy_read_timeout 10d;
    proxy_redirect off;
    proxy_send_timeout 10d;
    proxy_temp_file_write_size 64k;
    proxy_set_header Accept-Encoding '';
    proxy_set_header Cookie $http_cookie;
    proxy_set_header Host $host;
    proxy_set_header Proxy '';
    proxy_set_header Referer $http_referer;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Original-Request $request_uri;
  }
}

if there’s something wrong my side or need more information, kindly let me know. Thank you.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
Ammadkhalidcommented, Jul 1, 2020

Thank you for the suggested help. However that is not going to work for me. But i’m sure this will help somebody else in future. if this issue is not resolved.

0reactions
tkw1536commented, Jul 1, 2020

Hmm, that all looks right to me. I am not sure why this happens, especially the instability with 404s. The only thing I can imagine that different nginx workers pick up the request and one of them is configured incorrectly. I don’t know why that would be the case though.

For static file serving with django, I switched to uwsgi as opposed to gunicorn. Reason being that it can serve both Django wsgi requests and static files. That way I never have to worry about running nested nginx instances (one for nginx-proxy, one for the django app). In your case the config for uwsgi would be something like:

[uwsgi]

# network settings
http-socket = :80

# django settings
chdir = /path/to/django/app
module = src.wsgi

# static files, anything under 'static' should be served from this path
static-map = /static=/app/static/
# set expire headers, for each mime type
# 31536000 seconds = 365 * 24 * 60 * 60 seconds = 1 year
# see the docs at https://uwsgi-docs.readthedocs.io/en/latest/StaticFiles.html#setting-the-expires-headers
static-expires-type text/html=31536000

# process settings, sane defaults
master = 1
vacuum = True
processes = 2
threads = 2
max-requests = 5000

Depending on how much you rely on gunicorn uwsgi might or might not be an option.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django static files are broken - Stack Overflow
I'm trying to add a background picture to the hero section in Django, But whenever I open that URL: http://127.0.0.1:8000/static/img/bg.png ...
Read more >
Improve Load Times for APEX Static Files
In this post, I'll discuss several best practices to improve load times for your JavaScript, CSS, Image, and other static files.
Read more >
Errors and broken links | WordPress.org
Errors and broken links · 1. Go to All in One SEO > XML Sitemap and enable the Dynamically Generate Sitemap setting. ·...
Read more >
Pitfalls and Common Mistakes | NGINX
The try_files directive exists for an amazing reason: It tries files in a specific order. NGINX can first try to serve the static...
Read more >
Magento 2 CSS and JavaScript not loading from correct folder
Now we are going to verify Composer installation, Deploy static content, clear / flush Magento cache, and reindex the Magento 2 blocks. To...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found