Docker + Nginx + PHP 访问403,404问题

Posted 狂野小青年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker + Nginx + PHP 访问403,404问题相关的知识,希望对你有一定的参考价值。

问题描述

搭建好docker nginx php后,浏览器访问时,始终显示404或403问题,卡了半天,因此记录下解决方法和过程,希望能帮到读者少踩坑,截图如下:

曲折的排查

排查过程真的很曲折,刚开始以为是docker容器没配置好,文件映射没映射好,各种折腾没行。容器删了又装,装了又删,docker日志看了,也看不出问题,各种百度,方法各异都没有解决问题。

问题原因

折腾到最后,目光转移到 nginx.conf 上来了,果不其然就是,NGINX 的配置没有配置好。

 我这里主要是这三点导致的:

  1. index.php 没有配置
  2. fastcgi_pass 没有用容器名
  3. fastcgi_param  SCRIPT_FILENAME 路径问题

解决方法

我这里php容器用的是官网的镜像,实际中可能与我这边不一样,根据实际情况调整,这里只是提供一个思路。

 贴上我修改后的配置文件:

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    root /usr/share/nginx/html;

    location / {
        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   /usr/share/nginx/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$ {
       fastcgi_pass   php:9000;
       fastcgi_index  index.php;
       #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
       #fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param  SCRIPT_FILENAME /www/$fastcgi_script_name;
       fastcgi_param  SCRIPT_NAME $fastcgi_script_name;
       fastcgi_param  PATH_INFO $fastcgi_path_info;
       fastcgi_param  CI_ENV "local_dev";
       include        fastcgi_params;
       fastcgi_split_path_info ^(.+\\.php)(.*)$;
       # 隐藏index.php需开启
       fastcgi_param  PATH_TRANSLATED     $document_root$fastcgi_path_info;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\\.ht {
    #    deny  all;
    #}
}

 最后

最后贴上我的 docker-compose.yaml 文件,希望能帮助到你。

version: '2'

services:

  nginx:
    container_name: nginx
    depends_on:
      - php
    links:
      - php:9000
    image: nginx
    ports:
      - "8083:80"
    privileged: true
    volumes:
      - "/Users/wanzhou/project/HealthExam_Guide/wwwroot:/usr/share/nginx/html"
      - "./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf"
    #      - ~/Projects/sh-valley/docker-conf/lnmp/nginx/logs:/var/log/nginx
    networks:
      - lnmp-network
    restart: always

  php:
    container_name: php
    image: php:5.6-fpm-alpine3.8
    privileged: true
    volumes:
      - /Users/wanzhou/project/HealthExam_Guide/wwwroot:/www
    networks:
      - lnmp-network
    restart: always

#  php-fpm:
#    container_name: php-fpm
#    image: php:7.0.7-fpm-alpine
#    privileged: true
#    ports:
#      - "9005:9000"
#    volumes:
#      - "/Users/wanzhou/project/HealthExam_Standard/:/usr/local/nginx/html"
#    restart: always
#
#  tengine:
#    container_name: tengine
#    depends_on:
#      - php-fpm
#    links:
#      - php-fpm:9000
#    image: chasontang/tengine:2.1.2
#    privileged: true
#    volumes:
##      - "./nginx.vh.default.conf:/etc/nginx/conf.d/default.conf"
##      - "./nginx.vh.default.conf:/usr/local/nginx/conf.d/default.conf"
##      - "./nginx.vh.default.conf:/etc/nginx/conf.d/localhost.conf"
##      - "./nginx.vh.default.conf:/usr/local/nginx/conf.d/localhost.conf"
##      - "./nginx.vh.default.conf:/usr/local/nginx/config/conf.d/default.conf"
##      - "./nginx.vh.default.conf:/usr/local/nginx/sbin/conf.d/default.conf"
#      - "/Users/wanzhou/project/HealthExam_Standard/:/usr/local/nginx/html"
#    ports:
#      - "8082:80"
#    restart: always

  redis:
    container_name: redis
    image: redis

  mysql:
    restart: always
    image: mysql:5.7.16
    container_name: mysql
#    volumes:
#      - ./mydir:/mydir
#      - ./datadir:/var/lib/mysql
#      - ./conf/my.cnf:/etc/my.cnf
#      #      数据库还原目录 可将需要还原的sql文件放在这里
#      - /docker/mysql/source:/docker-entrypoint-initdb.d
    environment:
      - "MYSQL_ROOT_PASSWORD=123456"
      - "MYSQL_DATABASE=health"
      - "TZ=Asia/Shanghai"
    ports:
      - 3307:3306
    networks:
      - lnmp-network

networks:
  lnmp-network:

以上是关于Docker + Nginx + PHP 访问403,404问题的主要内容,如果未能解决你的问题,请参考以下文章

docker nginx和php,php无法连接到localhost

访问 Elastic Beanstalk/Docker Nginx/PHP-FPM 实例中的客户端 IP

docker搭建php+nginx常见问题

docker部署lnmp环境搭建php项目

docker部署lnmp环境

docker部署lnmp环境