DEV Community

Kishan vyas
Kishan vyas

Posted on

A Deep Dive into Forward and Reverse Proxy with NGINX on Linux

Proxies play a critical role in modern web architecture, enabling secure, efficient, and manageable network communications. NGINX, a powerful and widely-used web server, excels in providing both forward and reverse proxy capabilities. In this blog, we'll dive into the concepts of forward and reverse proxies, how they work in NGINX, and provide code examples for setting them up on a Linux machine.

Table of Contents

  1. What is a Proxy?
  2. Forward Proxy
    • Definition
    • Use Cases
    • Setting up Forward Proxy with NGINX
  3. Reverse Proxy
    • Definition
    • Use Cases
    • Setting up Reverse Proxy with NGINX
  4. Key Differences Between Forward and Reverse Proxy
  5. Conclusion

1. What is a Proxy?

A proxy acts as an intermediary between a client and a server. It handles requests from clients, forwards them to the appropriate server, and then returns the server's response back to the client. This can be done for various purposes, such as improving security, load balancing, or accessing blocked resources.


2. Forward Proxy

Definition

A forward proxy sits between a client and the internet. It is used by clients to access resources on the internet that they might not be able to reach directly. This could be due to network restrictions, privacy concerns, or simply to cache content for quicker access.

Use Cases

  • Access Control: Allow or block access to certain websites or resources.
  • Privacy: Hide the client's IP address from the internet.
  • Content Filtering: Block specific types of content (e.g., ads or adult content).
  • Caching: Store copies of frequently accessed resources to speed up requests.

Setting up Forward Proxy with NGINX

Here's how you can set up a forward proxy server with NGINX on a Linux machine:

  1. Install NGINX: Make sure NGINX is installed on your Linux machine. If not, install it using the package manager:
   sudo apt update
   sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode
  1. Configure NGINX as a Forward Proxy: Edit the NGINX configuration file to set up the forward proxy.

Open the NGINX configuration file:

   sudo nano /etc/nginx/nginx.conf
Enter fullscreen mode Exit fullscreen mode

Add the following configuration:

   events {}

   http {
       server {
           listen 8080; # The port on which the forward proxy will listen

           location / {
               proxy_pass $scheme://$host$request_uri;
               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-Proto $scheme;
           }

           access_log /var/log/nginx/proxy-access.log;
           error_log /var/log/nginx/proxy-error.log;
       }
   }
Enter fullscreen mode Exit fullscreen mode

This configuration tells NGINX to listen on port 8080 and forward all requests it receives to the destination server.

  1. Restart NGINX:

After editing the configuration file, restart NGINX to apply the changes:

   sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode
  1. Test the Forward Proxy: You can test the forward proxy by configuring a web browser to use your Linux machine's IP address and port 8080 as a proxy server.

3. Reverse Proxy

Definition

A reverse proxy, on the other hand, sits between the internet and a server. It forwards client requests to one or more backend servers and returns the server's response to the client. The client interacts with the reverse proxy as if it were the actual server.

Use Cases

  • Load Balancing: Distribute incoming traffic across multiple servers.
  • SSL Termination: Handle SSL encryption/decryption on behalf of backend servers.
  • Security: Protect backend servers by hiding their IP addresses and managing access control.
  • Caching: Store responses from backend servers to reduce load and improve response times.

Setting up Reverse Proxy with NGINX

Here's how to configure NGINX as a reverse proxy on a Linux machine:

  1. Install NGINX: Ensure NGINX is installed:
   sudo apt update
   sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode
  1. Configure NGINX as a Reverse Proxy: Edit the NGINX configuration file to set up the reverse proxy.

Open the NGINX configuration file:

   sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Modify the server block to include the reverse proxy configuration:

   server {
       listen 80;

       server_name example.com; # Replace with your domain or IP address

       location / {
           proxy_pass http://backend_server_ip; # Replace with your backend server IP or domain
           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-Proto $scheme;
       }

       access_log /var/log/nginx/reverse-proxy-access.log;
       error_log /var/log/nginx/reverse-proxy-error.log;
   }
Enter fullscreen mode Exit fullscreen mode

This configuration directs NGINX to forward all requests it receives on port 80 to the backend server specified by proxy_pass.

  1. Restart NGINX:

After updating the configuration, restart NGINX:

   sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode
  1. Test the Reverse Proxy: You can test the reverse proxy by accessing your domain or IP address in a web browser. The requests should be forwarded to the backend server.

4. Key Differences Between Forward and Reverse Proxy

  • Direction of Proxying: A forward proxy handles requests from clients seeking resources from servers, while a reverse proxy handles requests from clients and forwards them to backend servers.
  • Client Awareness: In a forward proxy, the client knows it's using a proxy, whereas, in a reverse proxy, the client is unaware it's interacting with a proxy.
  • Common Uses: Forward proxies are commonly used for privacy, filtering, and access control, while reverse proxies are used for load balancing, SSL termination, and security.

5. Conclusion

NGINX is a versatile tool that can act as both a forward and reverse proxy, providing a variety of benefits including improved security, load balancing, and access control. Whether you need to set up a forward proxy to control client access to the internet or a reverse proxy to manage and secure backend server traffic, NGINX on Linux offers a robust solution.

By understanding and implementing these proxies, you can enhance your network architecture, making it more efficient and secure.

Top comments (1)

Collapse
 
dailysandbox profile image
Art

I had been using nginx for years until i discovered caddy. Now, its all I use!