<p>I have a angular application running in a docker ubuntu image that has nginx installed. I want to deploy this image to Kubernetes and use a nginx proxy to redirect all calls to /api to my backend service in Kubernetes.</p>

<p>My static web resources lie in /var/www/html and I add the following config to /etc/nginx/conf.d:</p>

<pre><code>upstream backend-service {
  server backend-service:8080;
}

server {
  listen 80;

  location / {
    try_files $uri $uri/ /index.html;
  }

  location ^~ /api {
    proxy_pass http://backend-service;
  }
}
</code></pre>

<p>Accessing the frontend service on / or /#/dashboard returns the expected component of my Angular page, but <strong>a call to /api/v1/data only shows the default nginx 404 Not Found page</strong>.</p>

<p>What do I need to modify to have my backend calls redirected to my backend?</p>

<p><strong>I use nginx 1.10.3 on ubuntu 16.04</strong> and my frontend Dockerfile looks like this:</p>

<pre><code>FROM ubuntu:16.04

# Install curl, nodejs and nginx
RUN apt-get update &amp;&amp; \
  apt-get install -y curl &amp;&amp; \
  curl -sL https://deb.nodesource.com/setup_8.x | bash - &amp;&amp; \
  apt-get install -y nodejs nginx &amp;&amp; \
  rm -rf /var/lib/apt/lists/*

# Create directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Copy and build rest of the app
COPY . /usr/src/app
RUN npm install
RUN node_modules/@angular/cli/bin/ng build --prod
RUN cp -a dist/. /var/www/html

# Configure and start nginx
COPY frontend.conf /etc/nginx/conf.d

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
</code></pre>

<p><strong>Edit: Information about backend-service</strong></p>

<p>The backend service <strong>listens to get and post requests on /api/v1/data</strong> and is reachable in Kubernetes via a Service named backend-service.</p>

<p><strong>Edit2: Nginx access.log</strong></p>

<p><a href="https://gist.github.com/Steffen911/a56e3175bf12e511048d01359a475724" rel="noreferrer">https://gist.github.com/Steffen911/a56e3175bf12e511048d01359a475724</a></p>

<pre><code>172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET / HTTP/1.1" 200 380 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /styles.d41d8cd98f00b204e980.bundle.css HTTP/1.1" 200 0 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /inline.c9a1a6b995c65c13f605.bundle.js HTTP/1.1" 200 1447 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /polyfills.117078cae3e3d00fc376.bundle.js HTTP/1.1" 200 97253 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /main.3e9a37b4dd0f3bf2465f.bundle.js HTTP/1.1" 200 64481 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /vendor.146173c1a99cc2172a5f.bundle.js HTTP/1.1" 200 661261 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /api/v1/data/ HTTP/1.1" 404 209 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /assets/home.jpg HTTP/1.1" 200 2608 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /assets/busy.gif HTTP/1.1" 200 48552 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /assets/background_light.png HTTP/1.1" 200 170599 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /assets/google.svg HTTP/1.1" 200 2232 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /assets/email.svg HTTP/1.1" 200 1596 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:40 +0000] "GET /favicon.ico HTTP/1.1" 200 198 "http://192.168.99.100:30497/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
172.17.0.1 - - [13/Aug/2017:13:11:44 +0000] "GET /api/v1/data/ HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
</code></pre>

<p>The error.log file is empty.</p>

<p><strong>Edit3: New nginx version and other SO thread</strong></p>

<p>I also tried nginx 1.12.1 and it shows the same behaviour. The answers to this question also haven't helped: <a href="https://stackoverflow.com/questions/16157893/nginx-proxy-pass-404-error-dont-understand-why">nginx proxy_pass 404 error, don&#39;t understand why</a></p>

<p><strong>Edit4: I uploaded a minimal example that reproduces my problem on GitHub</strong></p>

<p><a href="https://github.com/Steffen911/nginx-sample" rel="noreferrer">https://github.com/Steffen911/nginx-sample</a></p>

<p>From your troubleshooting of nginx, it appears that the nginx configuration file you have has effectively no effect — you report getting <code>404 Not Found</code> errors for everything other than the index page, with all the directives, from <code>try_files</code> in <code>location /</code>, to <code>proxy_pass</code> and <code>return 200 test</code> in a more specific <code>location ^~ /api</code>, having no effect.</p>

<p>As such, the problem appears to be in the <code>Dockerfile</code> — it appears that most other NGINX + Docker tutorials remove default configurations (e.g., with <code>RUN rm /etc/nginx/conf.d/default.conf</code>), whereas your file is missing any such removal.</p>

<p>In fact, <a href="https://packages.debian.org/sid/all/nginx-common/filelist" rel="noreferrer">Debian</a>/<a href="https://packages.ubuntu.com/xenial/all/nginx-common/filelist" rel="noreferrer">Ubuntu</a> appear to have the non-standard directories of questionable utility called <code>/etc/nginx/sites-available</code> and <a href="https://anonscm.debian.org/cgit/collab-maint/nginx.git/tree/debian/conf/nginx.conf#n62" rel="noreferrer"><code>/etc/nginx/sites-enabled</code></a>, which, by default, must contain a <a href="https://anonscm.debian.org/cgit/collab-maint/nginx.git/tree/debian/conf/sites-available/default" rel="noreferrer"><code>default</code></a> file with a presumptuous <code>listen 80 default_server</code>, effectively taking precedence over any other <a href="http://nginx.org/r/listen" rel="noreferrer"><code>listen</code></a> of the same port in the absence of a more specific <code>server_name</code>.</p>

<hr>

<p><strong><em>As such, there are multiple independent solutions:</em></strong></p>

<hr>

<ul>
<li><p><strong><em>Do not use fundamentally broken packages like those offered by Debian/Ubuntu.</em></strong>  I once spent a good amount of time pulling my hair trying to figure out why my configs don't work, only to notice that even the backup files from <a href="http://ports.su/editors/emacs" rel="noreferrer"><code>emacs</code></a> like <code>test.conf~</code> get included in Debian through Debian's default <code>include /etc/nginx/sites-enabled/*;</code>.
<strong><em>Sites-Enabled Is Evil.</em></strong></p>

<p>Note that <a href="http://nginx.org/en/linux_packages.html" rel="noreferrer">NGINX provides official binary packages for most distributions</a>, which wouldn't have had this issue, as it doesn't try to define a <code>default_server</code> in its <code>/etc/nginx/conf.d/default.conf</code>, instead doing a <code>listen 80;</code> with <code>server_name  localhost;</code>, getting out of your way automatically by itself in most circumstances.</p>

<p>E.g., replace <code>FROM ubuntu:16.04</code> with <code>FROM nginx</code> in your <code>Dockerfile</code> to be using NGINX official image.</p></li>
</ul>

<hr>

<ul>
<li><strong><em>If still using nginx from Debian/Ubuntu</em></strong>, make sure to <strong><code>RUN rm /etc/nginx/sites-enabled/default</code></strong> in your <strong><code>Dockerfile</code></strong> to remove the <code>default_server</code> <code>listen</code>.</li>
</ul>

<hr>

<ul>
<li><p><strong><em>Use the <a href="http://nginx.org/r/server_name" rel="noreferrer"><code>server_name</code></a> directive to define hostname-based servers</em></strong>, possibly together with the <a href="http://nginx.org/r/listen" rel="noreferrer"><code>listen</code></a> directive with the <code>default_server</code> parameter, too.</p>

<p>Note that a duplicate <code>server_name</code> specification results in a configuration warning (with the <code>[warn]</code> severity), but a duplicate <code>default_server</code> is a configuration error (<code>[emerg]</code> severity), which might help troubleshoot the issue earlier.</p></li>
</ul>
