HillWithSmallFields
HillWithSmallFields

Reputation: 33

I don't understand how nginx is finding gunicorn in my setup

Largely following the instructions at https://tutos.readthedocs.io/en/latest/source/ndg.html, I got an nginx-gunicorn-django stack running, and it's been running reliably for weeks now. While looking at how to add something (user-uploaded media files) I looked into my nginx configuration, and it looks like I haven't edited any mention of gunicorn into it. I'd like to understand what's going on before I make any more complex changes. How can nginx be forwarding requests to gunicorn, when there are no mentions of gunicorn anywhere in the nginx configuration directory tree?

My main nginx configuration file looks like this, which I think is the default from the installation:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

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

        gzip on;
        gzip_disable "msie6";

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

and my gunicorn start script is:

#!/bin/bash

# Based on an example from https://tutos.readthedocs.io/en/latest/source/ndg.html

NAME="makers"                              # Name of the application (*)
DJANGODIR=/var/www/makers                  # Django project directory (*)
SOCKFILE=/var/www/makers/run/gunicorn.sock # we will communicate using this unix socket (*)
USER=nginx                                 # the user to run as (*)
GROUP=webdata                              # the group to run as (*)
NUM_WORKERS=1                              # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=makers.settings     # which settings file should Django use (*)
DJANGO_WSGI_MODULE=makers.wsgi             # WSGI module name (*)

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /var/www/makers_venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /var/www/makers_venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user $USER \
  --bind 0.0.0.0:8000
#   --bind=unix:$SOCKFILE

Upvotes: 1

Views: 324

Answers (1)

hcheung
hcheung

Reputation: 4034

How nginx pass the request to WSGI server (i.e. gunicorn) is defined in one of the location directive within the server block, which is included by the http block that you can see in your /etc/nginx.conf file:

http {

  # other http settings
  .....

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

Within the file where the server is defined, among all the server and location directives you will likely see the setting like this:

server {

  ...

  location / {

      ...

      # if your guincorn is listening to an IP add
      proxy_pass http:127.0.0.1:8000
      # or if your gunicorn is listening to a unix socket
      # proxy_pass http://unix:/var/www/makers/run/gunicorn.sock 
  }

Based on your gunicorn start script, your nginx is passing the request to gunicorn via an IP address at port 8000:

exec /var/www/makers_venv/bin/gunicorn makers.wsgi:application --bind 0.0.0.0:8000

I would recommend you read Understanding the Nginx Configuration File Structure and Configuration Contexts, or my blog for better understand on how nginx and gunicorn(or WSGI in general) works.

Upvotes: 2

Related Questions