<p>Consider the following nginx config file:</p>

<pre><code>server {
    listen 443;
    ssl    on;
    ssl_certificate     /etc/tls/cert.pem;
    ssl_certificate_key /etc/tls/key.pem;
    location / {
        proxy_pass http://api.default.svc.cluster.local;
    }
}
</code></pre>

<p>All incoming TCP requests on 443 should redirect to my server running on <code>api.default.svc.cluster.local:80</code> (which is a node REST-Api btw). This works fine, I can <code>curl https://&lt;nginx-IP&gt;/</code> nginx and get a correct response, as expected.</p>

<p>Now, I'd like to change the location from <code>/</code> to <code>/api</code>, so I can fire a <code>curl https://&lt;nginx-IP&gt;/api</code> in order to get the same response as before.</p>

<p><strong>1. Attempt</strong></p>

<p>So I change the location line in the config to:</p>

<p><code>location /api {</code></p>

<p>Unfortunately this won't work, instead I get an error <code>Cannot GET /api</code> which is a node error, so obviously it gets routed to the api but something's still smelly.</p>

<p><strong>2. Attempt</strong></p>

<p>It seems as the trailing slash in an URI is required so I added it to the location:</p>

<p><code>location /api/ {</code></p>

<p>Now something changed. I won't get the same error as before, instead I get an "301 moved permanently". How can I fix my nginx config file?</p>

<p><strong>Additional information regarding the environment</strong></p>

<p>I'm using a kubernetes deployment that deploys the nginx reverse proxy incl. the config introduced. I then expose nginx using a kubernetes service. Also, I tried using kubernetes ingress to deal with this situation, using the same routes, however, the ingress service would respond with a <code>default backend - 404</code> message.</p>

<p>As mentioned in the question, trailing slashes in URIs are important. I fixed this in the location, however, I didn't add it to the URI I pass using <code>proxy_pass</code>.</p>

<p>As for the nginx proxy I got it to work using the following config:</p>

<pre><code>server {
    listen 443;
    ssl    on;
    ssl_certificate     /etc/tls/cert.pem;
    ssl_certificate_key /etc/tls/key.pem;
    location /api/ {
        proxy_pass http://api.default.svc.cluster.local/;
    }
}
</code></pre>

<p>Concerning the ingress solution, I was not able to get it to work by adding the missing trailing slash to the path. The service is specified due its name and therefore no trailing slash can be added (i.e. it would result in an error). </p>
