从主机提供静态文件的CapRover



我想使用CapRover提供位于主机服务器上的静态文件。

我做了什么:

  1. 创建目录结构/srv/foo/bar.
  2. 确保bar权限为drw-rw-r--,其中所有文件为-rw-r--r--
  3. 在CapRover仪表板中创建了新的应用程序app
  4. 修改了它的Nginx配置(向下滚动查看我的更改):
<%
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. 我甚至为应用程序设置了持久的目录映射,/srv/foo/srv/foo

但是当我从浏览器打开https://app.caprover.mydomain.com/test.jpg时:

404 Not Found
nginx

所以我检查了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)" "-"

我错过了什么?

Nginx正在容器中寻找/srv/foo/bar的路径如果你已经在caprover nginx容器之外创建了目录,那么它必须被映射到容器中以使nginx能够访问它。

最新更新