Gunicorn not creating socket on nginx

boban0987 asked:

I am trying to setup Django, Gunicorn and nginx.

I have configured Gunicorn in order to run, but it seems that I am experiencing a problem with nginx configuration to use Gunicorn.

Here is my configuration:


Description=gunicorn daemon

ExecStart=/srv/venvs/myenv/bin/gunicorn --pid /run/gunicorn/pid   \
          --bind unix:/run/gunicorn/socket myapp.wsgi:application
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID


Gunicorn status looks like this

sudo systemctl status gunicorn

gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-01-18 23:32:11 UTC; 3min 23s ago
  Process: 6347 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 6355 (gunicorn)
    Tasks: 2
   Memory: 195.7M
      CPU: 1.426s
   CGroup: /system.slice/gunicorn.service
           ├─6355 /srv/venvs/myenv/bin/python3.6 /srv/venvs/myenv/bin/gunicorn --pid /run/gunicorn/pid --bind unix:/run/gunicorn/socket myapp.wsgi:application
           └─6360 /srv/venvs/myenv/bin/python3.6 /srv/venvs/myenv/bin/gunicorn --pid /run/gunicorn/pid --bind unix:/run/gunicorn/socket myapp.wsgi:application

Jan 18 23:32:11 python-server systemd[1]: Stopped gunicorn daemon.
Jan 18 23:32:11 python-server systemd[1]: Started gunicorn daemon.
Jan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6355] [INFO] Starting gunicorn 19.7.1
Jan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6355] [INFO] Listening at: unix:/run/gunicorn/socket (6355)
Jan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6355] [INFO] Using worker: sync
Jan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6360] [INFO] Booting worker with pid: 6360

My nginx configuration


server {
    server_tokens off;
    listen      443 ssl;
    keepalive_timeout   70;

    ssl_certificate     /etc/ssl/certs/;
    ssl_certificate_key /etc/ssl/private/;

    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    #access_log      /var/log/nginx/access.myserver.log;
    access_log      /var/log/nginx/access.myserver.log;
    #error_log       /var/log/nginx/error.myserver.log;
    error_log       /var/log/nginx/error.myserver.log;

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn/socket;

On the path /run/gunicorn I can see only pid file. File socket has never created.

This is how the error log looks like


2018/01/18 23:09:00 [crit] 5764#5764: *1 connect() to unix:/run/gunicorn/socket failed (2: No such file or directory) while connecting to upstream, client:, server:, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn/socket:/",, host: ""

Can anyone see what is wrong here? Why socket file is not made and pid file is made?

My answer:

The socket that gunicorn actually opened, as shown in the journal entries, differs from the socket configured in your systemd unit.

The journal shows what gunicorn actually did:

Jan 18 23:08:49 python-server gunicorn[5858]: [2018-01-18 23:08:49 +0000] [5858] [INFO] Listening at: unix:/run/gunicorn/socket (5858)

Note that it actually opened /run/gunicorn/socket. But your nginx configuration and systemd unit specify to open /run/gunicorn/gunicorn.socket, which is a different path.

My first thought is that you probably changed the path in the systemd unit, but haven’t run systemctl daemon-reload. Any changes in your systemd units won’t take effect until you run this (or reboot).

Therefore, I would have systemd reload and then try restarting gunicorn.

systemctl daemon-reload
systemctl restart gunicorn.service

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.