Using Nginx as a Reverse Proxy

A proxy server is a machine that relays a client’s resource request to the appropriate server. Some network installations use proxy servers to filter and cache HTTP requests that machines on the local network make to the Internet. Since we will be running a number of Tornado instances on a range of TCP ports, we will use a proxy server in reverse: clients across the Internet will connect to a reverse proxy server, which will forward requests to any one host in a pool of Tornado servers behind the proxy. The proxy server is designed to be transparent to the client and yet pass valuable information like the original client’s IP address and TCP scheme to the upstream Tornado node.

Our server configuration is illustrated in Figure 8-1. The reverse proxy receives all incoming HTTP requests and distributes them evenly among the individual Tornado instances.

Tornado instances behind a reverse proxy server

Figure 8-1. Tornado instances behind a reverse proxy server

Basic Nginx Configuration

The listing in Example 8-1 is an example Nginx configuration. This Nginx setup listens for connections on port 80 and distributes those requests among the upstream hosts listed in the configuration file. In this case, we will assume the upstream hosts are listening for connections on their own port on the loopback interface.

Example 8-1. A bare-bones Nginx proxy configuration

user nginx; worker_processes 5; error_log /var/log/nginx/error.log; ...

Get Introduction to Tornado now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.