© Binildas Christudas 2019
Binildas ChristudasPractical Microservices Architectural Patternshttps://doi.org/10.1007/978-1-4842-4501-9_22

Install, Configure, and Run Nginx Reverse Proxy

Binildas Christudas1 
(1)
Trivandrum, Kerala, India
 

Nginx is an HTTP and reverse proxy server which can also play as a mail proxy server or as a generic TCP/UDP proxy server. Basic HTTP server features include serving static and index files. Nginx also supports keep-alive and pipelined connections. TCP/UDP proxy server features include generic proxying of TCP and UDP as well as load balancing and fault tolerance. Go to https://nginx.org for more information.

I cover the following in this appendix:
  • How to install and start using Nginx

  • How to set up Nginx as an HTTP reverse proxy

  • How to set up Nginx as a TCP reverse proxy

Install Nginx

In this section, you will download, install, and configure Nginx and test the installation. Nginx is available for download at https://nginx.org/en/download.html .

Save the installation archive file to a suitable location in your hard disk first and then extract the archive to a location of your choice. Even though you may choose any location, like
C:\Program Files\nginx\
I explicitly avoid a location with a space in the path, like the space in Program Files. Hence in my Windows machine, I use the following location:
D:\Applns\nginx\nginx-1.10.1

Start Using Nginx

You can now start Nginx Server by using the following command, shown in Figure C-1:
D:\Applns\nginx\nginx-1.10.1>nginx
../images/477630_1_En_22_Chapter/477630_1_En_22_Fig1_HTML.jpg
Figure C-1.

Start Nginx Server

Once Nginx is started, it can be controlled by invoking the executable with the -s parameter, as in
nginx -s flag
where flag may be one of the following:
  • Stop: Forceful shutdown

  • Quit: Graceful shutdown

  • Reload: Reload the configuration file

  • Reopen: Reopen the log files

Start Using Nginx as Reverse Proxy

Nginx is frequently used for setting up as a reverse proxy server. When set as a reverse proxy server, Nginx receives requests, passes them to the proxied servers, retrieves responses from the proxied servers, and sends them back to the clients.

Configurations in Nginx are effected in the nginx.conf file located in the conf path.

Configure HTTP Reverse Proxy

An HTTP reverse proxy can be set up in Nginx to proxy requests with the HTTP protocol. Relevant sections in nginx.conf for setting up an HTTP proxy are shown in Listing C-1.
http {
    upstream myapp1 {
        server localhost:8081;
        server localhost:8082;
    }
    server {
        listen       8080;
        server_name  localhost;
        location / {
            proxy_pass http://myapp1;
        }
    }
Listing C-1

Nginx HTTP Reverse Proxy Configuration

Now, an HTTP request similar to URL pattern http://localhost:8080 will be proxied to both of the following URLs in a load-balanced fashion:
http://localhost:8081
http://localhost:80802

Configure TCP Reverse Proxy

A TCP reverse proxy can be set up in Nginx to proxy requests with the TCP protocol. Relevant sections in nginx.conf for setting up a TCP proxy are shown in Listing C-2.
stream {
    upstream myapp1 {
        server localhost:5672;
        server localhost:5673;
    }
    server {
        listen     5671;
        proxy_pass myapp1;
    }
}
Listing C-2

Nginx TCP Reverse Proxy Configuration

Summary

The reverse proxy setup of Nginx can be really useful in load balancing external requests to microservices. Hence I use Nginx to demonstrate many scenarios in this book.