简体   繁体   English

Docker + Apache + PHP-FPM 静态内容

[英]Docker + Apache + PHP-FPM static content

I am in the process of moving over some sites in PHP using Slim/Twig to docker containers.我正在使用 Slim/Twig 将 PHP 中的一些站点移动到 docker 容器。 In the old server a request came in slim routed the request sent back the html and then the browser made the call to get the resources CSS, images, etc. where apache took over.在旧服务器中,一个请求传入纤薄路由,请求发送回 html,然后浏览器调用以获取 apache 接管的资源 CSS、图像等。

Moving to Docker the httpd conf is essentially this:转移到 Docker,httpd conf 本质上是这样的:

ProxyPassMatch "^/(.*\.php\/(.*)?)$" "fcgi://php:9000/var/www/html/public/index.php/$2"

Still working all that out but this essentially forwards a request like http://192.168.33.20:8080/index.php/admin to fcgi://php:9000/var/www/html/public/index.php/$2 and Slim picks off all the rest of the path so admin and returns the correct view rendered by twig.仍在解决所有这些问题,但这基本上将像http://192.168.33.20:8080/index.php/admin这样的请求转发到fcgi://php:9000/var/www/html/public/index.php/$2和Slim 选择了路径的所有其余部分,因此admin并返回由 twig 呈现的正确视图。

Issue I am running into is the resources.我遇到的问题是资源。 I have most of my css and front-end frameworks in the php application via composer.我通过composer在php应用程序中拥有大部分css和前端框架。 So the browser makes another call to:因此浏览器再次调用:

/index.php/vendor/twbs/bootstrap/dist/js/bootstrap.bundle.js HTTP/1.1" 404

Getting a 404 because Slim has no idea what this path is and apache is just forwarding.得到404因为 Slim 不知道这条路径是什么,而 apache 只是转发。

What I have looked at and the cons:我看过什么和缺点:

  1. Placing the resources on Apache but this basically couple Apache and php like the server was.将资源放在 Apache 上,但这基本上像服务器一样将 Apache 和 php 结合起来。

  2. Creating a container to server these files ie another non-loadbalancer Apache to server these requests.创建一个容器来处理这些文件,即另一个非负载均衡器 Apache 来处理这些请求。 This still couples and also means I need to find a way to potentially store multiple versions in a blue green deployment.这仍然是耦合的,也意味着我需要找到一种方法来潜在地在蓝绿色部署中存储多个版本。

  3. Mount a volume to the Apache lb. Not sure why I abandoned this one.将一个卷挂载到 Apache lb。不知道为什么我放弃了这个。 Probably version issues.应该是版本问题。

  4. Create a route that can search for the resource.创建一个可以搜索资源的路由。 So far my favorite idea but added code complexity.到目前为止,我最喜欢的想法是增加了代码复杂性。

So the question I have is there a standard way of dealing with this in Docker?所以我的问题是在 Docker 中有一种标准的方法来处理这个问题吗?

My standard practice is to control resource access by framework route.我的标准做法是通过框架路由控制资源访问。 An example implementation (in Lumen):一个示例实现(在 Lumen 中):

$router->get('/asset[/{path:.*}]', 'AssetController@load');

An example request and response looks like this:示例请求和响应如下所示:

GET /asset/js/app.js HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
Accept: */*
Referer: http://localhost:8080/spa/example
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

HTTP/1.1 200 OK
Server: nginx
Content-Type: application/javascript
Content-Length: 21056410
Connection: keep-alive
Last-Modified: Sun, 31 Jul 63 19:34:21 +0000
Cache-Control: private, must-revalidate
Date: Wed, 31 Jul 2019 19:34:21 GMT
Accept-Ranges: bytes
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Security-Policy: default-src  'self'; connect-src  'self'; img-src  'self'; style-src 'unsafe-inline' fonts.googleapis.com 'self'; font-src fonts.gstatic.com 'self'; script-src 'unsafe-inline'  'self' 'unsafe-eval'

Your controller (eg, AssetController in my earlier example), would take care to translate the URL path to the file system path (watch out for .. and other tricks, use realpath and compare it to your code's installation base path), set the Content-Type and Content-Length headers, any relevant caching headers, and then stream the file.您的控制器(例如,我之前示例中的AssetController )会小心地将 URL 路径转换为文件系统路径(注意..和其他技巧,使用realpath并将其与代码的安装基本路径进行比较),设置Content-TypeContent-Length标头、任何相关的缓存标头,然后流式传输文件。

While that does add complexity and a certain amount of overhead, it provides the flexibility to constrain resources by business logic.虽然这确实增加了复杂性和一定数量的开销,但它提供了通过业务逻辑限制资源的灵活性。

If performance is a concern, you can promote the access to the web server or shield the route behind a CDN and proxy content.如果性能是一个问题,您可以促进对 Web 服务器的访问或屏蔽 CDN 和代理内容后面的路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM