title: IP - Domain redirect using nginx + EC2
body: <p>I need to redirect users to my domain name if my IP is hit. 
  I'm using an EC2 instance. I had to add this line to allow the instance to  bind public IP.</p>
  
  <p>Following @kirpit's answer on <a href="https://stackoverflow.com/questions/3191509/nginx-error-99-cannot-assign-requested-address">nginx error: (99: Cannot assign requested address)</a> i added this line in the file:  /etc/sysctl.conf</p>
  
  <pre><code>net.ipv4.ip_nonlocal_bind = 1
  </code></pre>
  
  <p>and reloaded sysctl.conf </p>
  
  <pre><code>sysctl -p /etc/sysctl.conf
  </code></pre>
  
  <p>Here's the nginx configuration I've tried so far:</p>
  
  <pre><code>server {
      listen 80;
  }
  
  server {
      listen MY_PUBLIC_IP:80;
      server_name MY_PUBLIC_IP default;
      rewrite  ^/(.*)$  http://mydomain.in/$1 permanent;
  }
  </code></pre>
  
  <p>If i replace MY_PUBLIC_IP with MY_PRIVATE_IP the redirect works but its a redirect loop. So i think somehow nginx isnt recognising my public IP.</p>
  
  <p>How do i get this to work? </p>