How to Use a Reverse Proxy to Manage Https for Multiple Services

Managing HTTPS for multiple services on a single server can be complex. Using a reverse proxy simplifies this process by acting as an intermediary that handles all incoming requests and directs them to the appropriate service securely.

What Is a Reverse Proxy?

A reverse proxy is a server that sits in front of web services, receiving client requests and forwarding them to the correct backend server. It also manages SSL/TLS encryption, providing a unified point for HTTPS management.

Benefits of Using a Reverse Proxy for HTTPS

  • Centralized SSL Management: Handle all SSL certificates in one place.
  • Security: Protect backend services from direct exposure.
  • Flexibility: Easily add or remove services without changing SSL configs.
  • Load Balancing: Distribute traffic efficiently across multiple servers.

Setting Up a Reverse Proxy for Multiple Services

Popular reverse proxy tools include Nginx and Apache. Here, we’ll focus on Nginx due to its performance and ease of configuration.

Step 1: Install Nginx

Install Nginx on your server using your package manager. For example, on Ubuntu:

sudo apt update

sudo apt install nginx

Step 2: Obtain SSL Certificates

Use Let’s Encrypt for free SSL certificates. Install Certbot and request certificates for your domain:

sudo apt install certbot python3-certbot-nginx

sudo certbot –nginx -d example.com -d www.example.com

Step 3: Configure Nginx as a Reverse Proxy

Edit your Nginx configuration file to include server blocks for each service. Here’s an example:

sudo nano /etc/nginx/sites-available/your_conf

“`nginx

server {

listen 443 ssl;

server_name example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;

ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

location /service1/ {

proxy_pass http://localhost:8001/;

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;

}

location /service2/ {

proxy_pass http://localhost:8002/;

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;

}

}

server {

listen 80;

server_name example.com;

return 301 https://$host$request_uri;

}

Testing and Maintenance

After configuration, restart Nginx:

sudo systemctl restart nginx

Test your setup by accessing your domain and verifying that requests are correctly routed to each service over HTTPS.

Regularly renew your SSL certificates with Certbot:

sudo certbot renew

Conclusion

Using a reverse proxy like Nginx simplifies managing HTTPS for multiple services. It provides centralized SSL handling, enhances security, and makes your infrastructure more flexible and scalable.