Setting 2 sites in a Nginx server with gunicorn + django using systemD service

Question!

I am following the tutorial: http://www.obeythetestinggoat.com/book/chapter_09.html

So, i have two sites in DigitalOcean with nginx and gunicorn, one for stage and the normal one... stage.nossalista.tk and nossalista.tk, first i manage to successfully deploy and run the stage one, but after upload the normal and added another systemd service for my normal one, everything stops working.. i gonna put all my configs and hope you guys give me some hints were may be the problem....

im getting error 400 from the nossalista.tk and 502 for my stage.nossalista.tk

[UPDATE]: I was able to access the site only in stage.nossalista.tk:9000 .... hope this helps who wants to help me

First i did is configure the

/etc/nginx/sites-available/stage.nossalista.tk:

server {
    listen 80;
    server_name nossalista.tk;

    location /static {
        alias /home/tyago/sites/nossalista.tk/static;
    }

    location / {
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/nossalista.tk.socket;
    }
}

Second i configure the service for this stage site in

/etc/systemd/system/gunicorn.service:

[Unit]
Description=Gunicorn server for stage.nossalista.tk
After=network.target

[Service]
User=tyago
Group=www-data
WorkingDirectory=/home/tyago/sites/stage.nossalista.tk/source
ExecStart=/home/tyago/sites/stage.nossalista.tk/virtualenv/bin/gunicorn --workers 3 --bind unix:/tmp/stage.nossalista.tk.socket superlists.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

and then configure my socket service in

/etc/systemd/system/gunicorn.socket:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn/socket
ListenStream=0.0.0.0:9000
ListenStream=[::]:8000

[Install]
WantedBy=sockets.target

and then the gunicorn.conf file in (tyago is my user)

/usr/lib/tmpfiles.d/gunicorn.conf

d /run/gunicorn 0755 tyago tyago  -

After i enable both with:

systemctl enable gunicorn.service
systemctl enable gunicorn.socket
systemctl start gunicorn.service
systemctl start gunicorn.socket

after reboot the server i was able to access stage.nossalista.tk. Great! but the problem was when put the normal site up i make another service in systemd, this time called

gunicorn-nossalista.tk.service:

[Unit]
Description=Gunicorn server for www.nossalista.tk
After=network.target

[Service]
User=tyago
Group=www-data
WorkingDirectory=/home/tyago/sites/nossalista.tk/source
ExecStart=/home/tyago/sites/nossalista.tk/virtualenv/bin/gunicorn --workers 3 --bind unix:/tmp/nossalista.tk.socket superlists.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

and configured in

/etc/nginx/sites-available/nossalista.tk as:

[Unit]
Description=Gunicorn server for www.nossalista.tk
After=network.target

[Service]
User=tyago
Group=www-data
WorkingDirectory=/home/tyago/sites/nossalista.tk/source
ExecStart=/home/tyago/sites/nossalista.tk/virtualenv/bin/gunicorn --workers 3 --bind unix:/tmp/nossalista.tk.socket superlists.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

my systemctl status of the gunicorn.service , gunicorn.socket and gunicorn-nossalista.tk.serice are all Active

checking my error log of nginx i saw that only my stage file was logging there with the following error:

Nginx error log

2016/09/11 21:16:17 [crit] 3173#3173: *39 connect() to unix:/tmp/stage.nossalista.tk.socket failed (2: No such file or directory) while connecting to upstream, client: 189.34.5.XXX, server: stage.nossalista.tk, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/stage.nossalista.tk.socket:/", host: "stage.nossalista.tk"

ok i change the socket to work with the normal site so probally my error is that i dont have a socket for stage.nossalista ... BUT why i not see any log error message from my normal site in error.log if i configured correct the /etc/nginx/sites-enabled/nossalista.tk ? in access log i got this when i access via normal site(nossalista.tk)

Access Nginx log:

189.34.5.XXX - - [11/Sep/2016:21:53:30 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0"

i know that something is really strange there but cant find why im getting error 400 if i have it in sites-enabled/nossalista.tk all configuration correct pointing to the right socket and all that ....

If you guys help me i would be very great full and i would help anyone with similar errors... i just need some help to get it work!

my sudo nginx -t:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

i really dont know what to check more .... if you guys have some hints that would be Very Very NICE !!!

i think my problem is in the socket ... probally i need to have 2 sockets but dont know how to do it !

TY and sory for this big big question....



Answers
It's not clear if you have two servers defined in your nginx.conf (i.e., stage.nossalista.tk and nossalista.tk), but from your later comment, it seems like you do.

So if both of your gunicorn services are creating sockets, then in your nginx.conf just specify each one as an upstream app server:

upstream staging_server {
  server unix:<staging-socket-name> fail_timeout=0;
}

upstream production_server {
  server unix:<production-socket-name> fail_timeout=0;
}

server {
  listen 80;
  server_name stage.nossalista.tk;
  <...>
    proxy_pass http://staging_server; 
  <...>
}

server {
  listen 80;
  server_name nossalista.tk;
  <...> 
    proxy_pass http://production_server; 
  <...>
}

Though I will say that most staging and production environments are on separate servers (droplets). That way the configuration remains the same across the instances. In your case you will need to make sure that you have different database names for your stage and production instances.



This video can help you solving your question :)
By: admin