Thi*_*ico 10 nginx static-content php-fpm docker
我正在尝试使用 docker 配置一个 php webapp。这个想法是php-fpm
在一个独立的容器中运行应用程序,并有另一个运行 nginx 的容器。此设置的想法是使用相同的 nginx 容器将请求代理到已经在同一台机器上工作的其他 web 应用程序。问题是我无法nginx
正确处理静态文件(js、css 等),因为对这些文件的请求一直转到fpm
.
这是文件系统的样子:
/
??? Makefile
??? config
? ??? webapp.config
??? webapp
??? web
??? index.php
??? static.js
Run Code Online (Sandbox Code Playgroud)
我正在使用Makefile
看起来像这样的(对此不感兴趣docker-compose
)运行整个事情:
PWD:=$(shell pwd)
CONFIG:='/config'
WEBAPP:='/webapp'
run: | run-network run-webapp run-nginx
run-network:
docker network create internal-net
run-webapp:
docker run --rm \
--name=webapp \
--net=internal-net \
--volume=$(PWD)$(WEBAPP):/var/www/webapp:ro \
-p 9000:9000 \
php:5.6.22-fpm-alpine
run-nginx:
docker run --rm \
--name=nginx \
--net=internal-net \
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro \
-p 80:80 \
nginx:1.11.0-alpine
Run Code Online (Sandbox Code Playgroud)
这就是我的config/webapp.conf
样子。
server {
listen 80;
server_name webapp.domain.com;
# This is where the index.php file is located in the webapp container
# This folder will contain an index.php file and some static files that should be accessed directly
root /var/www/webapp/web;
location / {
try_files $uri $uri/ @webapp;
}
location @webapp {
rewrite ^(.*)$ /index.php$1 last;
}
location ~ ^/index\.php(/|$) {
include fastcgi_params;
fastcgi_pass webapp:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
Run Code Online (Sandbox Code Playgroud)
需要使用该index.php
文件处理的任何操作都将起作用。但是,不会提供静态文件,从而导致严重404
错误(因为 php webapp 并没有真正为这些配置路由)。我相信 nginx 会尝试从它自己的容器文件系统中加载它们,当它们实际上在webapp
容器中时,失败回@webapp
.
有没有办法可以配置nginx
为驻留在另一个容器中的那些文件提供服务?
webapp
我设法通过将卷安装到容器中来解决这个问题nginx
。现在的工作是这样run-nginx
的:
run-nginx:
docker run --rm \
--name=nginx \
--net=internal-net \
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro \
--volume=$(PWD)$(WEBAPP)/web:/var/www/webapp/web:ro \
-p 80:80 \
nginx:1.11.0-alpine
Run Code Online (Sandbox Code Playgroud)
这是一个webapp.conf
文件,它将尝试从容器加载静态文件,如果不可能,则会将请求代理给工作fpm
人员:
server {
listen 80;
server_name webapp.domain.com;
root /var/www/webapp/web;
location ~ \.(js|css|png) {
try_files $uri $uri/;
}
location / {
rewrite ^(.*)$ /index.php$1 last;
}
location ~ ^/index\.php(/|$) {
include fastcgi_params;
fastcgi_pass webapp:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我想知道是否有更好的方法来代替两次共享同一卷。多谢!
归档时间: |
|
查看次数: |
5642 次 |
最近记录: |