CapRover serving static files from host

1.1k Views Asked by At

I want to serve static files located on the host server using CapRover.

What I did:

  1. Created directory structure /srv/foo/bar.
  2. Ensured bar permission is drw-rw-r-- and all files inside it are -rw-r--r--.
  3. Created new app in CapRover dashboard called app.
  4. Modified its Nginx config (scroll all the way down to see my changes):
<%
if (s.forceSsl) {
%>
    server {
        listen       80;
        server_name  <%-s.publicDomain%>;

        # Used by Lets Encrypt
        location /.well-known/acme-challenge/ {
            root <%-s.staticWebRoot%>;
        }

        # Used by CapRover for health check
        location /.well-known/captain-identifier {
            root <%-s.staticWebRoot%>;
        }

        location / {
            return 302 https://$http_host$request_uri;
        }
    }
<%
}
%>

server {
    <%
    if (!s.forceSsl) {
    %>
        listen       80;
    <%
    }
    if (s.hasSsl) {
    %>
        listen              443 ssl http2;
        ssl_certificate     <%-s.crtPath%>;
        ssl_certificate_key <%-s.keyPath%>;
    <%
    }
    %>

        client_max_body_size 500m;
        server_name  <%-s.publicDomain%>;

        # 127.0.0.11 is DNS set up by Docker, see:
        # https://docs.docker.com/engine/userguide/networking/configure-dns/
        # https://github.com/moby/moby/issues/20026
        resolver 127.0.0.11 valid=10s;
        # IMPORTANT!! If you are here from an old thread to set a custom port, you do not need to modify this port manually here!!
        # Simply change the Container HTTP Port from the dashboard HTTP panel
        set $upstream http://<%-s.localDomain%>:<%-s.containerHttpPort%>;

        # THIS IS WHAT I CHANGED
        location / {
            root /srv/foo/bar;
        }

        # Used by Lets Encrypt
        location /.well-known/acme-challenge/ {
            root <%-s.staticWebRoot%>;
        }
        
        # Used by CapRover for health check
        location /.well-known/captain-identifier {
            root <%-s.staticWebRoot%>;
        }

        error_page 502 /captain_502_custom_error_page.html;
        location = /captain_502_custom_error_page.html {
                root <%-s.customErrorPagesDirectory%>;
                internal;
        }
}
  1. I even set persistent directory mapping for the app, /srv/foo to /srv/foo.

But when I open https://app.caprover.mydomain.com/test.jpg from a browser:

404 Not Found
nginx

So I checked Nginx log (docker service logs captain-nginx --follow):

... *4773 open() "/srv/foo/bar/test.jpg" failed (2: No such file or directory), client: 1.2.3.4, server: app.caprover.mydomain.com, request: "GET /test.jpg HTTP/1.1", host: "app.caprover.mydomain.com"
... "app.caprover.mydomain.com" "GET /test.jpg HTTP/1.1" 404 548 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" "-"

What do I miss?

1

There are 1 best solutions below

0
On

Nginx is looking inside the container for the path to /srv/foo/bar I you have created the directory outside of the caprover nginx container, then it has to be mapped into the container to enable nginx to access it.