DEV Community

S Karthik
S Karthik

Posted on

1

Configuring Nginx for IP Redirection and Domain Configuration

Introduction

Nginx, a powerful web server, can efficiently manage web traffic and serve multiple applications simultaneously. In this beginner-friendly guide, we’ll explore how to set up IP to domain redirection and application proxying using Nginx.

Configuring IP Address to Domain Redirection and Application Proxying

Navigate to your Nginx configuration file, typically located at /etc/nginx/sites-available/<your-file>

server {
    listen 80;
    server_name <server-ip>;

    location / {
        return 301 http://<dns-name>$request_uri;
    }
}

server {
    listen 80;
    server_name <dns-name>;

    location / {
        proxy_pass http://localhost:<port>;
        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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. IP Redirection: The first server block listens on port 80 for requests directed to the specified <server-ip>. It then issues a 301 redirect to the designated <dns-name> for all incoming requests.

  2. Domain Configuration: The second server block listens on port 80 for requests directed to the <dns-name>. It proxies the incoming traffic to the application running on http://localhost:<port>, ensuring seamless communication between Nginx and your application.

Conclusion

That’s it! You’ve successfully configured Nginx to redirect traffic from an IP address to a domain name. Now, whenever someone accesses your application using the IP address, they will be automatically redirected to the specified domain name.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay