DEV Community

Code masters
Code masters

Posted on

1

How to Set Up a Reverse Proxy with Nginx for Your Backend Application

Setting up a reverse proxy is an essential step for routing traffic from a custom domain to your backend application. This guide provides a general step-by-step process for configuring a reverse proxy using Nginx.

1. Configure DNS for Your Domain

Begin by setting up the DNS records:

Access your DNS provider's dashboard (e.g., Namecheap, GoDaddy, Cloudflare).

Create an A record:

Type: A Record

Name: Subdomain (e.g., "api")

Value: Your server’s public IP address

TTL: Automatic or 3600 seconds (1 hour)

Wait for DNS propagation, which can take up to 24 hours.

2. Install Nginx on Your Server

If Nginx is not already installed, follow these steps:
sudo apt update
sudo apt install nginx

Check if Nginx is running:
sudo systemctl status nginx

3. Configure Nginx as a Reverse Proxy

1.Open a new Nginx configuration file for your domain or subdomain:
sudo nano /etc/nginx/sites-available/your-subdomain

2.Add the following configuration:

server {
    listen 80;
    server_name your-subdomain.example.com;

    location / {
        proxy_pass http://localhost:your-backend-port;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    error_log /var/log/nginx/backend_error.log;
    access_log /var/log/nginx/backend_access.log;
}
Enter fullscreen mode Exit fullscreen mode

3.Save and exit the file

  • listen 80; tells Nginx to listen on port 80.

  • _server_name _your-subdomain.example.com; specifies the domain name.

  • proxy_pass forwards requests to the backend application.

  • Additional proxy_set_header directives ensure proper header forwarding.

4. Enable the Nginx Site Configuration

Create a symbolic link to enable the site configuration:

sudo ln -s /etc/nginx/sites-available/your-subdomain /etc/nginx/sites-enabled/

5. Test the Nginx Configuration

Verify the syntax of the Nginx configuration:

sudo nginx -t

If successful, you should see:

nginx: configuration file /etc/nginx/nginx.conf test is successful

6. Restart Nginx

Restart Nginx to apply the changes:

sudo systemctl restart nginx

7. Secure the Connection with SSL (Optional but Recommended)

For a secure HTTPS connection, use Certbot to obtain a free SSL certificate:

Install Certbot and the Nginx plugin:

sudo apt install certbot python3-certbot-nginx

Obtain an SSL certificate for your domain:

sudo certbot --nginx -d your-subdomain.example.com

Follow the prompts to complete the SSL setup. Certbot will update your Nginx configuration to redirect HTTP to HTTPS automatically.

Verify the certificate renewal process:

sudo certbot renew --dry-run

8. Verify the Setup

Access your backend application through the configured domain or subdomain:

https://your-subdomain.example.com

If the site is not working, check the Nginx logs for errors:

sudo tail -f /var/log/nginx/backend_error.log

Conclusion

By following these steps, you’ve successfully configured Nginx as a reverse proxy for your backend application. This setup ensures that traffic to your domain or subdomain is routed securely and efficiently, providing a professional and reliable user experience.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay