Dump the code

Setting up reverse proxy

Created 8 months ago
Posted By admin
3min read
A reverse proxy is a server that sits between client devices (like web browsers) and a web server, forwarding client requests to the server. It acts on behalf of the server, handling tasks such as load balancing, SSL termination, compression, and caching. From the client's perspective, it appears as if they are interacting directly with the reverse proxy, while in reality, the proxy forwards requests to one or more backend servers.

Why use a reverse proxy?

Load Balancing: Distributes incoming requests across multiple backend servers to ensure optimal resource utilization and better performance.
SSL Termination: Handles SSL/TLS encryption and decryption, relieving backend servers from this resource-intensive task.
Caching: Stores copies of static resources, reducing the load on backend servers and improving response times.
Security: Can hide the structure of your internal network, providing an additional layer of security.

Nginx reverse proxy configuration:

Create a new configuration file in the sites-available directory.

sudo nano /etc/nginx/sites-available/reverse-proxy

Here's a simple example of an Nginx configuration for a reverse proxy:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://backend_server;
        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;
    }
}
  • listen 80; Specifies that Nginx should listen on port 80, the default port for HTTP.
  • server_name yourdomain.com; Defines the domain name for which this server block will be responsible.
  • proxy_pass http://backend_server; Forwards requests to the specified backend server.
  • proxy_set_header Sets headers to pass additional information to the backend server, such as the original host, client's IP address, and the protocol used.
  • X-Real-IP: Client's real IP address.
  • X-Forwarded-For: Client's IP address when requests pass through a proxy.
  • X-Forwarded-Proto: Protocol (HTTP or HTTPS) used by the client.

Remember to replace yourdomain.com with your actual domain and http://backend_server with the address of your backend server.

Enable the site configuration:
Create a symbolic link to enable the site configuration:

sudo ln -s /etc/nginx/sites-available/reverse-proxy /etc/nginx/sites-enabled/

After making changes to the configuration file, don't forget to reload Nginx to apply the changes:

sudo service nginx reload

This is a basic introduction to reverse proxy configuration in Nginx. As you become more familiar with Nginx, you can explore additional features and optimizations based on your specific needs.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles