問題描述
搭建好docker nginx php后,瀏覽器訪問時,始終顯示404或403問題,卡了半天,因此記錄下解決方法和程序,希望能幫到讀者少踩坑,截圖如下:

曲折的排查
排查程序真的很曲折,剛開始以為是docker容器沒配置好,檔案映射沒映射好,各種折騰沒行,容器刪了又裝,裝了又刪,docker日志看了,也看不出問題,各種百度,方法各異都沒有解決問題,
問題原因
折騰到最后,目光轉移到 nginx.conf 上來了,果不其然就是,NGINX 的配置沒有配置好,
我這里主要是這三點導致的:
- index.php 沒有配置
- fastcgi_pass 沒有用容器名
- 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:
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/291994.html
標籤:其他
