docker安装nginx

一、安装nginx

1、nginx 目录位置

  • 配置文件:/etc/nginx/nginx.conf
  • 默认配置文件:/etc/nginx/conf.d/default.conf
  • 日志:/var/log/nginx
  • 网页目录:/usr/share/nginx/html
  • 配置文件夹/etc/nginx/conf.d

2、拉取镜像

docker pull nginx:1.21

3、创建目录

mkdir -p /home/apps/nginx/{conf,html,logs,ssl}

4、复制配置文件(先简单创建一个容器,从中复制配置文件到本地)

# 创建容器并运行
docker run -d --name nginx -p 80:80 nginx:1.21

cd /home/apps/nginx

# 从运行的docker中复制nginx配置文件到本地
docker cp nginx:/etc/nginx/nginx.conf ./
docker cp nginx:/etc/nginx/conf.d/default.conf ./conf/

# 移除docker
docker stop nginx && docker rm nginx

5、创建容器并运行

docker run -d \
--name nginx \
-p 80:80 \
-p 443:443 \
-v /home/apps/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /home/apps/nginx/logs:/var/log/nginx \
-v /home/apps/nginx/html:/usr/share/nginx/html \
-v /home/apps/nginx/conf:/etc/nginx/conf.d \
-v /home/apps/nginx/ssl:/etc/nginx/ssl \
--restart always \
--privileged=true \
nginx:1.21

6、测试页面

  • 前端页面需要放到目录:/home/apps/nginx/html/
  • 新建index.html页面
cd /home/apps/nginx/html/

# 输出hello word
vim index.html

# 输入文字
hello word

# 保存并退出
wq
  • 访问页面:ip地址:80

二、域名配置

1、新建一个zentao.abc.com的二级域名访问

vim /home/apps/nginx/conf/zentao-nginx.conf

# 内容如下
server{
    listen 80;
    server_name zentao.abc.com;

    location / {
        client_max_body_size 100m;
        proxy_pass http://192.168.1.6:6003;
        index index.html index.htm;
    }
}

2、https域名ssl配置

server{
    listen 80;
    server_name gitlab.abc.com;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    # 后端接口配置
    location ~* ^/(code|api) {
      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;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_pass http://192.168.1.6:6003;
    }
}

server {
    listen       443 ssl;
    server_name  gitlab.abc.com;
    ssl_certificate      /etc/nginx/ssl/gitlab.abc.com.pem;
    ssl_certificate_key  /etc/nginx/ssl/gitlab.abc.com.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers  ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers  on;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    # 后端接口配置
    location ~* ^/(code|api) {
      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;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_pass http://192.168.1.6:6003;
    }	
}

3、Nginx重新加载配置文件

# 进入nginx容器
docker exec -it nginx /bin/bash

# 刷新配置
nginx -s reload

三、参考

https://www.cnblogs.com/qiqiloved/p/13470064.html

posted @ 2021-08-27 22:06  吕林光  阅读(448)  评论(0编辑  收藏  举报