Table of contents
To configure Nginx to proxy requests to your server running on port 3000 and attach the domain name api.example.com
, follow these steps:
Step 1:
Installation
On Ubuntu/Debian
Update your package lists:
sudo apt update
Install Nginx:
sudo apt install nginx
Start and enable Nginx:
sudo systemctl start nginx sudo systemctl enable nginx
On CentOS/RHEL
Install the EPEL repository (if not already installed):
sudo yum install epel-release
Install Nginx:
sudo yum install nginx
Start and enable Nginx:
sudo systemctl start nginx sudo systemctl enable nginx
Step 2: Configure DNS
Ensure that the DNS A record for api.example.com
points to the IP address of your server. This configuration is typically done through your domain registrar's control panel.
Step 3: Create an Nginx Configuration for Your Domain
Create a configuration file for your domain:
sudo vim /etc/nginx/conf.d/api.example.com.conf
Add the following configuration to the file:
server { listen 80; server_name api.example.com; location / { proxy_pass http://localhost:3000; 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 configuration tells Nginx to listen for requests on port 80 for
api.example.com
and proxy those requests to your server running onhttp://localhost:3000
.
Step 4: Test and Reload Nginx Configuration
Test the Nginx configuration for syntax errors:
sudo nginx -t
If the test is successful, reload Nginx to apply the new configuration:
sudo systemctl reload nginx
Step 5: Secure Your Domain with HTTPS (Optional but Recommended)
To secure your domain with HTTPS, you can use Let's Encrypt to obtain a free SSL certificate.
Install Certbot and the Nginx plugin:
For Ubuntu/Debian:
sudo apt install certbot python3-certbot-nginx
For CentOS/RHEL:
sudo yum install certbot python3-certbot-nginx
Obtain and install the SSL certificate:
sudo certbot --nginx -d api.example.com
Follow the prompts to complete the installation. Certbot will automatically configure Nginx to use the SSL certificate.
Verify that your SSL certificate is working by visiting
https://api.example.
com
in your web browser.
Example Full Nginx Configuration with HTTPS
If Certbot successfully installed your SSL certificate, your Nginx configuration file (/etc/nginx/conf.d/api.example.com.conf
) will look similar to this:
server {
listen 80;
server_name api.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name api.example.com;
ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://localhost:3000;
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 configuration includes both HTTP to HTTPS redirection and the SSL setup for secure connections.
Conclusion
By following these steps, you can configure Nginx to serve your application running on port 3000 and attach the domain name api.example.com
. Additionally, securing your domain with HTTPS ensures encrypted communication between clients and your server.