title: Nginx restrict domains
body: <p>Please find the below setting which is placed in <strong>/etc/nginx/sites-enabled</strong> under my site domain name. (mysite.lk)</p>
  
  <pre><code>server {  
     listen   80;
     server_name mysite.lk www.mysite.lk;
  
     location / {  
          proxy_set_header   X-Forwarded-For $remote_addr;
          proxy_set_header   Host $http_host;
          proxy_pass "http://127.0.0.1:8080";
     }
  
  } 
  </code></pre>
  
  <p>The application is running on port 8080 and here I'm redirecting all the 80 traffic to 8080.
  My website only uses <strong>mysite.lk</strong> and <strong>www.mysite.lk</strong> domain names.</p>
  
  <p>Hence, I want to <strong>restrict/block all other domains</strong> (except mysite.lk and www.mysite.lk) which are coming to this server IP. What is the change that I need to do to achieve this?</p>
  
  <p>I tried numerous things such as answers given in the 
  <a href="https://stackoverflow.com/questions/9824328/why-is-nginx-responding-to-any-domain-name">Why is nginx responding to any domain name?</a>, but was getting errors at the nginx startup. </p>
  
  <p>Please help me out!
  Thanks.</p>
  
  <h2><strong>Update</strong></h2>
  
  <p>Found the Answer. A catch-all server block should needed in the top of the config before the given config like below. The code block should be like this.</p>
  
  <pre><code>server {
      return 403;
  }
  
  server {
      listen   80;
      server_name mysite.lk www.mysite.lk;
      location / {
          proxy_set_header   X-Forwarded-For $remote_addr;
          proxy_set_header   Host $http_host;
          proxy_pass "http://127.0.0.1:8080";
     }
  
  }
  </code></pre>