[Fixed]-Gunicorn is not binding my domain by using ".sock" file

1👍

I have the same problem, .sock doesn’t create. This method helps me.
Prerequests:

  1. Installed nginx: when you type in browser 127.0.0.1 – obtain “Wellcome to nginx…”.
  2. You install python2 or 3 no matter, and other stuffs: pip, django, gunicorn…
  3. You installed and settled virtualenv. (in my case, I use virtualenvwrapper – this is good staffs, saves all you env in one folder: /home/user/.virtualenvs/)
  4. You created django project, and when: python manage.py runserver –
    you obtain “It works…” – this good news.
  5. When you type gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application – you have the same result, as a step 4.

Next step for setting you dj.project throgh gunicorn to nginx:

  1. You create file in /etc/systemd/system/any_file_name.service – you can named this file as you want, at DO – it names as gunicorn.service.

my method:

$cd /etc/systemd/system

$sudo touch gunicorn.service

and open it your favorite text editor

$sudo subl gunicorn.service
  1. Inside it you write:
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=vetal
Group=www-data
WorkingDirectory=/var/www/apple.net
ExecStart=/home/vetal/.virtualenvs/univ/bin/gunicorn --workers 3 --bind unix:/var/www/apple.net/mysite/mysite.sock mysite.wsgi:application
 [Install] WantedBy=multi-user.target

ExecStart – what will be started by nginx, when your virualenv will be turned off. Do you remember, gunicorn was install through pip, when your env was turn on ?
— bind unix:… – this address WHERE your .sock will created! Pay attention for this!

CHECK EVERY LETTER!TWISE!!! (of course with you links..)

  1. Type:

$ls -l

if you see in attributes to your ‘gunicorn.service’ something:

-rw-r--r-- 1 root root    0 Янв 12 11:48 gunicorn.service

this means – this file is not executable, and you .sock – file will never created! Make next:

$sudo chmod 755 gunicorn.service

and check:

$ls -l

if you get:

-rwxr-xr-x 1 root root  305 Янв 11 19:48 gunicorn.service

this good! Everything allright!

  1. Then you created nginx block, in /etc/nginx/site-available/ it likes next:
server {
    listen 80;
    root /var/www/apple.net;

    server_name apple.net;
    location = /favicon.ico { access_log off; log_not_found off; }

    location = /static/ {
            alias /var/www/apple.net/static/;
    }
    location / {
            include proxy_params;
            proxy_pass http://unix:/var/www/apple.net/mysite/mysite.sock;
    } }

Notice: proxy_pass – must be identicaly correct with folder where .sock file created in gunicorn.service!

  1. Copies this file to /sites-enable

    $ sudo cp /etc/nginx/site-avaliable/apple.net /etc/nginx/site-enable

  2. I don’t have any domaine, so I modify my /etc/hosts file, add row:

127.0.0.10 apple.net

  1. Very important steps!!!

$pkill gunicorn – this step kill daemon, which you may started before. gunicorn in this case, means name of file which you created before with .service extention, in /etc/systemd/system – folder.

  1. Start gunicorn.service daemon:
$sudo systemctl start gunicorn

$sudo systemctl enable gunicorn
  1. Start(or restart nginx)

$sudo /etc/init.d/nginx (re)start

  1. Check your domane name in browser.

0👍

Since gunicorn is running on a socket, you need to bind to that socket, not to a port, in the upstream section.

upstream mydomain_server {
    server unix:/home/django/mydomain/run/gunicorn.sock fail_timeout=0;
}

0👍

I have nginx serving up a .sock file from gunicorn. My typical gunicorn call looks like this:

exec gunicorn \
    --pid /web/gunicorn.pid \
    --workers '4' \
    --name myapp \
    --chdir /src/myapp \
    --bind unix:/web/.sock \
    --log-file=- \
    myapp.wsgi:application

My nginx conf for / looks like this; the main difference seems to be that your proxy_pass statement doesn’t point to the .sock file:

    location / {
        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_pass http://unix:/web/.sock;
    }

Leave a comment