How to Configure Nginx as a Reverse Proxy with SSL on Ubuntu 24.04 for Multiple Domains
Introduction
Setting up a secure and efficient reverse proxy with SSL on Ubuntu 24.04 is essential for managing multiple websites or services behind a single server. At ByteHosting, we understand the importance of reliable infrastructure, and in this guide, we’ll walk you through configuring nginx reverse proxy ssl ubuntu 24.04 step-by-step. Whether you're hosting multiple domains or optimizing your web traffic, this setup ensures security, performance, and scalability.
Prerequisites
Before we begin, ensure you have the following:
- An Ubuntu 24.04 VPS instance — our cost-effective KVM-based VPS plans are perfect for this purpose.
- Domain names for each website or service you want to host.
- DNS records pointing your domains to your VPS IP address.
Having these in place will streamline the setup process and prevent common issues later on.
Installing Nginx from Ubuntu Repositories
Our first step is to install Nginx, the powerful web server that will act as our reverse proxy. On Ubuntu 24.04, Nginx is available directly from the default repositories, making installation straightforward:
sudo apt update
sudo apt install nginx -yOnce installed, verify that Nginx is running:
sudo systemctl status nginxIf active, you're ready to proceed. We recommend enabling Nginx to start on boot:
sudo systemctl enable nginxCreating Server Blocks for Each Domain
Next, we set up individual server blocks for each domain. This allows Nginx to handle multiple sites or services efficiently. For each domain, create a configuration file in /etc/nginx/sites-available/. For example, for example1.com:
sudo nano /etc/nginx/sites-available/example1.comInsert the following configuration, replacing your_backend_service_ip with the IP of your backend server or service:
server {
listen 80;
server_name example1.com www.example1.com;
location / {
proxy_pass http://your_backend_service_ip;
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;
}
}Repeat this process for each domain you wish to host. After creating the configuration files, enable them by creating symbolic links:
sudo ln -s /etc/nginx/sites-available/example1.com /etc/nginx/sites-enabled/Test the Nginx configuration for syntax errors:
sudo nginx -tIf all is well, reload Nginx:
sudo systemctl reload nginxObtaining SSL Certificates via Let's Encrypt Certbot
Security is paramount, and SSL encryption is a must-have. We recommend using Certbot to obtain free SSL certificates from Let's Encrypt. Install Certbot and the Nginx plugin:
sudo apt install certbot python3-certbot-nginx -yTo generate and install SSL certificates for your domain, run:
sudo certbot --nginx -d example1.com -d www.example1.comFollow the prompts to complete the process. Certbot will automatically configure your Nginx server blocks to use SSL and set up redirects from HTTP to HTTPS.
Configuring SSL and Redirect Rules
After Certbot completes, your server blocks will include SSL configurations. To ensure all traffic is secure, verify that your server blocks contain redirect rules. For example:
server {
listen 80;
server_name example1.com www.example1.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example1.com www.example1.com;
ssl_certificate /etc/letsencrypt/live/example1.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example1.com/privkey.pem;
location / {
proxy_pass http://your_backend_service_ip;
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;
}
}
This setup ensures all HTTP traffic is redirected to HTTPS, maintaining secure connections.
Testing the Reverse Proxy Setup
Once everything is configured, test your setup by visiting your domains in a browser. You should see the content served securely over HTTPS, with the reverse proxy correctly forwarding requests to your backend services.
You can also use online tools like SSL Labs' SSL Server Test to verify your SSL configuration and security grade.
Troubleshooting Common SSL and Proxy Issues
- SSL certificate errors: Ensure Certbot certificates are valid and paths are correct.
- Proxy not forwarding requests: Check your proxy_pass URL and headers.
- HTTP to HTTPS redirection not working: Verify redirect rules in your server blocks.
- Nginx syntax errors: Always run
sudo nginx -tafter changes.
Conclusion
Configuring nginx reverse proxy ssl ubuntu 24.04 is a powerful way to manage multiple domains securely and efficiently. By following our step-by-step guide, you can set up SSL encryption, optimize performance, and ensure your web services are protected against common threats. At ByteHosting, we’re committed to providing reliable infrastructure solutions — whether you’re deploying a single website or managing complex multi-domain setups. If you need scalable VPS hosting to support your projects, explore our affordable plans and experience enterprise-grade hosting with us.