Nginx as a Proxy Server and web server

mailmedatascience
5 min readApr 7, 2023

--

Nginx can be used as a reverse proxy server, which means it sits in front of web servers and forwards client requests to them. This can be useful in a number of scenarios, such as load balancing, SSL termination, and serving static content.

In this chapter, we will explore the following topics:

  1. Configuring Nginx as a reverse proxy: This section will explain how to configure Nginx as a reverse proxy, including setting up upstream servers and creating location blocks.
  2. Using Nginx as a caching proxy: This section will explain how to configure Nginx to act as a caching proxy, which can help improve website performance.
  3. Load balancing with Nginx: This section will explain how to use Nginx as a load balancer to distribute client requests across multiple web servers.

1.Configuring Nginx as a reverse proxy:

To configure Nginx as a reverse proxy, you need to define upstream servers and create location blocks. Here’s an example configuration:

http {
upstream backend {
server 192.168.1.10;
server 192.168.1.11;
}

server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

In this example, we define an upstream block with two servers, and a server block for the example.com domain. The location block forwards requests to the upstream servers, and sets some headers to preserve the original client IP address and other information.

2. Using Nginx as a caching proxy:

Nginx can be used as a caching proxy, which can help improve website performance by serving cached content instead of making requests to the backend servers. To enable caching, you need to add some directives to your Nginx configuration:

http {
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;

server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend;
proxy_cache my_cache;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 60m;
proxy_cache_bypass $http_pragma;
proxy_cache_revalidate on;
proxy_cache_lock on;
}
}
}

In this example, we define a cache path and a cache zone, and configure the location block to use the cache. We also set some cache-related directives, such as the cache key, cache validity, and cache bypass.

3. Load balancing with Nginx:

Nginx can also be used as a load balancer, which can distribute client requests across multiple web servers to improve performance and scalability. To configure Nginx as a load balancer, you need to define upstream servers and use the proxy_pass directive in the location block:

http {
upstream backend {
server 192.168.1.10;
server 192.168.1.11;
}

server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

In this example, Nginx defines an upstream group called backend that includes three servers. Incoming requests to the Nginx server will be distributed among the backend servers according to the configured load balancing algorithm (which defaults to round-robin).

Nginx as a Web Server

Nginx can be used as a standalone web server to serve static files or as a front-end server for dynamic web applications. In this chapter, we will discuss how to configure Nginx as a web server, perform URL rewriting with Nginx, serve static files using Nginx, and create custom error pages with Nginx.

  1. Configuring Nginx as a Web Server:

To configure Nginx as a web server, we need to modify the Nginx configuration file, which is typically located at /etc/nginx/nginx.conf. Here's a simple configuration for serving a basic web page:

http {
server {
listen 80;
server_name example.com;
root /var/www/example.com;

location / {
index index.html;
}
}
}

In this configuration, we have defined an HTTP server block with a single server block. The listen directive specifies the port on which Nginx should listen for incoming connections. The server_name directive specifies the domain name for which this server block should respond. The root directive specifies the root directory for serving static files. Finally, the location block specifies the configuration for handling requests to the root URL.

2. URL Rewriting with Nginx:

URL rewriting is the process of modifying the URL of a web page dynamically based on certain rules. This can be useful for creating search engine-friendly URLs or for redirecting users from old URLs to new ones. Nginx provides a powerful URL rewriting engine that can be used to accomplish this.

Here’s an example of how to perform URL rewriting with Nginx:

http {
server {
listen 80;
server_name example.com;

location /old-url {
rewrite ^/old-url(.*)$ /new-url$1 permanent;
}
}
}

In this example, we have defined a server block that listens on port 80 and responds to requests for example.com. The location block specifies that requests to the old URL should be rewritten to the new URL using a regular expression. The permanent flag indicates that this is a permanent redirect and that search engines should update their indexes accordingly.

3. Serving Static Files with Nginx:

Nginx can also be used to serve static files, such as HTML, CSS, JavaScript, and images. This can be useful for improving website performance, as Nginx is highly optimized for serving static content.

Here’s an example of how to serve static files using Nginx:

http {
server {
listen 80;
server_name example.com;
root /var/www/example.com;

location /static {
expires 1d;
add_header Cache-Control "public";
}
}
}

In this example, we have defined a server block that listens on port 80 and responds to requests for example.com. The root directive specifies the root directory for serving static files. The location block specifies that requests to the /static URL should be served from the root directory. The expires directive specifies that the browser should cache the file for one day. The add_header directive adds a Cache-Control header to the response.

4. Creating Custom Error Pages with Nginx:

Nginx allows you to create custom error pages for common HTTP errors, such as 404 Not Found or 500 Internal Server Error. This can be useful for improving the user experience and providing more informative error messages.

Here’s an example of how to create custom error pages with Nginx:

/var/www/example.com/html/404.html

Then, add the following configuration to your Nginx server block:

server {
...
error_page 404 /404.html;
location = /404.html {
internal;
}
...
}

This configuration tells Nginx to serve the 404.html file when a 404 error occurs, and to treat the 404.html location as an internal URL.

You can also create custom error pages for other HTTP status codes by adding similar error_page and location directives to your Nginx configuration.

After adding this configuration, reload or restart Nginx to apply the changes:

sudo systemctl reload nginx

Now, if a visitor to your website encounters a 404 error, they will see your custom error page instead of the default Nginx error page.

References:

--

--