WSL2 Nginx + PHP FPM 在连接到上游时失败(111:连接被拒绝),客户端:172.23.0.1,上游:"fastcgi://172.23.0.3:9001"
WSL2 Nginx + PHP FPM failed (111: Connection refused) while connecting to upstream, client: 172.23.0.1, upstream: "fastcgi://172.23.0.3:9001"
每次我尝试通过 localhost:8000 / 127.0.0.1:8000 访问 nginx 时,我都会遇到此错误:
nginx_1 | 2022/05/29 13:28:57 [error] 32#32: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.22.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://172.22.0.5:9001", host: "localhost:8000", referrer: "http://localhost:8000/"
前一天docker 配置成功
我创建了 github repo 以便于代码审查
我已经尝试过的:
- 更改 FPM/Nginx 端口
- 重启WSL/Docker/PC
- 新建 symfony 项目
- 在同一网络添加php && nginx 容器
docker-撰写:
version: '3.7'
services:
database:
image: postgres:11-alpine
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: symfony
POSTGRES_DB: main
ports:
- 15432:5432
php:
build: ./docker/php
ports: ['9001:9000']
volumes: ['./symfony/:/var/www/symfony:cached']
depends_on:
- database
nginx:
build: ./docker/nginx
ports: ['8000:80']
volumes: ['./symfony/:/var/www/symfony:cached']
adminer:
image: adminer
restart: always
links:
- database
ports:
- 8081:8080
symfony.conf:
upstream php-upstream {
server php:9001;
}
server {
listen 80;
root /var/www/symfony/public;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
fastcgi_pass php-upstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
nginx.conf:
user nobody;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 4000;
multi_accept on;
use epoll;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log off;
gzip on;
gzip_min_length 10240;
gzip_comp_level 1;
gzip_vary on;
gzip_disable msie6;
gzip_proxied expired no-cache no-store private auth;
gzip_types
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
application/atom+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
reset_timedout_connection on;
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
client_body_temp_path /tmp 1 2;
client_body_buffer_size 256k;
client_body_in_file_only off;
}
daemon off;
您正在尝试使用主机上映射的端口 9001
连接到使用其服务名称的容器。
您有两个选择:
在您的 nginx 上游使用容器正在侦听的端口 9000
。 php:9000
将上游转发给带host.docker.internal:9001
的主机。
奖励:使用 Unix 套接字,但那是另一回事。
当服务共享网络并且必须相互通信时,我通常会使用“内部”端口。这样,您就可以将网络流量保持在该网络内。因此,如果您使用 TCP/IP 进行连接,“解决方案 1”将是最佳方法。另外,如果您不需要从 Docker 网络外部连接,则不必在主机上映射端口。
所以...这应该有效:
upstream php-upstream {
server php:9000;
}
server {
listen 80;
root /var/www/symfony/public;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
fastcgi_pass php-upstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
每次我尝试通过 localhost:8000 / 127.0.0.1:8000 访问 nginx 时,我都会遇到此错误:
nginx_1 | 2022/05/29 13:28:57 [error] 32#32: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.22.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://172.22.0.5:9001", host: "localhost:8000", referrer: "http://localhost:8000/"
前一天docker 配置成功
我创建了 github repo 以便于代码审查
我已经尝试过的:
- 更改 FPM/Nginx 端口
- 重启WSL/Docker/PC
- 新建 symfony 项目
- 在同一网络添加php && nginx 容器
docker-撰写:
version: '3.7'
services:
database:
image: postgres:11-alpine
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: symfony
POSTGRES_DB: main
ports:
- 15432:5432
php:
build: ./docker/php
ports: ['9001:9000']
volumes: ['./symfony/:/var/www/symfony:cached']
depends_on:
- database
nginx:
build: ./docker/nginx
ports: ['8000:80']
volumes: ['./symfony/:/var/www/symfony:cached']
adminer:
image: adminer
restart: always
links:
- database
ports:
- 8081:8080
symfony.conf:
upstream php-upstream {
server php:9001;
}
server {
listen 80;
root /var/www/symfony/public;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
fastcgi_pass php-upstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
nginx.conf:
user nobody;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 4000;
multi_accept on;
use epoll;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log off;
gzip on;
gzip_min_length 10240;
gzip_comp_level 1;
gzip_vary on;
gzip_disable msie6;
gzip_proxied expired no-cache no-store private auth;
gzip_types
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
application/atom+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
reset_timedout_connection on;
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
client_body_temp_path /tmp 1 2;
client_body_buffer_size 256k;
client_body_in_file_only off;
}
daemon off;
您正在尝试使用主机上映射的端口 9001
连接到使用其服务名称的容器。
您有两个选择:
在您的 nginx 上游使用容器正在侦听的端口
9000
。php:9000
将上游转发给带
host.docker.internal:9001
的主机。奖励:使用 Unix 套接字,但那是另一回事。
当服务共享网络并且必须相互通信时,我通常会使用“内部”端口。这样,您就可以将网络流量保持在该网络内。因此,如果您使用 TCP/IP 进行连接,“解决方案 1”将是最佳方法。另外,如果您不需要从 Docker 网络外部连接,则不必在主机上映射端口。
所以...这应该有效:
upstream php-upstream {
server php:9000;
}
server {
listen 80;
root /var/www/symfony/public;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
fastcgi_pass php-upstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}