docker中hyperf项目配置虚拟域名

在学习hyperf框架时遇到一些问题,这里是直接用了docker环境

下载镜像运行容器

shell 复制代码
docker run --name hyperf -v /data/project:/data/project -p 9501:9501 -itd -w /data/project --privileged -u root --entrypoint /bin/sh 镜像ID

配置docker-compose.yml

yml 复制代码
version: "3.9"
services:
  hyper83:
    image: hyperf/hyperf:8.3-alpine-v3.19-swoole-slim
    container_name: hyper83
    working_dir: /data/project
    privileged: true
    user: root
    entrypoint: /bin/sh
    ports:
      - "9501:9501"
    networks:
      - website
    volumes:
      - /data/project:/data/project
      - /usr/bin/git:/var/run/git
      - /usr/bin/git:/usr/bin/git
    restart: always
    tty: true
    stdin_open: true

  nginx12:
    image: nginx:1.23.1-alpine
    container_name: nginx12
    ports:
      - "80:80"
    networks:
      - website
    volumes:
      - /data/project:/usr/share/nginx/html
      - /data/project:/etc/nginx/conf.d
    restart: always

networks:
  website:
    driver: bridge

容器运行后进入容器后composer创建项目

运行项目

shell 复制代码
cd hyperf-skeleton
php bin/hyperf.php start

配置nginx反向代理

# 至少需要一个 Hyperf 节点,多个配置多行
upstream hyperf {
    # Hyperf HTTP Server 的 IP 及 端口
    server 172.23.0.1:9501; # 改成docker中的网桥ip
}

server {
    # 监听端口
    listen 80;
    # 绑定的域名, 改成自己的域名
    server_name hyperf.xyz;

    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;
    }
}

重启nginx,修改hosts文件

关键点在于127.0.0.1指向的是docker容器的ip

docker inspect nginx

查看docker网关是172.23.0.1,所以在upstream hyperf中配置172.23.0.1

我前面因为nginx.conf中配置的是127.0.0.1报错502

相关推荐
破-风1 小时前
linux的用法
linux·运维·服务器
大耳朵土土垚5 小时前
【Linux】日志设计模式与实现
linux·运维·设计模式
学问小小谢5 小时前
第26节课:内容安全策略(CSP)—构建安全网页的防御盾
运维·服务器·前端·网络·学习·安全
yaoxin5211236 小时前
第十二章 I 开头的术语
运维·服务器
ProgramHan6 小时前
1992-2025年中国计算机发展状况:服务器、电脑端与移动端的演进
运维·服务器·电脑
马立杰9 小时前
H3CNE-33-BGP
运维·网络·h3cne
Linux运维老纪9 小时前
DNS缓存详解(DNS Cache Detailed Explanation)
计算机网络·缓存·云原生·容器·kubernetes·云计算·运维开发
云空10 小时前
《DeepSeek 网页/API 性能异常(DeepSeek Web/API Degraded Performance):网络安全日志》
运维·人工智能·web安全·网络安全·开源·网络攻击模型·安全威胁分析
没有名字的小羊11 小时前
Cyber Security 101-Build Your Cyber Security Career-Security Principles(安全原则)
运维·网络·安全
杨浦老苏11 小时前
开源音乐管理软件Melody
docker·群晖·多媒体