Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shall I restart both nginx and gunicorn when production is updated?

What is the best practice when I have an update for my Django app pushed in my production? Shall I restart both gunicorn and nginx services, with

sudo service gunicorn restart
sudo service nginx restart

or restarting only gunicorn is enough? Finally does the order of the restarts makes any difference if I have to do both the restarts? Thanks!

like image 710
pebox11 Avatar asked Dec 15 '15 23:12

pebox11


People also ask

When should I restart Nginx?

Restart Nginx only when making significant configuration updates, such as changing ports or interfaces. This command will force shut down all worker processes.

How do gunicorn and nginx work together?

Nginx and Gunicorn work togetherGunicorn translates requests which it gets from Nginx into a format which your web application can handle, and makes sure that your code is executed when needed. They make a great team! Each can do something, which the other can't.

Do Nginx need restart after config change?

You need to reload or restart Nginx whenever you make changes to its configuration. The reload command loads the new configuration, starts new worker processes with the new configuration, and gracefully shuts down old worker processes.

Is Nginx required with gunicorn?

Every request comes to nginx and asks for which gunicorn application should it go and it redirects it. NOTE - Gunicorn cannot serve static files automatically as your local django server does. So you will need nginx for that again. Save this answer.


1 Answers

It entirely depends on how you've configured your box.

To keep downtime to an absolute minimum, I actually load my new release into a different directory on the box while the old release is still running. I create a new virtual environment based on my new release's requirements.txt. Then I start a second instance of gunicorn with the new release running in it (done via supervisord with entries in supervisord.conf), and leave the old instance still running.

I then update my nginx vhost file to point the server to the new release's gunicorn socket, and finally reload nginx. I do a quick check that the new site is up and functioning, and then I stop the old gunicorn instance. If for some reason it's not responding, I switch my nginx config back to point to the old one again, and then go figure out what's wrong.

I do all this using an Ansible script, but here's a great article with some Fabric scripts to do something similar: https://medium.com/@healthchecks/deploying-a-django-app-with-no-downtime-f4e02738ab06

If, on the other hand, you just update your code in-place, then there should be no changes needed to your nginx config, so you shouldn't need to reload it. Just reload gunicorn and you're good to go.

like image 92
skulegirl Avatar answered Sep 29 '22 00:09

skulegirl