title: Host static website and api service on same server using nginx
body: <p>I want to serve my <code>static website</code> and <code>API service</code> from same machine using <a href="http://nginx.org" rel="nofollow noreferrer">nginx</a>.</p>
  <p>Website is present in <code>/var/www/html</code><br />
  API service is running on port <code>8000</code></p>
  <p><code>http://localhost</code> should open static website<br />
  <code>http://localhost/api</code> should proxy api service which is running on port <code>8000</code></p>
  <p>With the help of <a href="http://nginx.org/en/docs/beginners_guide.html" rel="nofollow noreferrer">http://nginx.org/en/docs/beginners_guide.html</a> I tried this config</p>
  <pre><code>server {
          listen 80 default_server;
          listen [::]:80 default_server;
  
          root /var/www/html;
  
          index index.html index.htm index.nginx-debian.html;
  
          server_name _;
  
          location / {
                  try_files $uri $uri/ =404;
          }
  
          location /api {
                  proxy_pass http://localhost:8000;
          }
  }
  </code></pre>
  <p><code>http://localhost</code> is working fine but <code>http://localhost/api</code> is giving me error <code>404</code>.</p>
  <p>What should be correct configuration to achive such infrastucture?</p>