广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >docker部署nginx+php-fpm环境访问动态文件404报错
  • 363
分享到

docker部署nginx+php-fpm环境访问动态文件404报错

nginxdockerphp 2023-08-31 21:08:19 363人浏览 薄情痞子
摘要

中午用Docker不熟Nginx+PHP-fpm 环境时,访问php文件报404 Not Found错误, 访问静态资源是可以正常访问返回码200。  查看容器日志 [root@hecs-136019 ~]# more /data/doc

中午用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: "GET /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.php HTTP/1.1", upstream: "fastcgi://172.17.201.8:9000", host: "121.37.166.201:8081"

根据日志反馈,定位问题:

nginx与php-fpm容器网络不通

nginx、php-fpm配置文件

验证:

查看网络是否连通

[root@hecs-136019 ~]# docker exec -it 34f07196ec86 /bin/bash[root@34f07196ec86 html]# ping 172.17.201.8PING 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 ms64 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 999msrtt 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
nginx172.17.201.6
php-fpm172.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: anylisten.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 httpduser = 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 34f07196ec8634f07196ec86

验证

[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:       a2fe7cae0c3c1229

 

来源地址:https://blog.csdn.net/weixin_70661795/article/details/130968646

--结束END--

本文标题: docker部署nginx+php-fpm环境访问动态文件404报错

本文链接: https://www.lsjlt.com/news/387229.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作