2

I am trying to host static files in kubernetes with an nginx container, and expose them on a private network with istio.

I want the root of my server to exist at site.com/foo, as I have other applications existing at site.com/bar, site.com/boo, etc.

My istio virtual service configuration:

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: cluster-foo
  namespace: namespace
spec:
  gateways:
  - cluster-gateway
  hosts:
  - site.com
  http:
  - match:
    - name: http
      port: 80
      uri:
        prefix: /foo
    route:
    - destination:
        host: app.namespace.svc.cluster.local
        port:
          number: 80

All of my static files exist in the directory /data on my nginx container. My nginx config:

events {}
http {
    server {
        root /data;

        location /foo {
            autoindex on;
        }
    }
}

Applying this virtual service, and a kube deployment that runs an nginx container with this config, I get a nginx server at site.com/foo that serves all of the static files in /data on the container. All good. The problem is that the autoindexing that nginx does, does not respect the prefix /foo. So all the file links that nginx indexes at site.com/foo, look like this: site.com/page.html, rather than site.com/foo/page.html. Furthermore, when I put site.com/foo/page.html in my browser manually, site.com/foo/page.html is displayed correctly, So I know that nginx is serving it in the correct location, just the links that it is indexing are incorrect.

Is there any way to configure nginx autoindex with a prefix?

Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
DCoolaid
  • 21
  • 2
  • Try adding a trailing slash to your location definition: `location /foo/ {...}` – anemyte Dec 03 '20 at 19:48
  • Kubernetes seems to not like that. When I try to apply the deployment, the container enters a crash loop. – DCoolaid Dec 03 '20 at 22:07
  • I meant in NGINX config but forget it anyway. Can you show how your broken links look like? Are they absolute ('/some/path') or relative ('some/path')? – anemyte Dec 04 '20 at 06:37
  • Have you tried to use [alias](https://stackoverflow.com/a/10647080) instead of root? I have tried with below server `http { server { listen 80; location / { root /usr/share/nginx/html; index index.html index.htm; } location /a { alias /usr/share/nginx/html; index page.html page.htm; autoindex on; }` And as there is no page.html it shows me `Index of /a/` when i put `site.com/a` in my browser. – Jakub Dec 04 '20 at 11:51
  • @anemyte I meant that I added that location definition to my nginx.conf, but when I then applied the deployment in my kube cluster, the pod entered a crash loop. I am not sure what you mean by are the links absolute. The link addresses are `site.com/link.html` rather than the desired `site.com/foo/link.html` – DCoolaid Dec 04 '20 at 22:34

0 Answers0