中午用docker不熟nginx+php-fpm 环境时,访问php文件报404 Not Found错误, 访问静态资源是可以正常访问返回码200。
查看容器日志
[root@hecs-136019 ~]# more /data/docker/nginx/logs/error.log
2023/05/31 06:38:44 [error] 6#0: *5 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 61.164.44.227, server: localhost, request: "G
ET /index.php HTTP/1.1", upstream: "fastcgi://172.17.201.8:9000", host: "121.37.166.201:8081"
2023/05/31 06:38:44 [error] 6#0: *5 open() "/usr/local/nginx/html/50x.html" failed (2: No such file or directory), client: 61.164.44.227, server: localhost, request: "GET /index.ph
p HTTP/1.1", upstream: "fastcgi://172.17.201.8:9000", host: "121.37.166.201:8081"
根据日志反馈,定位问题:
1、nginx与php-fpm容器网络不通
2、nginx、php-fpm配置文件
验证:
1、查看网络是否连通
[root@hecs-136019 ~]# docker exec -it 34f07196ec86 /bin/bash
[root@34f07196ec86 html]# ping 172.17.201.8
PING 172.17.201.8 (172.17.201.8) 56(84) bytes of data.
64 bytes from 172.17.201.8: icmp_seq=1 ttl=64 time=0.074 ms
64 bytes from 172.17.201.8: icmp_seq=2 ttl=64 time=0.063 ms
^C
--- 172.17.201.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.063/0.068/0.074/0.009 ms
[root@34f07196ec86 html]#
结论: 网络没有问题
验证:
2
查看php-fpm容器IP
root@hecs-136019 ~]# docker inspect nginx-php-fpm | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.17.201.8",
查看nginx容器IP
[root@hecs-136019 ~]# docker inspect 34f07196ec86 | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.17.201.6",
[root@hecs-136019 ~]#
容器 | IP |
nginx | 172.17.201.6 |
php-fpm | 172.17.201.8 |
查看nginx配置, 开启php模块, 把fastcgi_pass 地址更改为php容器的地址 IP:端口
#user nobody;
worker_processes 1;
error_log /var/log/nginx/error.log; #开启错误日志,便于查询
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/local/nginx/html; # php容器的path
fastcgi_pass 172.17.201.8:9000; #注意,此处是php-fpm容器的IP
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
......
更改php-fpm配置
[www]
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 0.0.0.0:9000 # 监听所有服务器的9000端口(默认是本机127.0.0.1:9000)
; Set listen(2) backlog. A value of '-1' means unlimited.
; Default Value: -1
;listen.backlog = -1
; List of ipv4 addresses of FastCGI clients which are allowed to connect.
; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
; must be separated by a comma. If this value is left blank, connections will be
; accepted from any ip address.
; Default Value: any
listen.allowed_clients = 172.17.201.6 #更改为nginx容器的IP(默认是本机127.0.0.1)
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
; mode is set to 0666
;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0666
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache
重启服务
[root@hecs-136019 ~]# docker restart nginx-php-fpm
nginx-php-fpm
[root@hecs-136019 ~]# docker restart 34f07196ec86
34f07196ec86
验证
[root@hecs-136019 ~]# curl http://121.37.166.201:8081/info.php
Array ( [REMOTE_ADDR] => 121.37.166.201 [REQUEST_METHOD] => GET [HTTP_USER_AGENT] => curl/7.29.0 [REQUEST_URI] => /info.php ) php_host: a2fe7cae0c3c 1229