How to integrate gunicorn with nginx

$pip install gunicorn
$sudo apt-get install gunicorn
WSGI_APPLICATION = 'config.wsgi.application'
import os

from django.conf import settings
from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()
$gunicorn project.wsgi:application --bind= --daemon --reload
$sudo yum install nginx
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 300M;

include /etc/nginx/mime.types;
default_type application/octet-stream;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;

fastcgi_connect_timeout 1000;
fastcgi_send_timeout 1000;
fastcgi_read_timeout 1000;
$mkdir /etc/nginx/sites-enabled
$cd sites-enabled
upstream site {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response

server unix:/var/lib/nginx/tmp/proxy/site.sock fail_timeout=0;

server {
listen 80;
listen [::]:80 ipv6only=on;

server_name site;

root /usr/share/nginx/html;
index index.html index.htm;

location /static/ {
alias /usr/share/nginx/site/staticfiles/;

location /media/ {
alias /usr/share/nginx/site/media/;

location / {
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# ensure https for django
# proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://site;
$cd /etc/systemd/system/
$vi site.service

Description=Gunicorn instance to serve site listener


# TODO : set workers from CPU (2 x $num_cores) + 1,
# from

ExecStart=/usr/local/bin/gunicorn --workers 4 --bind unix:/var/lib/nginx/tmp/proxy/site.sock config.wsgi:application

$sudo systemctl enable site.service
$sudo systemctl enable nginx.service
$sudo service site.service restart
$sudo service nginx restart




Experienced cloud engineer with 19 years of extensive online membership, billing, micro-transaction and payment on cloud.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

In the Beginning There was the Code…

AWS ManagedBlockchain + Hyperledger Explorer quickstart (1)

Monitoring Cassandra Health and Performance Metrics

Cleaning Services Ajax For Refreshment Ambience in Ajax ON

Crop Image to Face with Round Crop in Python

Getting Started with Orange Tool

An adventure in Data Structures and Algorithms [Part 1]

[Review] Leveraging Corellium for Mobile Penetration Testing

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hyoung Soo Kim

Hyoung Soo Kim

Experienced cloud engineer with 19 years of extensive online membership, billing, micro-transaction and payment on cloud.

More from Medium

GraphQL with Python — Part-1

Zipping Files In-Memory Using Django

Elegant Way to Manage Multiple Independent Python, MySQL, Node Versions

Setting Up Django App