<p><strong>GOAL</strong>
I want to get access to kubernetes dashboard with a standalone nginx service and a microk8s nodeport service.</p>
<p><strong>CONTEXT</strong>
I have a linux server.
On this server, there are several running services such as:</p>
<ul>
<li>microk8s</li>
<li>nginx (note: I am not using ingress, nginx service works independently from microk8s).</li>
</ul>
<p>Here is the workflow that I am looking for:</p>
<ol>
<li>http:// URL /dashboard</li>
<li>NGINX service (FROM http:// URL /dashboard TO nodeIpAddress:nodeport)</li>
<li>nodePort service</li>
<li>kubernetes dashboard service</li>
</ol>
<p><strong>ISSUE:</strong>
However, each time I request http:// URL /dashboard I receive a <strong>502 bad request</strong> answer, what I am missing?</p>
<p><strong>CONFIGURATION</strong>
Please find below, nginx configuration, node port service configuration and the status of microk8s cluster:</p>
<p><a href="https://i.stack.imgur.com/s2i6p.png" rel="nofollow noreferrer">nginx configuration: /etc/nginx/site-availables/default</a></p>
<p><a href="https://i.stack.imgur.com/exeDh.png" rel="nofollow noreferrer">node-port-service configuration</a></p>
<p><a href="https://i.stack.imgur.com/qp27a.png" rel="nofollow noreferrer">node ip address</a></p>
<p><a href="https://i.stack.imgur.com/qT5eb.png" rel="nofollow noreferrer">microk8s namespaces</a></p>
<p>Thank you very much for your helps.</p>

itbob
<p>I'll summarize the whole problem and solutions here.</p>
<p>First, the service which needs to expose the Kubernetes Dashboard needs to point at the right target port, and also needs to select the right Pod (the kubernetes-dashboard Pod)</p>
<p>If you check your service with a:</p>
<pre><code>kubectl desribe service &lt;service-name&gt;
</code></pre>
<p>You can easily see if its selecting a Pod (or more than one) or nothing, by looking at the Endpoints section. In general, your service should have the same selector, port, targetPort and so on of the standard kubernetes-dashboard service (which expose the dashboard but only internally to the cluster)</p>
<p>Second, your NGINX configuration proxy the location /dashboard to the service, but the problem is that the kubernetes-dashboard Pod is expecting requests to reach / directly, so the path /dashboard means nothing to it.</p>
<p>To solve the second problem, there are a few ways, but they all lay in the NGINX configuration. If you read the documentation of the module proxy (aka <a href="http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass" rel="nofollow noreferrer">http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass</a>) you can see that the solution is to add an URI in the configuration, something like this:</p>
<pre><code>proxy_pass https://51.68.123.169:30000/
</code></pre>
<p>Notice the trailing slash, that is the URI, which means that the location matching the proxy rule is rewritten into /. This means that your_url/dashboard will just become your_url/</p>
<p>Without the trailing slash, your location is passed to the target as it is, since the target is only and endpoint.</p>
<p>If you need more complex URI changes, what you're searching is a rewrite rule (they support regex and lots more) but adding the trailing slash should solve your second problem.</p>
