title: Nginx multiple server blocks listening to same port
body: <p>I want to run <code>www.example.com</code> and <code>api.example.com</code> on same port <code>80</code>.</p>
  <p>This is what I have. All my googles ping lead to the below code. But, this is not working.</p>
  <pre><code>server {
          listen 80 default_server;
  #       listen [::]:80 default_server ipv6only=on;
  
          root /var/www/example.com/html/example/app;
          index index.html index.htm;
  
          # Make site accessible from http://localhost/
          server_name www.example.com www.example.org;
  
          location / {
                  # First attempt to serve request as file, then
                  # as directory, then fall back to displaying a 404.
                  try_files $uri $uri/ =404;
                  # Uncomment to enable naxsi on this location
                  # include /etc/nginx/naxsi.rules
          }
  
          location /bower_components {
                  alias /var/www/example.com/html/example/bower_components;
          }
  
          location /scripts {
                  alias /var/www/example.com/html/example/scripts;
          }
  
          location /content {
                  alias /var/www/example.com/html/example/content;
          }
  
          location /api {
                  proxy_set_header Host $host;
                  proxy_set_header X-Real-IP $remote_addr;
                  proxy_pass http://127.0.0.1:3836;
          }
  }
  
  server {
          listen 80
          server_name api.example.com
  
          location / {
                  proxy_set_header Host $host;
                  proxy_set_header X-Real-IP $remote_addr;
                  proxy_pass http://127.0.0.1:3836;
          }
  }
  </code></pre>
  <p>I do not know the reason. Any suggestions on this?</p>