当前位置: 首页>编程语言>正文

docker中hyperf项目配置虚拟域名

在使用hyperf框架时,直接用了docker环境进行开发

下载镜像运行容器

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

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创建项目

运行项目

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

docker中hyperf项目配置虚拟域名,在这里插入图片描述,第1张
我前面因为nginx.conf中配置的是127.0.0.1报错502


https://www.xamrdz.com/lan/5a81848972.html

相关文章: