這是我的docker-compose.yml:
version: '3'
services:
nginx:
image: nginx:alpine
restart: always
hostname: nginx
container_name: nginx
ports:
- 8088:80
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
- ./index.php:/usr/share/nginx/html/index.php
php7:
hostname: php7
container_name: php7
image: php:7.4-fpm-alpine
volumes:
- ./index.php:/usr/share/nginx/html/index.php
php8:
image: php:8.1-fpm-alpine
hostname: php8
container_name: php8
volumes:
- ./index.php:/usr/share/nginx/html/index.php
這是default.conf:
upstream php {
random two;
server php7:9000;
server php8:9000;
}
server {
listen 80;
server_name localhost;
access_log /dev/stderr main;
error_log /dev/stderr;
location / {
root /usr/share/nginx/html;
index index.php info.php index.html index.htm;
}
#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;
}
location ~ \.php$ {
fastcgi_pass php;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
# location ~ \.php(/|$) {
# fastcgi_hide_header Content-Type;
# try_files $uri $uri/ /index.php?$query_string;
# fastcgi_pass php7:9000;
# fastcgi_split_path_info ^(. \.php)(/.*)$;
# include fastcgi_params;
# fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
# fastcgi_param DOCUMENT_ROOT $realpath_root;
# }
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
}
但是當我輸入時http://IP:8088/index.php,我在網路瀏覽器中收到 404 錯誤,但是沒有登錄 nginx 來解決更多問題。
我想在 nginx 中測驗一個簡單的負載均衡器,這樣當我輸入 時http://IP:8088/index.php,一次看到 php8.1,另一次看到 7.4。
首先,我這樣做對嗎?
第二,我怎么可能擁有它?
uj5u.com熱心網友回復:
原因是您需要告訴塊php-fpm中的某些引數location ~ \.php$ {}才能找到index.php檔案。對我有用的正確 nginx conf 檔案:
upstream php {
random two;
server php7:9000;
server php8:9000;
}
server {
listen 80;
server_name localhost;
access_log /dev/stderr main;
error_log /dev/stderr;
root /usr/share/nginx/html;
index index.php info.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
#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;
}
location ~ \.php$ {
fastcgi_pass php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
請注意,您需要將root指令移到location /塊外,以便$document_root正確設定。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/507689.html
上一篇:從靜態檔案夾加載檔案時遇到問題
