Hosting Multiple Sites on a Single VPS with Nginx
If you're running several web apps or domains, you might be tempted to spin up a separate VPS for each one. But with a reverse proxy like Nginx, you can easily host multiple sites on a single server – saving money and simplifying management. Here's how.
What is a Reverse Proxy?
A reverse proxy is a server that sits in front of your web applications and routes client requests to the right backend based on the domain name. Nginx is one of the most popular reverse proxy servers due to its high performance, flexibility, and ease of use.
With Nginx, you can host multiple apps or domains on one VPS, each running on a different port or Unix socket. Nginx listens on the standard HTTP/HTTPS ports (80/443) and forwards requests to the appropriate backend based on the domain in the request header.
Setting Up Nginx as a Reverse Proxy
To configure Nginx for multiple sites, you'll create a separate server block for each domain in your Nginx configuration. Here's a basic example:
http {
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:3000;
}
}
server {
listen 80;
server_name another.com;
location / {
proxy_pass http://localhost:5000;
}
}
}In this config, we have two server blocks, one for example.com and one for another.com. Each block listens on port 80 and uses the server_name directive to specify which domain it handles.
The location block defines how to handle requests. Here we're using proxy_pass to forward requests to a backend running on localhost port 3000 for example.com, and localhost port 5000 for another.com. You can also use Unix sockets for the backend connection.
Handling SSL/HTTPS
To secure your sites with SSL, you'll need to obtain an SSL certificate for each domain (or a multi-domain wildcard certificate). Then add an additional server block for each domain that listens on port 443 and includes the SSL configuration:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/example.com.crt;
ssl_certificate_key /path/to/example.com.key;
location / {
proxy_pass http://localhost:3000;
}
}With this setup, Nginx will handle the SSL encryption and decryption, and still forward the decrypted request to your backend as HTTP.
For even better security, you can also configure Nginx to redirect all HTTP requests to HTTPS:
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}Nginx Performance and Security Tips
To get the most out of your Nginx reverse proxy setup, consider these performance and security best practices:
- Use HTTP/2 for improved performance, especially with SSL.
- Configure Nginx caching to speed up response times for static assets.
- Tune Nginx worker processes and connections based on your VPS resources and traffic.
- Harden Nginx security by restricting access, using strong SSL ciphers, and keeping Nginx updated.
- Implement rate limiting to protect against DDoS attacks and abuse.
Managing Multiple Sites Made Easy
With Nginx as a reverse proxy, hosting multiple websites or apps on a single VPS becomes much simpler and more efficient. You can manage all your sites' configurations in one place, secure them with a single SSL setup, and fine-tune performance globally.
Of course, there's a lot more you can do with Nginx – this is just a starting point. But even a basic reverse proxy configuration can go a long way in optimizing your VPS usage and simplifying your hosting setup. Give it a try!
