使用docker 安装nginx及配置volume目录及负载均衡(win10)
发布日期:2021-09-23 03:31:51 浏览次数:20 分类:技术文章

本文共 5261 字,大约阅读时间需要 17 分钟。

1.第一步 先拉取镜像

docker pull nginx:latest

2. nginx 容器内部一般地址配置如下:

日志位置:/var/log/nginx/ 配置文件位置:/etc/nginx/ 项目位置:/usr/share/nginx/html

我们对应映射路径到windows10的目录地址如下:

 

3.创建配置文件:

a.在conf中添加nginx.conf 内容如下:

user  nginx;worker_processes  1;error_log  /var/log/nginx/error.log warn;pid        /var/run/nginx.pid;events {    worker_connections  1024;}http {    include       /etc/nginx/mime.types;    default_type  application/octet-stream;    log_format  main    '"$remote_addr" "$http_host" "[$time_local]" "$request" "$status" "$body_bytes_sent" '                    '"$bytes_sent" "$gzip_ratio" "$http_referer" "$http_user_agent" "$http_x_forwarded_for" '                    '"$upstream_addr" "$upstream_response_time" "$request_time" "$request_body" "$http_authorization" ';    access_log  /var/log/nginx/access.log  main;    sendfile        on;    #tcp_nopush     on;    keepalive_timeout  65;    #gzip  on;    include /etc/nginx/conf.d/*.conf;}

b. 在conf.d目录中创建文件default.conf和server9001.conf 在a步骤中的include /etc/nginx/conf.d/*.conf; 则会读取conf.d中配置的服务文件

default.conf及server9001.conf 内容配置如下:

80端口(default.conf)对应容器外部9000

server {    listen       80;    server_name  localhost;    #charset koi8-r;    access_log  /var/log/nginx/host.access.log  main;    location / {        #root   /data/nginx/html;        root   /usr/share/nginx/html;        index  index.html index.htm;		add_header 'Access-Control-Allow-Origin' '*' always;        #autoindex  on;        #try_files $uri /index/index/page.html;        #try_files $uri /index/map/page.html;    }    #error_page  404              /404.html;    # redirect server error pages to the static page /50x.html    #    error_page   500 502 503 504  /50x.html;    location = /50x.html {        root   /usr/share/nginx/html;    }    location ~ /images {        default_type                            application/json;        return 200  '{"code": "A000000", "message": "ok", "timestamp": "20180307184426", "data": {"isvip": "1", "monthProList": []}}';    }    }

9001端口(server9001.conf)对应容器外部9001

server {    listen 9001 default_server;    listen [::]:9001 default_server;	#/usr/share/nginx/html 在docker中已经映射了所以9001目录不需要再次映射    root /usr/share/nginx/html/server9001;    location / {        try_files $uri $uri/ /index.html;        add_header 'Access-Control-Allow-Origin' '*' always;    }}

4.在静态文件内容页上目录如下:

外部index.html为9000端口配置文件,server9001为9001端口配置页面

 

5. cmd运行容器配置volume: 

docker run -it -d  --name nginx-linux -p 9000:80 -p 9001:9001   -v D:/dockerdata/volume/nginx-vol/conf/nginx.conf:/etc/nginx/nginx.conf -v D:/dockerdata/volume/nginx-vol/conf.d:/etc/nginx/conf.d -v D:/dockerdata/volume/nginx-vol/wwwroot:/usr/share/nginx/html -v D:/dockerdata/volume/nginx-vol/log:/var/log/nginx nginx

6.查看运行结果:

 

Nginx负载均衡配置:

1、实现访问宿主机8000 -->代理到9000,9001、

单独再配置一个nginx 映射目录为:

 

配置内容:

user  nginx;worker_processes  1;error_log  /var/log/nginx/error.log warn;pid        /var/run/nginx.pid;events {    worker_connections  1024;}http {    include       /etc/nginx/mime.types;    default_type  application/octet-stream;    log_format  main    '"$remote_addr" "$http_host" "[$time_local]" "$request" "$status" "$body_bytes_sent" '                    '"$bytes_sent" "$gzip_ratio" "$http_referer" "$http_user_agent" "$http_x_forwarded_for" '                    '"$upstream_addr" "$upstream_response_time" "$request_time" "$request_body" "$http_authorization" ';    access_log  /var/log/nginx/access.log  main;    sendfile        on;    #tcp_nopush     on;    keepalive_timeout  65;    #gzip  on;    # include /etc/nginx/conf.d/*.conf;	 # 注意,这里的server名字即不能带下划线,     upstream localhost {        server 192.168.137.172:9000 weight=1; #ip地址为宿主机ip        server 192.168.137.172:9001 weight=2;    }	server {        #注意,这里保留80,因为前面的docker运行命令,会映射80端口        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        # 不带数据的请求        location / {            root   html;            index  index.html index.htm;            #访问映射            proxy_pass   http://localhost;         }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}

 

2、运行启动容器:

docker run -it -d  --name nginx-linux -p 8000:80 -v D:/dockerdata/volume/nginx/conf/nginx.conf:/etc/nginx/nginx.conf

 

3.测试并访问:localhost:8000

总结:

1.Docker的数据卷及挂载数据到容器的两种主要方式Volumes和Bind Mounts

```

# docker volume create edc-nginx-vol // 创建一个自定义容器卷
# docker volume ls // 查看所有容器卷
# docker volume inspect edc-nginx-vol // 查看指定容器卷详情信息
```

```

 docker run -d -it --name=edc-nginx -p 8800:80 -v edc-nginx-vol:/usr/share/nginx/html nginx
```

其中,-v代表挂载数据卷,这里使用自定数据卷edc-nginx-vol,并且将数据卷挂载到 /usr/share/nginx/html (这个目录是yum安装nginx的默认网页目录)。

清理卷:

```

# docker stop edc-nginx // 暂停容器实例
# docker rm edc-nginx // 移除容器实例
# docker volume rm edc-nginx-vol // 删除自定义数据卷
```

2、Bind Mounts 使用:

```

docker run -d -it --name=edc-nginx -v /app/wwwroot:/usr/share/nginx/html nginx
```

这里指定了将宿主机上的 /app/wwwroot 目录(如果没有会自动创建)挂载到 /usr/share/nginx/html (这个目录是yum安装nginx的默认网页目录)。

bind mounts的方式会隐藏掉被挂载目录里面的内容(如果非空的话),这里是/usr/share/nginx/html 目录下的内容被隐藏掉了,因此我们看不到。

3.

验证绑定:

```

docker inspect edc-nginx
```
 

转载地址:https://blog.csdn.net/andy5520/article/details/112574861 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:vs MSBuild构建发布dotnet项目指令及参数
下一篇:linux 发布部署asp.net core 独立部署包并添加守护进程服务

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年03月31日 01时41分45秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Leetcode 662. 二叉树最大宽度(DAY 20) 2019-04-27
Leetcode 面试题 04.05. 合法二叉搜索树(DAY 20)(迭代中序遍历) 2019-04-27
Leetcode 1028. 从先序遍历还原二叉树(DAY 21)(划开新时代 Hard第一题 Hard含题解) 2019-04-27
Leetcode 99. 恢复二叉搜索树(DAY 22 Hard含题解) 2019-04-27
Leetcode 297. 二叉树的序列化与反序列化(DAY 23)(Hard细节挺多的 需要调试一会 含题解) 2019-04-27
Leetcode 1641. 统计字典序元音字符串的数目(DAY 24 动态规划开始 ---鸽子一星期准备考试)----动态规划学习期 2019-04-27
Leetcode 338. 比特位计数(DAY 25高数线代终于考完)----动态规划学习期 2019-04-27
Leetcode 1402. 做菜顺序(DAY 25 Hard 含题解)----动态规划学习期 2019-04-27
Leetcode 1025. 除数博弈(DAY 25)---- 动态规划学习期 2019-04-27
Leetcode 303. 区域和检索 - 数组不可变(DAY 25) ---- 动态规划学习期 2019-04-27
Leetcode 剑指 Offer 42. 连续子数组的最大和(DAY 25)---- 动态规划学习期 2019-04-27
Leetcode 121. 买卖股票的最佳时机(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 746. 使用最小花费爬楼梯(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 面试题 17.16. 按摩师(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 70. 爬楼梯(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 392. 判断子序列(DAY 26)---- 动态规划学习期 双百解法 2019-04-27
Leetcode 面试题 08.01. 三步问题(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 877. 石子游戏(DAY 27) ---- 动态规划学习期 2019-04-27
Leetcode 714. 买卖股票的最佳时机含手续费(DAY 27) ---- 动态规划学习期 2019-04-27
Leetcode 96. 不同的二叉搜索树(DAY 28) ---- 动态规划学习期 (含题解) 2019-04-27