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

docker搭建nginx教程

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

添加nginx容器

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

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文件内容以下:nginx

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.confgit

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

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

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端口sql

启动nginx

> docker-compose up -d gogs_nginx

经过该命令会一块儿启动mysql和gogs。docker

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

全部容器都启动成功了。shell

安装gogs

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

gogs安装界面

添加以下配置:服务器

gogs配置1

gogs配置2

添加完成后点击当即安装,等待安装完成便可。hexo

安装完成界面

更多精彩文章,请关注个人博客 SOCKSTACK,分享个人工做经验。
相关文章
相关标签/搜索