Search notes:

Configuring nginx as an application gateway for a WSGI server

Some notes on using nginx as an application gateway for a WSGI server.

Application server

We use Gunicorn to implement the WSGI application server.

appModule.py

def appCallable(environ, start_response):

    start_response(
       '200 OK',
       [('Content-Type', 'text/plain; charset=utf-8')]
    )

    return [
      f'Hello world!\n\n'                              .encode('utf-8'),
      f'PATH_INFO      = {environ["PATH_INFO"     ]}\n'.encode('utf-8'),
      f'RAW_URI        = {environ["RAW_URI"       ]}\n'.encode('utf-8'),
      f'HTTP_HOST      = {environ["HTTP_HOST"     ]}\n'.encode('utf-8'),
      f'REMOTE_ADD     = {environ["REMOTE_ADDR"   ]}\n'.encode('utf-8'),
      f'HTTP_X_REAL_IP = {environ["HTTP_X_REAL_IP"]}\n'.encode('utf-8'),
      f'REMOTE_PORT    = {environ["REMOTE_PORT"   ]}\n'.encode('utf-8')
    ]

Starting the WSGI webserver

We start the WSGI webserver. The Gunicorn command line option -b allows to specify the IP address and port where the server listens:
$ gunicorn -b 127.0.0.1:1234 appModule:appCallable

nginx.conf

The following minimal nginx configuration file (/etc/nginx/nginx.conf) routes requests of /app/ URLs to the WSGI webserver:
events {
    worker_connections 1024;
}

http {

    sendfile on;

  #
  # Specify IP address and port of (WSGI) application server.
  # These values correspond to those specified with -b when
  # the Gunicorn webserver was started:
  #
    upstream gunicorn_srv {
        server 127.0.0.1:1234;
    }

    server {
    
      #
      # Port 80 is default. It cannot hurt to
      # explicitly state it:
      #
        listen 80;

      #
      # Forward requests to /app/ and beneath
      # to upstream server gunicorn_srv
      #
        location /app/ {
        
            proxy_pass         http://gunicorn_srv;
            proxy_redirect     off;

          # Without setting Host to $host, its value will be gunicorn_srv.
            proxy_set_header   Host             $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-Host $server_name;
        
        }
    }
}

Links

Using NGINX and NGINX Plus as an Application Gateway with uWSGI and Django

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/web/webs...', 1758199429, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/web/webserver/nginx/application-gateway(114): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78