使用docker从零开始搭建私人代码仓库之nginx搭建

docker搭建nginx教程

通过《使用docker从零开始搭建私人代码仓库之MySQL搭建》和《使用docker从零开始搭建私人代码仓库之gogs搭建》的搭建其实已经可以搭建成功了代码仓库的了,但是为了访问方便,我们有时候需要绑定域名,那么我们可以通过nginx进行转发。

添加nginx容器

打开上一个教程中的docker-compose.yml文件,填入如下内容:

  gogs_nginx:
    build:
      context: nginx
    tty: true
    depends_on:
      - gogs
    restart: always
    networks: 
      frontend:
    ports:
      - 80:80
    volumes:
      - ./nginx/conf:/etc/nginx/conf.d
      - ${DATA_DIR}/nginx/conf:/var/log/nginx

最终docker-compose.yml文件内容如下:

version: "3"

networks:
  frontend:

services:
  mysql:
    image: mysql:${MYSQL_VERSION}
    networks: 
      frontend:
    tty: true
    restart: always
    ports:
      - 3306:3306
    volumes:
      - ${DATA_DIR}/mysql/:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
  gogs:
    image: gogs/gogs
    depends_on:
      - mysql
    tty: true
    networks: 
      frontend:
    restart: always
    volumes:
      - ${DATA_DIR}/gogs:/data
  gogs_nginx:
    build:
      context: nginx
    tty: true
    depends_on:
      - gogs
    restart: always
    networks: 
      frontend:
    ports:
      - 80:80
    volumes:
      - ./nginx_conf:/etc/nginx/conf.d
      - ${DATA_DIR}/nginx/conf:/var/log/nginx

在项目根目录创建nginx_conf目录并创建nginx的配置文件default.conf

> mkdir nginx_conf && cd nginx_conf && touch default.conf

编辑default.conf配置文件并加入如下配置:

upstream gogs {
    server gogs:3000;
}

server {
    listen       80 default_server;
    server_name  gogs.me; # 域名

    location / {
        #反向代理的地址
        proxy_pass http://gogs;  
        #设置主机头和客户端真实地址,以便服务器获取客户端真实IP
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        access_log /var/log/nginx/gogs.log main;
    }
}

该配置绑定了域名gogs.me并把请求转发到gogs容器的3000端口

启动nginx

> docker-compose up -d gogs_nginx

通过该命令会一起启动mysql和gogs。

https://raw.githubusercontent.com/sockstack/hexo_blog_img/master/%E4%BD%BF%E7%94%A8docker%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E6%9E%84%E5%BB%BA%E7%A7%81%E4%BA%BA%E4%BB%A3%E7%A0%81%E4%BB%93%E5%BA%93/%E5%90%AF%E5%8A%A8nginx.png

所有容器都启动成功了。

安装gogs

在浏览器中输入gogs.me会出现安装gogs的界面:

gogs安装界面

添加如下配置:

gogs配置1

gogs配置2

添加完成后点击立即安装,等待安装完成即可。

安装完成界面

更多精彩文章,请关注我的博客SOCKSTACK,分享我的工作经验。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章