我有一个 Docker Compose 文件,其中包含几个基于 PHP/FastCGI 的 Web 应用程序,我想通过 Nginx 公开这些应用程序(总共 3 个容器)。如果两个 Web 应用程序容器将其静态文件存储在“/var/www/html”上,我如何让 Nginx 容器直接提供静态文件?
我根本找不到办法做到这一点,这意味着我必须让这两个网络应用程序来提供它们自己的静态文件。如果我无法更改应用程序代码(就像此处的情况),则需要从 FastCGI 迁移到 HTTP。
如果有一种方法可以将容器卷挂载到不同的路径上,就像在主机系统上一样,那么这就是可行的。除了通过主机系统完成之外,还有什么方法可以完成这样的事情吗?
最佳答案
解决方案是将 /var/www/html
文件夹直接挂载到 nginx,以使用 serving static files 的 nginx 功能。为了简单起见,我还建议使用 data-container将 html 文件夹挂载到所有 3 个容器。最终的 docker-compose.yml 如下所示:
nginx:
image: nginx
ports:
- 80:80
links:
- app1
- app2
volumes_from:
- html
app1:
image: [app1]
volumes_from:
- html
app2:
image: [app2]
volumes_from:
- html
html:
image: busybox
volumes:
- html:/var/www/html
nginx 配置如下:
server {
root /var/www/html;
location / {
}
location ~ \.php {
fastcgi_pass localhost:8000;
...
}
location /images/ {
root /var/www/html/images;
}
location ~ \.(mp3|mp4) {
root /var/www/html;
}
}
上述配置将通过静态资源服务器 /images
文件夹和任何 mp3/mp4
文件。
关于docker - 直接通过容器化前端 Web 服务器提供静态文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30493935/