This is my 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
This is 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;
#}
}
But when I type http://IP:8088/index.php
, I get 404 error in web browser, but there's no log in nginx to troubleshoot more.
I want to test a simple load balancer in nginx, so that when I type http://IP:8088/index.php
, one time I see php8.1 and another time I see 7.4.
First of all, is that right what I'm doing?
Second, how may I have it?
CodePudding user response:
The reason is you need to tell php-fpm
certain params in the location ~ \.php$ {}
block to be able to find the index.php
file. The correct nginx conf file that worked for me:
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;
}
}
Please note that you need to move the root
directive outside the location /
block so that $document_root
is properly set.