Introduction
When developing web applications, we often run multiple services on our local machine. For example, you might have a frontend application running on localhost:3000
and a backend API running on localhost:8000
. While you can access these services using localhost:port
, it’s more convenient to use subdomains like frontend.domain.com
and backend.domain.com
.
In this article, we’ll show you how to set up a reverse proxy with Nginx to route traffic from subdomains to your local services. This way, you can access your services using subdomains instead of localhost:port
.
Terminal Based
If you are using a terminal-based environment, you can use the following steps to set up a reverse proxy with Nginx.
Prerequisites
Before you begin, make sure you have the following prerequisites:
- A domain name pointing to your local machine (e.g.,
domain.com
). - Nginx installed on your local machine. You can install Nginx using your package manager (e.g.,
apt
,yum
,brew
).
Setting Up Nginx
1. Bind Subdomains to Localhost
First, you need to bind the subdomains to localhost
in your /etc/hosts
file. Open the /etc/hosts
file with a text editor and add the following lines:
2. Configure Nginx
Next, you need to configure Nginx to route traffic from subdomains to your local services. Create a new Nginx configuration file in /etc/nginx/sites-available
(e.g., domain.com
) and add the following configuration:
In the configuration above, we define two server blocks for frontend.domain.com
and backend.domain.com
. Each server block listens on port 80
and proxies traffic to the respective local service.
3. Enable the Configuration
Next, create a symbolic link to enable the configuration file in /etc/nginx/sites-enabled
:
4. Restart Nginx
Finally, restart Nginx to apply the changes:
5. Test the Setup
To test the setup, open your browser and navigate to http://frontend.domain.com
and http://backend.domain.com
. You should see your frontend and backend applications running on the respective subdomains.
Docker Based + Dashboard
Thanks to Nginx Proxy Manager you can easily set up a reverse proxy with Nginx using Docker and a web-based dashboard.
Prerequisites
Before you begin, make sure you have the following prerequisites:
- Docker installed on your local machine. You can install Docker using the official installation guide.
- Port
80
and443
available on your local machine.
Setting Up Nginx Proxy Manager
1. Create a Docker Compose File
Create a new docker-compose.yml
file in your project directory and add the following configuration:
2. Start the Docker Container
Run the following command to start the Docker container:
3. Access the Dashboard
Open your browser and navigate to http://localhost:81
to access the Nginx Proxy Manager dashboard. You can login with the default credentials:
4. Add a Proxy Host and more
You can follow the instructions in the Nginx Proxy Manager documentation to add a new proxy host and configure routing rules using the web-based dashboard.
SSL Certificate with mkcert
The easiest way to get a valid SSL certificate is to use Let’s Encrypt. However, if you want to use a self-signed certificate for development purposes, you can use mkcert.
1. Install mkcert
First, install mkcert
on your local machine:
2. Generate SSL Certificates
Run the following commands to generate SSL certificates for your subdomains:
You should see the generated certificates (frontend+backend.pem
and frontend+backend-key.pem
) in your current directory.
3. Configure Nginx
Update your Nginx configuration to use the SSL certificates:
Conclusion
In this article, we’ve shown you how to set up a reverse proxy with Nginx to route traffic from subdomains to your local services. Whether you’re using a terminal-based environment or Docker, you can easily configure Nginx to proxy traffic to your local services using subdomains. Additionally, we’ve covered how to generate SSL certificates for your subdomains using mkcert
for development purposes.