1

I have a Django system that has gunicorn hosting the dynamic content and nginx the static, nginx passing through to gunicorn as required via a Unix socket.

Every request that requires static content is generating an error like that below. Is this because the request comes from a Unix socket and not via IP? How do I fix this? A Quick "Google" hasn't helped but perhaps I've not found the right question yet :-(.

django.db.utils.IntegrityError: null value in column "ip" violates not-null constraint
DETAIL:  Failing row contains (48, 302, GET, /, 2022-05-25 07:51:28.855717+00, f, f, null, , Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KH..., en-GB,en;q=0.9,en-US;q=0.8,mt;q=0.7, null).

Thanks for any suggestions.

Paul D Smith
  • 639
  • 5
  • 16
  • Elaborate more on the question please. Give us the name of the table, and a little bit of code invilving the view in question, as well as the trace if possible. – Kaiss B. May 25 '22 at 08:29
  • Thanks for the question. I don't have a table that has the field `ip` in and I don't know what bit of code this is so I can only assume this is some sort of internal Django/PostgreSQL table. I can enable any trace I want though so what would you suggest? I can also add that I didn't see this until I switched from using the dev `runserver` to using `nginx` and `gunicorn`. – Paul D Smith May 25 '22 at 11:44
  • managed to get some more info by turning debug on: `(0.002) INSERT INTO "request_request" ("response", "method", "path", "time", "is_secure", "is_ajax", "ip", "user_id", "referer", "user_agent", "language") VALUES (302, 'GET', '/', '2022-05-25T16:23:26.944518+00:00'::timestamptz, false, false, NULL, NULL, '', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.53', 'en-GB,en;q=0.9,en-... ` `request_request` is definitely not one of my objects. – Paul D Smith May 25 '22 at 16:27
  • My suspicion is that `nginx` is not passing host/IP information through to gunicorn so I'm going to try switching to using an IP socket and see if that provides a temporary workaround. – Paul D Smith May 26 '22 at 07:12
  • The answer is that as suspected, `nginx` was not passing host/IP information through. As a workaround I have switched from using a unix socket to using a TCP socket between `nginx` and `gunicorn` but it looks from the `nginx` documentation as though it should be possible to pass the information through. If anyone has working config using a unix socket and a recent `nginx` and `gunicorn` install, could you please share? – Paul D Smith May 26 '22 at 10:51
  • I'll share a solution in a form of an answer below. – Kaiss B. May 27 '22 at 08:36
  • Just to make sure I have the right answer for you, a configuration of Django + NGINX + GUNICORN (and using a gunicorn socket instead of ports in NGINX) correct? – Kaiss B. May 27 '22 at 08:38

1 Answers1

0

Here is the configuration of NGINX using a Gunicorn socket:

server {
    listen 80;
    server_name YOUR_IP_OR_DOMAIN;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static {
        root WHERE YOUR STATIC FOLDER IS, DO NOT INCLUDE STATIC FOLDER IN THIS PATH;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock; <<<<<<====== HERE IS YOUR SOCKET FILE, I WILL EXPLAIN ITS CONTENT BELOW
    }
    client_max_body_size 100M; <= this is just something I added to not have timeout issues if the user uploads large files, you don't have to have this line

}

Gunicorn:

sudo nano /etc/systemd/system/gunicorn.service:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myproject
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/sammy/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

After saving the service file:

sudo systemctl start gunicorn
sudo systemctl enable gunicorn

You can check the socket working by typing : sudo systemctl status gunicorn

Kaiss B.
  • 249
  • 1
  • 12