php-fpm 111: connection refused,connect() failed (111: Connection refused) while connecting to upstr...
问题IntroductionI 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 wh
问题
Introduction
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
[error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: app, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:8000"
Searched answers
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
However, none of them solved my problems.
Files
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
In addition to that, here is the code
General
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"
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 ;
# 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;
}
回答1:
This is cause the 127.0.0.1 should be the name of your php service not the localhost
So change
fastcgi_pass php-upstream;
to
fastcgi_pass php:9000;
Or similar in the used upstream directive.
php is reachable from your nginx if its in the same network (which obvously is through code-network)
来源:https://stackoverflow.com/questions/51122146/connect-failed-111-connection-refused-while-connecting-to-upstream-for-ngin
更多推荐
所有评论(0)