Search code examples
djangodockernginxuwsgigoogle-cloud-run

How to fix 504 error caused by Docker image update


I have Django project. It works with nginx, uwsgi and google cloud run. This project is using docker which python:3.9 image. I have got this error since 17,Aug.

2021-10-13 17:22:29.654 JSTGET504717 B899.9 sGoogleStackdriverMonitoring-UptimeChecks(https://cloud.google.com/monitoring) https://xxxx/

The request has been terminated because it has reached the maximum request timeout. To change this limit, see https://cloud.google.com/run/docs/configuring/request-timeout

and also this error occur on all my pages. However when I open my pages myself, I can see my pages. It means I can't see 504 error and I can only check that it happens from server log.

enter image description here

I added a line in admin.py at 17, Aug. I didn't think this line is no related with this error. Because this change is only effect in admin page. I had rollback my code before the error. Now I'm still can't fix this error.

Builded docker image is different size before after error. And Vulnerability has decreased. I think this is caused by some small change on python image. In this case, how can I solve this problem?

enter image description here

What I did

I changed docker image to python:3.8 and python:3.9.6-buster. I couldn't fix the error.


Solution

  • I solved this problem. I changed socket to port connection.

    This is my settings.

    uwsgi.ini

    [uwsgi]
    # this config will be loaded if nothing specific is specified
    # load base config from below
    ini = :base
    
    # %d is the dir this configuration file is in
    http = 127.0.0.1:8000
    master = true
    processes = 4
    max-requests = 1000                  ; Restart workers after this many requests
    max-worker-lifetime = 3600           ; Restart workers after this many seconds
    reload-on-rss = 512                  ; Restart workers after this much resident memory
    threaded-logger = true
    
    [dev]
    ini = :base
    # socket (uwsgi) is not the same as http, nor http-socket
    socket = :8001
    
    
    [local]
    ini = :base
    http = :8000
    # set the virtual env to use
    home = /Users/you/envs/env
    
    
    [base]
    # chdir to the folder of this config file, plus app/website
    chdir = %dapp/
    # load the module from wsgi.py, it is a python path from 
    # the directory above.
    module = website.wsgi:application
    # allow anyone to connect to the socket. This is very permissive
    chmod-socket = 666
    
    

    nginx-app.conf

    # the upstream component nginx needs to connect to
    upstream django {
        # server unix:/code/app.sock; # for a file socket
        server 127.0.0.1:8000; # for a web port socket (we'll use this first)
    }
    
    # configuration of the server
    server {
        # the port your site will be served on, default_server indicates that this server block
        # is the block to use if no blocks match the server_name
        listen      8080;
    
        # the domain name it will serve for
        server_name MY_DOMAIN.COM; # substitute your machine's IP address or FQDN
        charset     utf-8;
    
    
        # max upload size
        client_max_body_size 10M;   # adjust to taste
        # set timeout
        uwsgi_read_timeout 900;
        proxy_read_timeout 900;
        # Django media
        location /media  {
            alias /code/app/media;  # your Django project's media files - amend as required
        }
    
        location /static {
            alias /code/app/static; # your Django project's static files - amend as required
        }
    
        # Finally, send all non-media requests to the Django server.
        location / {
            proxy_pass  http://127.0.0.1:8000;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_http_version 1.1;
            include     /code/uwsgi_params; # the uwsgi_params file you installed
        }
    }