在连接到Nginx + php-fpm docker的上游时,connect()失败(111:连接被拒绝) [英] connect() failed (111: Connection refused) while connecting to upstream for nginx+php-fpm docker
问题描述
我想创建一个由 php-fpm
, nginx
和 docker
,我想拥有 php-fpm
和 nginx
在单独的容器上。为此,我遵循了本教程,但现在在 localhost:8000
下访问我的Web服务器时卡住了。我的浏览器显示502,而我的 /var/log/nginx/project_error.log
已满
I want to create a site that is powered by php-fpm
, nginx
and docker
and I want to have php-fpm
and nginx
on separate containers. To do this, I followed this tutorial but am stuck now when accessing my web server under localhost:8000
. My browser displays a 502 and my /var/log/nginx/project_error.log
is full with
[错误] 7#7:* 1连接到上游时,connect()失败(111:连接被拒绝),客户端:172.20.0.1,服务器:app,请求: GET / HTTP / 1.1,上游: fastcgi://127.0.0.1:9000,主机: localhost:8000
我已经在该主题中进行了一些搜索,找到了以下线程:
I already searched a bit for this topic and found these threads:
- https://serverfault.com/questions/827781/docker-nginx-and-php7-error-111-connection-refused -while-connecting-to-upstream
- https://serverfault.com/questions/546349/what-is-the-difference-between-using-upstream-and-location-for-php-fpm
- https:// serverfault .com / questions / 840970 / nginx-connect-failed-111-connection-refused-while-connecting-to-upstream
- https://serverfault.com/questions/827781/docker-nginx-and-php7-error-111-connection-refused-while-connecting-to-upstream
- https://serverfault.com/questions/546349/what-is-the-difference-between-using-upstream-and-location-for-php-fpm
- https://serverfault.com/questions/840970/nginx-connect-failed-111-connection-refused-while-connecting-to-upstream
但是,它们都没有解决我的问题。
However, none of them solved my problems.
我创建了一个包含所有文件的GitHub存储库,因此任何人都可以在那里简单地对其进行检查(我认为这比将所有文件都转储到此处要好)。为此,只需做
I created a GitHub repository containing all files, so anyone can simply check it out there (I think it's better than having all files dumped in here). To do that, simply do
git clone git@github.com:tzfrs / dockertest.git
mkdir -p citynavigator / public
touch citynavigator / public / index.php
除此之外,这是代码
docker-compose.yml
version: "2"
services:
db:
image: mysql:5.6
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=citynavigatorrootpass
- MYSQL_DATABASE=citynavigator
- MYSQL_USER=citynavigator
- MYSQL_PASSWORD=citynavigatorpass
volumes:
- /var/lib/mysql
nginx:
image: nginx:latest
ports:
- "8000:80"
volumes:
- ../citynavigator:/var/www/app
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/app.conf:/etc/nginx/conf.d/default.conf
- ./nginx/fastcgi_params:/etc/nginx/fastcgi_params
links:
- php
networks:
- code-network
php:
build: fpm/
volumes:
- ../citynavigator:/var/www/app
networks:
- code-network
networks:
code-network:
driver: bridge
fpm / Dockerfile
FROM php:7.2-fpm
LABEL maintainer="Theo Tzaferis<t.tzfrs@gmail.com>"
USER root
RUN rm /usr/local/etc/php-fpm.d/docker.conf \
&& rm /usr/local/etc/php-fpm.d/www.conf \
&& rm /usr/local/etc/php-fpm.d/www.conf.default \
&& rm /usr/local/etc/php-fpm.d/zz-docker.conf \
&& rm -rf /var/www/html
ADD app.pool.conf /usr/local/etc/php-fpm.d/app.pool.conf
fpm / app.pool.conf
[app]
user = www-data
group = www-data
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 20
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
catch_workers_output = yes
nginx / app.conf
upstream php-upstream {
server 127.0.0.1:9000;
}
server {
server_name app;
root /var/www/app/public;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass php-upstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# optionally set the value of the environment variables used in the application
# fastcgi_param APP_ENV prod;
# fastcgi_param APP_SECRET <app-secret-id>;
# fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name";
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/index.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
nginx / fastcgi_params
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param HTTPS $https;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
# fastcgi_param REDIRECT_STATUS 200;
# Allow 10 minute script execution time
fastcgi_read_timeout 600s;
nginx / nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
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";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;
include /etc/nginx/conf.d/*.conf;
open_file_cache max=100;
client_max_body_size 30m;
}
推荐答案
这是造成127.0的原因。 0.1应该是您的php服务的名称,而不是localhost
This is cause the 127.0.0.1 should be the name of your php service not the localhost
因此更改
fastcgi_pass php-upstream;
至
fastcgi_pass php:9000;
或与所使用的上游指令类似。
Or similar in the used upstream directive.
php
如果您的nginx在同一网络中(可以通过代码网络进行访问),则可以从您的nginx到达
php
is reachable from your nginx if its in the same network (which obvously is through code-network)
这篇关于在连接到Nginx + php-fpm docker的上游时,connect()失败(111:连接被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!