• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
菜鸟来咯
帮助1

中午用docker不熟nginx php-fpm 环境时,访问php文件报404 Not Found错误, 访问静态资源是可以正常访问返回码200。 

学新通

查看容器日志

  1.  
    [root@hecs-136019 ~]# more /data/docker/nginx/logs/error.log
  2.  
    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
  3.  
    ET /index.php HTTP/1.1", upstream: "fastcgi://172.17.201.8:9000", host: "121.37.166.201:8081"
  4.  
    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
  5.  
    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、查看网络是否连通

  1.  
    [root@hecs-136019 ~]# docker exec -it 34f07196ec86 /bin/bash
  2.  
    [root@34f07196ec86 html]# ping 172.17.201.8
  3.  
    PING 172.17.201.8 (172.17.201.8) 56(84) bytes of data.
  4.  
    64 bytes from 172.17.201.8: icmp_seq=1 ttl=64 time=0.074 ms
  5.  
    64 bytes from 172.17.201.8: icmp_seq=2 ttl=64 time=0.063 ms
  6.  
    ^C
  7.  
    --- 172.17.201.8 ping statistics ---
  8.  
    2 packets transmitted, 2 received, 0% packet loss, time 999ms
  9.  
    rtt min/avg/max/mdev = 0.063/0.068/0.074/0.009 ms
  10.  
    [root@34f07196ec86 html]#

  结论: 网络没有问题

验证:

2

 查看php-fpm容器IP

  1.  
    root@hecs-136019 ~]# docker inspect nginx-php-fpm | grep IPAddress
  2.  
    "SecondaryIPAddresses": null,
  3.  
    "IPAddress": "",
  4.  
    "IPAddress": "172.17.201.8",

查看nginx容器IP

  1.  
    [root@hecs-136019 ~]# docker inspect 34f07196ec86 | grep IPAddress
  2.  
    "SecondaryIPAddresses": null,
  3.  
    "IPAddress": "",
  4.  
    "IPAddress": "172.17.201.6",
  5.  
    [root@hecs-136019 ~]#
容器 IP
nginx 172.17.201.6
php-fpm 172.17.201.8

查看nginx配置, 开启php模块, 把fastcgi_pass 地址更改为php容器的地址  IP:端口

  1.  
     
  2.  
    #user nobody;
  3.  
    worker_processes 1;
  4.  
     
  5.  
    error_log /var/log/nginx/error.log; #开启错误日志,便于查询
  6.  
    #error_log logs/error.log notice;
  7.  
    #error_log logs/error.log info;
  8.  
     
  9.  
    #pid logs/nginx.pid;
  10.  
     
  11.  
     
  12.  
    events {
  13.  
    worker_connections 1024;
  14.  
    }
  15.  
     
  16.  
     
  17.  
    http {
  18.  
    include mime.types;
  19.  
    default_type application/octet-stream;
  20.  
     
  21.  
    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  22.  
    # '$status $body_bytes_sent "$http_referer" '
  23.  
    # '"$http_user_agent" "$http_x_forwarded_for"';
  24.  
     
  25.  
    #access_log logs/access.log main;
  26.  
     
  27.  
    sendfile on;
  28.  
    #tcp_nopush on;
  29.  
     
  30.  
    #keepalive_timeout 0;
  31.  
    keepalive_timeout 65;
  32.  
     
  33.  
    #gzip on;
  34.  
     
  35.  
    server {
  36.  
    listen 80;
  37.  
    server_name localhost;
  38.  
     
  39.  
    #charset koi8-r;
  40.  
    #access_log logs/host.access.log main;
  41.  
     
  42.  
    location / {
  43.  
    root html;
  44.  
    index index.html index.htm index.php;
  45.  
    }
  46.  
     
  47.  
    #error_page 404 /404.html;
  48.  
     
  49.  
    # redirect server error pages to the static page /50x.html
  50.  
    #
  51.  
    error_page 500 502 503 504 /50x.html;
  52.  
    location = /50x.html {
  53.  
    root html;
  54.  
    }
  55.  
     
  56.  
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  57.  
    #
  58.  
    #location ~ \.php$ {
  59.  
    # proxy_pass http://127.0.0.1;
  60.  
    #}
  61.  
     
  62.  
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  63.  
    #
  64.  
    location ~ \.php$ {
  65.  
    root /usr/local/nginx/html; # php容器的path
  66.  
    fastcgi_pass 172.17.201.8:9000; #注意,此处是php-fpm容器的IP
  67.  
    fastcgi_index index.php;
  68.  
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  69.  
    include fastcgi_params;
  70.  
    }
  71.  
    ......
学新通

更改php-fpm配置

  1.  
    [www]
  2.  
     
  3.  
    ; The address on which to accept FastCGI requests.
  4.  
    ; Valid syntaxes are:
  5.  
    ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
  6.  
    ; a specific port;
  7.  
    ; 'port' - to listen on a TCP socket to all addresses on a
  8.  
    ; specific port;
  9.  
    ; '/path/to/unix/socket' - to listen on a unix socket.
  10.  
    ; Note: This value is mandatory.
  11.  
    listen = 0.0.0.0:9000 # 监听所有服务器的9000端口(默认是本机127.0.0.1:9000)
  12.  
     
  13.  
    ; Set listen(2) backlog. A value of '-1' means unlimited.
  14.  
    ; Default Value: -1
  15.  
    ;listen.backlog = -1
  16.  
     
  17.  
    ; List of ipv4 addresses of FastCGI clients which are allowed to connect.
  18.  
    ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
  19.  
    ; PHP FCGI (5.2.2 ). Makes sense only with a tcp listening socket. Each address
  20.  
    ; must be separated by a comma. If this value is left blank, connections will be
  21.  
    ; accepted from any ip address.
  22.  
    ; Default Value: any
  23.  
    listen.allowed_clients = 172.17.201.6 #更改为nginx容器的IP(默认是本机127.0.0.1)
  24.  
     
  25.  
    ; Set permissions for unix socket, if one is used. In Linux, read/write
  26.  
    ; permissions must be set in order to allow connections from a web server. Many
  27.  
    ; BSD-derived systems allow connections regardless of permissions.
  28.  
    ; Default Values: user and group are set as the running user
  29.  
    ; mode is set to 0666
  30.  
    ;listen.owner = nobody
  31.  
    ;listen.group = nobody
  32.  
    ;listen.mode = 0666
  33.  
     
  34.  
    ; Unix user/group of processes
  35.  
    ; Note: The user is mandatory. If the group is not set, the default user's group
  36.  
    ; will be used.
  37.  
    ; RPM: apache Choosed to be able to access some dir as httpd
  38.  
    user = apache
  39.  
    ; RPM: Keep a group allowed to write in log dir.
  40.  
    group = apache
学新通

重启服务

  1.  
    [root@hecs-136019 ~]# docker restart nginx-php-fpm
  2.  
    nginx-php-fpm
  3.  
    [root@hecs-136019 ~]# docker restart 34f07196ec86
  4.  
    34f07196ec86

验证

  1.  
    [root@hecs-136019 ~]# curl http://121.37.166.201:8081/info.php
  2.  
    <pre>
  3.  
    Array
  4.  
    (
  5.  
    [REMOTE_ADDR] => 121.37.166.201
  6.  
    [REQUEST_METHOD] => GET
  7.  
    [HTTP_USER_AGENT] => curl/7.29.0
  8.  
    [REQUEST_URI] => /info.php
  9.  
    )
  10.  
    php_host: a2fe7cae0c3c
  11.  
    1229

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfegch
系列文章
更多 icon
同类精品
更多 icon
继续加载