DEV Community

S Karthik
S Karthik

Posted on

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.

Top comments (0)