简单的 docker-由两个服务组成:nginx 和 php
Simple docker-compose with two services: nginx and php
我从 Docker and nginx 开始,我正在尝试设置两个容器环境 运行:
nginx:latest
一侧
php:fpm
另一边
我在使用 php-fpm 时遇到问题:我总是收到 502 Bad Gateway 错误。
我的设置很简单($TEST_DIR
是我的工作目录)。
我的 Docker 撰写配置 TEST_DIR/docker-compose.yml
:
nginx:
image: nginx
ports:
- "8080:80"
volumes:
- ./www:/usr/share/nginx/html
- ./conf/nginx.conf:/nginx.conf
- ./logs/nginx:/var/log/nginx
links:
- php:php
command: nginx -c /nginx.conf
php:
image: php:fpm
ports:
- "9000:9000"
volumes:
- ./www:/var/www/html
nginx 配置$TEST_DIR/conf/nginx.conf
:
user nginx;
worker_processes 1;
pid /var/run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log off;
gzip on;
gzip_disable "msie6";
open_file_cache max=100;
upstream php-upstream {
server php:9000;
}
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# Pass PHP scripts to PHP-FPM
location ~* \.php$ {
fastcgi_pass php-upstream;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param HTTPS off;
}
error_log /var/log/nginx/php_error.log;
access_log /var/log/nginx/php_access.log;
}
}
daemon off;
然后,我将我的 PHP 内容放在与 docker-compose.yml
相同的目录中:
$TEST_DIR/www/test.php
<?php phpinfo(); ?>
如果我使用 docker-compose up
启动基础设施然后转到 localhost:8080/test.php
,那么我会得到 502 Bad Gateway
以及来自 nginx 的以下错误:
[error] 6#6: *1 connect() failed (113: No route to host) while connecting to upstream, client: 172.17.42.1, server: localhost, request: "GET /phpinsfo2.php HTTP/1.1", upstream: "fastcgi://172.17.0.221:9000", host: "localhost:8080"
导致错误的原因是什么?
我终于成功了。
问题是我的主机 (Fedora 21) 启用了防火墙。
这样做:systemctl stop firewalld
解决了我的问题。
显然这是 Red Hat 的一个众所周知的问题 Linux:
Bug 1098281 - Docker interferes with firewall initialisation via firewalld
我从 Docker and nginx 开始,我正在尝试设置两个容器环境 运行:
nginx:latest
一侧php:fpm
另一边
我在使用 php-fpm 时遇到问题:我总是收到 502 Bad Gateway 错误。
我的设置很简单($TEST_DIR
是我的工作目录)。
我的 Docker 撰写配置 TEST_DIR/docker-compose.yml
:
nginx:
image: nginx
ports:
- "8080:80"
volumes:
- ./www:/usr/share/nginx/html
- ./conf/nginx.conf:/nginx.conf
- ./logs/nginx:/var/log/nginx
links:
- php:php
command: nginx -c /nginx.conf
php:
image: php:fpm
ports:
- "9000:9000"
volumes:
- ./www:/var/www/html
nginx 配置$TEST_DIR/conf/nginx.conf
:
user nginx;
worker_processes 1;
pid /var/run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log off;
gzip on;
gzip_disable "msie6";
open_file_cache max=100;
upstream php-upstream {
server php:9000;
}
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# Pass PHP scripts to PHP-FPM
location ~* \.php$ {
fastcgi_pass php-upstream;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param HTTPS off;
}
error_log /var/log/nginx/php_error.log;
access_log /var/log/nginx/php_access.log;
}
}
daemon off;
然后,我将我的 PHP 内容放在与 docker-compose.yml
相同的目录中:
$TEST_DIR/www/test.php
<?php phpinfo(); ?>
如果我使用 docker-compose up
启动基础设施然后转到 localhost:8080/test.php
,那么我会得到 502 Bad Gateway
以及来自 nginx 的以下错误:
[error] 6#6: *1 connect() failed (113: No route to host) while connecting to upstream, client: 172.17.42.1, server: localhost, request: "GET /phpinsfo2.php HTTP/1.1", upstream: "fastcgi://172.17.0.221:9000", host: "localhost:8080"
导致错误的原因是什么?
我终于成功了。
问题是我的主机 (Fedora 21) 启用了防火墙。
这样做:systemctl stop firewalld
解决了我的问题。
显然这是 Red Hat 的一个众所周知的问题 Linux:
Bug 1098281 - Docker interferes with firewall initialisation via firewalld