208.4. Implementing Nginx as a web server and a reverse proxy

208.4 Implementing Nginx as a web server and a reverse proxy

Weight: 2

Description: Candidates should be able to install and configure a reverse proxy server, Nginx. Basic configuration of Nginx as a HTTP server is included.

Key Knowledge Areas:

  • Nginx

  • Reverse Proxy

  • Basic Web Server

Terms and Utilities:

  • /etc/nginx/

  • nginx

Whats is nginx ?

NGINX (short forEngine X) is a free, open-source and powerful HTTP web server and reverse proxy with an event-driven (asynchronous) architecture. It is written using C programming language and runs on Unix-like operating systems as well as Windows OS.

It also works as a reverse proxy, standard mail and TCP/UDP proxy server, and can additionally be configured as a load balancer. It is powering many sites on the web. well known for its high-performance, stability and feature-rich set.

history

In 2002, Igor Sysoev start work on Nginx as a solution provider to the C10K problem, which was a extreme challenge for web servers to start handling ten thousand synchronized connections as a necessity for the modern network. The first public release of Nginx was made in 2004, meeting this goal by relying on an asynchronous, events-driven architecture.

nginx vs apache

Apache and Nginx both are the most common open source web servers. Together, both web servers are responsible for serving over 50% of traffic on the internet. Both solutions are capable of handling different workloads and working with other software to provide a full web stack.

While Apache and Nginx split many qualities, they should not be consideration of as completely interchangeable. Each has been developed in its own way and it is important to understand the situations where we may need to re-evaluate ourweb server of choices. This table will show how each web server stacks up in various areas:

Criterian

Apache

Nginx

Static speed

Second to nginx

2.5x faster than apache

Dynamic speed

Both same in this area

Both same in this area

OS support

Unix, Windows, Mac OSX

works great with Unix-like OS. Not so with windows.

Security

Both have excellent security track record

Both have excellent security track record

Flexibility

Highly customizable architecture

Difficult to customize modules for the server due to complex base architecture

Support

Excellent community with wide spread user base.Lots of online support

provide community support through mailing lists, IRC , ...

Cost

Open source hence free to download and support

Open source license available along with paid liencese for advanced features like NGINX plus

Comparing and getting deeper to nginx features is beyond the scope of this lesson but it is recommanded to invest some time on it.

In this course we will stablish a basic web server using nginx and then implement a revere proxy using that.

Basic Web server

Lets start by installing Nginx web server from Ubuntu official repositories:

/etc/nginx/

All NGINX configuration files are located in the /etc/nginx/ directory.

The primary configuration file is /etc/nginx/nginx.conf:

In CentOS the configuration file would be like this:

Configuration options in NGINX are called directives. Directives are organized into groups known as blocks (or contexts,They are synonymous)

Lines preceded by a # character are comments and not interpreted by NGINX. Lines containing directives must end with a ; or NGINX will fail to load the configuration and report an error.

The file starts with 5 irectives:user,worker_processes,error_log, andpid. These are outside any specific block or context, so they’re said to exist in themaincontext. See the NGINX docs for explanations of these directives and others available in the maincontext.

Theeventsandhttpblocks are areas for additional directives, and they also exist in themaincontext.

  • The http Block :The http block contains directives for handling web traffic. These directives are often referred to as universal because they are passed on to to all website configurations NGINX serves.See the NGINX docs for a list of available directives for the http block.

  • Server Blocks : Thehttpblock above contains anincludedirective which tells NGINX where website Configuration files are located.

If we installed from the official NGINX repository, or RedHat, this line will sayinclude /etc/nginx/conf.d/*.conf;as it does in thehttpblock above. Each website we host with NGINX should have its own configuration file inetc/nginx/conf.d/, with the name formatted asexample.com.conf. Sites which are disabled (not being served by NGINX) should be named xample.com.conf.disabled.

If we installed NGINX from the Debian or Ubuntu repositories, this line will sayinclude /etc/nginx/sites-enabled/*;. The../sites-enabled/folder contains symlinks to the site configuration files stored in/etc/nginx/sites-available/. Sites insites-availablecan be disabled by removing the symlink tosites-enabled.

Regardless of the installation source, server configuration files will contain asserverblock (or blocks) for a website.

  • Listening ports : The listendirective tells NGINX the hostname/IP and the TCP port where it should listen for HTTP connections. The argument default_server means this virtual host will answer requests on port 80 that don’t specifically match another virtual host’s listen statement. The second statement listens over IPv6 and behaves similarly.

  • Name-based virtual Hosting : The server_name directive allows multiple domains to be served from a single IP address. The server decides which domain to serve based on the request header it receives.

NGINX allows us to specify server names that are not valid domain names. NGINX uses the name from the HTTP header to answer requests, regardless of whether the domain name is valid or not.

Using non-domain hostnames is useful if our server is on a LAN, or if we already know all of the clients that will be making requests of the server.

  • Location Blocks : The locationsetting lets we configure how NGINX will respond to requests for resources within the server. Just like the server_namedirective tells NGINX how to process requests for the domain, locationdirectives cover requests for specific files and folders, such as http://example.com/blog/. locationalso supports regular expressions:

  • Location Root and Index: Thelocationsetting is another variable that has its own block of arguments.

    Once NGINX has determined whichlocationdirective best matches a given request, the response to this request is determined by the contents of the associatedlocationdirective block. Here’s an example:

In this example, the document root is located in thehtml/directory. Under the default installation prefix for NGINX, the full path to this location is/etc/nginx/html/.Theindexvariable tells NGINX which file to serve if none is specified.

/usr/share/ngnix/html

The default public web root in NGNIX is /usr/share/ngnix/html how ever, here in ubuntu16.04 the default web root is:

Lets create ourselves index.html :

and lets chek the results:

nginx command

nginx deamon has a handy command tool nginxwhich can help us save more time, see some of switches:

Reverse Proxy

ngnix can be configured to works as a Reversy Proxy. It seats some where between client and web server and can do caching, loadbalancing, acceleration, client Authentication, bandwidth management and etc.

The most significant beneit of using proxies in our networks is server abstaraction. Becuase the client sees the proxy and not the actual server itself. From other side, when the server respond to the client request,it responds to the proxy , which respond to the client. This way we have added an abstaraction layer between client and the server. Client just sees the proxy server and doesn't have any idea about server configuration and on the other hand server is protected from evil hackers.

Working as a ReverseProxy is what ngnix is famous for and configuring it is so simple.It is configured as like as a virtual host.So lets start, we use Ubuntu and use previous basic web server. we have setup to proxy example.com:

Now we want to proxy request for example.com to the google.com, for that we need some configurations.

The simplest and minimal configuration which are requred for setting up a reverse proxy using nginx is inside proxy_param :

We need weather copy these configuration inside our virtual host or as we do here, include it.

Now lets make it enable and restart the nginx service:

Now it is time to check the results:

and see what will happend.

Last updated