Search code examples
djangonginxservergunicorn

Nginx + Gunicorn + Django: displays Nginx default page every 3 times


I have a Django app on Gunicorn 20.1.0 (:8000), served with Nginx 1.18.0 as the main server (link to the website) on Debian 11.2:

Nginx <-:8000-> Gunicorn <-> Django

The server displays the Nginx default template every three times (1st request, 4, 7, 10, so on). Here is a snippet of the Nginx access log (notice the pattern in the bytes sent):

89.xx.xxx.xx - - [05/Jul/2023:11:37:23 -0400] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0"
89.xx.xxx.xx - - [05/Jul/2023:12:01:51 -0400] "GET / HTTP/1.1" 200 6723 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0"
89.xx.xxx.xx - - [05/Jul/2023:12:01:52 -0400] "GET / HTTP/1.1" 200 6723 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0"
89.xx.xxx.xx - - [05/Jul/2023:12:01:53 -0400] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0"
89.xx.xxx.xx - - [05/Jul/2023:12:01:54 -0400] "GET / HTTP/1.1" 200 6723 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0"
89.xx.xxx.xx - - [05/Jul/2023:12:01:54 -0400] "GET / HTTP/1.1" 200 6723 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0"
...

There's no error in Nginx or Gunicorn error.log files (both server and site specific logs are blank. previously I had some errors so I'm assured that the logging is not flawed). Other requests (2nd,3rd,5th,...) are responded properly and as expected (no problems whatsoever).

I know It must be Gunicorn failing to respond but I have no clue where should I look at to figure out a solution.

So far, I tried to increase the timeout limit of both Gunicorn (timeout) and Nginx (the server responds pretty quick). It should be noted that the server has plenty of free resources. I also ran the Gunicorn with --log-level=debug but the error.log shows nothing related.

Here is the Gunicorn config file:

"""Gunicorn *development* config file"""
timeout = 120
wsgi_app = "project.wsgi:application"
loglevel = "debug"
workers = 2
bind = "0.0.0.0:8000"
reload = True
accesslog = errorlog = "/var/log/gunicorn/dev.log"
capture_output = True
pidfile = "/var/run/gunicorn/dev.pid"
daemon = True

and the Nginx server's:

server_tokens               off;
access_log                  /var/log/nginx/e-damac.ir.log;
error_log                   /var/log/nginx/e-damac.ir.error.log;

server {
  server_name               e-damac.ir www.e-damac.ir;
  listen                    80;
  location / {
    proxy_pass              http://localhost:8000;
    proxy_set_header        Host $host;
  }
  location /static {
    autoindex on;
    alias /var/www/html/e-damac.ir/static/;
  }
}

I haven't edited the nginx.conf file and it's in the original condition.

Any suggestions or diagnosis methods would be appreciated. Thanks in advance.

UPDATE: As gunicorn's access log suggests it's never received the requests in question.


Solution

  • If you are getting the default nginx page you need to remember the flow. Nginx -> gunicorn -> django. So if you have a different rendered page that is related to nginx the issue probably lies in nginx and not gunicorn. I'd just go find the conf server definition that points to nginx's default page and comment it out or remove it altogether. You most likely have over lapping server definitions going on right now.