首先用docker起两个容器这边容器名称分别是hyperf、hyperf2
映射到主机的端口分别是9551和9552
nginx配置:
# 至少需要一个 Hyperf 节点,多个配置多行
upstream hyperf_servers {
# f容器名:端口 权重
server hyperf:9501 weight=50;
server hyperf2:9502 weight=50;
server hyperf3:9505 backup;
}
server {
# 监听端口
listen 80;
# 绑定的域名,填写您的域名
server_name test.hyperf.com;
location / {
# 将客户端的 Host 和 IP 信息一并转发到对应节点
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 转发Cookie,设置 SameSite
proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
# 执行代理访问真实服务器
proxy_pass http://hyperf_servers;
}
}
#通过其他域名代理跳转
server {
listen 80;
server_name ultron.mijian360.host;
root /www/ultron/public;
index index.php index.html index.htm;
#charset koi8-r;
access_log /dev/null;
#access_log /var/log/nginx/nginx.ultron.access.log main;
error_log /var/log/nginx/nginx.ultron.error.log warn;
#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 / {
try_files $uri $uri/ /index.php?$query_string;
}
location /hyperf/api {
# 执行代理访问真实服务器
proxy_pass http://test.hyperf.com/;
# proxy_pass http://test.hyperf.com/index;
}
# 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;
include fastcgi-php.conf;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
通过命令访问 curl http://test.hyperf.com/ 或者浏览器访问http://test.hyperf.com/ 结果如下:
当修改代码的时候可以执行以下shell,达到平滑启动的效果
#!/bin/bash
set -e
#重启hyperf容器
docker exec -it hyperf /bin/sh -c "composer dump-autoload -o && php bin/hyperf.php && exit"
docker restart hyperf
# 检测端口是否可访问
check_port() {
nc -zv 127.0.0.1 9551 >/dev/null 2>&1
}
# 执行命令,重启hyperf2容器
execute_command() {
docker exec -it hyperf2 /bin/sh -c "composer dump-autoload -o && php bin/hyperf.php && exit"
docker restart hyperf2
}
# 循环最多一分钟检测端口是否可访问
start_time=$(date +%s)
while ! check_port; do
sleep 1
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [ $elapsed_time -gt 60 ]; then
echo "Port 9551 is not accessible after 60 seconds, exiting..."
exit 1
fi
done
# 当端口可访问时执行命令
execute_command
最新评论