[英]How to setup php-fpm and nginx on 2 separate containers with different volume paths
I'm attempting to setup a development environment using docker using 2 containers: nginx
and php7-fpm
. 我正在尝试使用
php7-fpm
使用2个容器设置开发环境: nginx
和php7-fpm
。
What I want to happen is when a user visits any URL that contains /api
it uses php-fpm, but everything else is loaded from /var/www/html
. 我想发生的是,当用户访问包含
/api
任何URL时,它都使用php-fpm,但是其他所有内容都从/var/www/html
加载。
Here is my configuration: 这是我的配置:
site.conf: site.conf:
server {
index index.html;
server_name impressive.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location /api {
index index.php;
alias /var/www/api;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}
docker-compose.yml 泊坞窗,compose.yml
web:
image: nginx
volumes:
- ./frontend/public:/var/www/html
- ./site.conf:/etc/nginx/conf.d/site.conf
links: [ php ]
ports:
- "8080:80"
environment:
- NGINX_HOST=http://impressive.local
- NGINX_PORT=80
php:
image: php:7-fpm
volumes:
- ./api:/var/www/api
This doesn't work as expected and when I visit impressive.local/api
I get the following error in logs: 这无法正常工作,当我访问
impressive.local/api
在日志中出现以下错误:
web_1 | 2019/01/10 12:23:47 [error] 6#6: *1 "/var/www/api/index.php" is not found (2: No such file or directory), client: 172.17.0.1, server: impressive.local, request: "GET /api/ HTTP/1.1", host: "impressive.local:8080"
I realize that the php-fpm container is the one that contains the /var/www/api
directory and not nginx. 我意识到php-fpm容器是一个包含
/var/www/api
目录而不是nginx的容器。 With my configuration nginx is trying to alias
to a non existent path and is thus failing. 通过我的配置,nginx尝试将
alias
为不存在的路径,因此失败了。
My question is how is possible to achieve this? 我的问题是如何实现这一目标?
Yes, I use exactly this configuration for all of my Laravel apps. 是的,我对所有Laravel应用程序都完全使用了此配置。
Here is an example of my configuration... 这是我的配置示例...
version: '2'
services:
app:
container_name: app
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=x.x.x.x"
web:
container_name: web
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes_from:
- app
ports:
- 8080:80
As you can see you specify to use the volume from your web container. 如您所见,您指定使用Web容器中的卷。
I think the configuration file is invalid; 我认为配置文件无效; try this code to fix [ docker-compose.yml and site.conf ]
试试这个代码来修复[docker-compose.yml和site.conf]
docker-compose.yml
泊坞窗,compose.yml
version: '2'
services:
web:
image: nginx
volumes:
- ./frontend/public:/var/www/html
- ./site.conf:/etc/nginx/conf.d/site.conf
ports:
- "8080:80"
environment:
- NGINX_HOST=impressive.local
- NGINX_PORT=80
links:
- php
php:
image: php:7-fpm
volumes:
- ./api:/var/www/api
- ./frontend/public:/var/www/html
site.conf
site.conf
server {
index index.html index.php;
server_name impressive.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /api/ {
alias /var/www/api;
}
location ~ ^/api/(.+\.php)$ {
alias /var/www/api;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# pass the PHP scripts to FastCGI server listening on php:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
finally, run docker-compose build
and docker-compose up
最后,运行
docker-compose build
和docker-compose up
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.