在.net core中使用nginx做负载均衡
off999 2025-01-04 22:24 17 浏览 0 评论
前言:
本文主要内容是docker部署netcore应用以及docker运行nginx实现负载均衡。到目前为止感觉微软在跨平台的方面虽然有较大的进步,但是和linux比还是有一定的差距,在学习docker,nginx以及Netcore 过程中网上能查找参考的资料还是比较有限的,所以在此记录下遇到的问题以及踩到的各种坑,希望避免再次走弯路。
一.新建NET Core应用程序;
1.添加Docker支持,由于到目前为止,nginx还不支持windows容器,为了便于本机测试所以选用Linux容器,如下图:
2.输出请求处理,显示当前请求IP以及端口号:
3.编辑Dockerfile文件
dockerfile文件指令说明:
FROM -指定基础镜像(FROM是必备的指令,并且必须为第一条指令)
WORKDIR-配置工作目录
EXPOSE-声明镜像内服务监听的端口
COPY-复制内容到镜像
ENTRYPOINT-启动镜像的默认人口命令
ENV -设置环境变量
编辑后的dockerfile文件如下:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 8005
EXPOSE 443
ENV ASPNETCORE_URLS http://+:8005
COPY . .
ENTRYPOINT ["dotnet", "DockerDemo.dll"]
右键属性,选择“如果较新则复制”
4.Docker发布,
Release生成解决方案,定位到reliease文件夹,执行"docker build”指令,如下图:
在执行过程中,有可能会碰到“no such host”问题,重试几次或者设置镜像代理即可
ps: 镜像版本后面有个空格+“ .”需留意 注意最后有个点,代表使用当前路径的 Dockerfile 进行构建
运行 docker images查看当前镜像:
可以看到已经成功构建。
5.构建dockerdemo容器:
执行"docker run“指令,构建该镜像的实例docker run -d -t -p 8006:8005 --name dockerdemo8006 dockerdemo:1.0,可以看到,容器已经构建成功:
浏览器输入即可看到下列结果
同理构建该镜像的其他容器,分别为8007,8008;至此docker构建netcore镜像已经完成,我们可以在Kitematic查看已经构建的容器;
二.Docker构建nginx容器实现负载均衡
1.执行“docker pull nginx“指令获取镜像,docker images 查看当前镜像;
2.构建nginx镜像容器
执行docker run -d -p 8003:80 --name dockernginxdemo nginx,其中-d表示后台运行,-p表示开放的端口,将80端口代理到宿主8003端口,这样访问ip:8003就可以访问nginx 80端口,--name表示容器名字,最后为镜像名:标签。
docker ps查看当前容器:
可以看出已经实例化镜像文件。在浏览器中输入localhost:8003出现如下界面则表示成功:
3.修改nginx配置文件:
该页面是镜像文件中自带的一个html文件,我们需要将我们自己nginx的配置文件复制替换容器自带的配置文件,进入容器中
docker exec -it dockernginxdemo/bin/bash
cat /etc/nginx/nginx.conf
可以看到Nginx镜像自带的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 - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
可以看到这和nginx教程中的配置文件有一定的区别的,我自己在这儿踩了几个坑,nginx中的配置文件是作为一个整体的,而镜像中是默认包括两个文件:default.conf和nginx.conf文件,下图是nginx教程中的配置文件
#定义Nginx运行的用户和用户组
#user nobody;
#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 1;
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程文件
#pid logs/nginx.pid;
#工作模式与连接数上限
events {
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 1024;
}
#设定http服务器
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改 成off。
sendfile on;
#防止网络阻塞
#tcp_nopush on;
#长连接超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩输出
#gzip on;
#虚拟主机的配置
server {
#监听端口
listen 80;
#域名可以有多个,用空格隔开
server_name localhost;
#默认编码
#charset utf-8;
#定义本虚拟主机的访问日志
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
如果直接将conf文件替换镜像中的nginx文件的话,会出现“directive is not allowed here in /etc/nginx/conf.d/nginx.conf”这样的问题,原因是在default.conf配置文件中,已经存在"server”节点,我们可以执行如下指令
docker cp f3d9ee01637a:/etc/nginx/conf.d/default.conf E:/ngingconf/default.conf
将conf.d/default.conf文件复制到 E:/ngingconf/default.conf文件中,default.conf文件如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
可以看到,在default文件中已经存在server节点,所以不能直接复制自定义的nginx.conf文件到容器的nginx.conf文件中。有两种解决方案,第一分别修改nginx.conf和default.conf文件;第二,将容器中的default.conf文件内容删除,然后再复制自定义的conf文件覆盖掉镜像中nginx.conf的配置文件。这里,我们采用第一种方法,修改镜像中Nginx.conf以及default.conf的局部节点;
首先,执行
docker cp f3d9ee01637a:/etc/nginx/nginx.conf E:/ngingconf/nginx.conf
指令,将镜像文件中的nginx.conf文件复制到本地,打开E:/ngingconf/nginx.conf,编辑配置文件如下:
1 user nginx;
2 worker_processes 1;
3
4 error_log /var/log/nginx/error.log warn;
5 pid /var/run/nginx.pid;
6
7
8 events {
9 worker_connections 1024;
10 }
11
12
13 http {
14
15 #集群站点配置
16 upstream nginxtest.com{
17 #server 127.0.0.1:8887 weight=1;#服务器配置 WEIGHT是权重的意思,权重越大,分配的概率越大。
18 server 127.0.0.1:8006 weight=1;
19 server 127.0.0.1:8007 weight=1;
20 server 127.0.0.1:8008 weight=1;
21 }
22
23
24
25 include /etc/nginx/mime.types;
26 default_type application/octet-stream;
27
28 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
29 '$status $body_bytes_sent "$http_referer" '
30 '"$http_user_agent" "$http_x_forwarded_for"';
31
32 access_log /var/log/nginx/access.log main;
33
34 sendfile on;
35 #tcp_nopush on;
36
37 keepalive_timeout 65;
38
39 #gzip on;
40
41 include /etc/nginx/conf.d/*.conf;
42 }
nginx.conf文件中,我们添加了
节点指令,配置服务集群,对应之前的三个netcore应用的端口8006,8007,8008。然后执行
docker cp E:/ngingconf/nginx.conf f3d9ee01637a:/etc/nginx/nginx.conf
执行,将本地修改后的nginx.conf文件拷贝到镜像文件中;同理,执行
docker cp f3d9ee01637a:/etc/nginx/conf.d/default.conf E:/ngingconf/default.conf
将镜像文件中的default.conf文件复制到本地,打开E:/ngingconf/default.conf,编辑配置文件如下:
1 server {
2 listen 80;
3 server_name localhost;
4
5 #charset koi8-r;
6 #access_log /var/log/nginx/host.access.log main;
7
8 #缓存文件路由
9 location ~ .*(\.(js|css|jpg|svg)).* {
10
11 proxy_pass http://nginxtest.com;
12 proxy_cache_valid 200;
13 expires 3d;
14 }
15 #集群站点路由
16 location / {
17
18 proxy_pass http://nginxtest.com; #http://nginxtest.com对应upstream后面的名称加上http
19 proxy_http_version 1.1;
20 proxy_redirect default;
21
22 proxy_set_header Upgrade $http_upgrade;
23 proxy_set_header Connection keep-alive;
24 proxy_set_header Host $host;
25 proxy_cache_bypass $http_upgrade;
26
27 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
28 }
29 }
注意 集群站点中“proxy_pass”后面参数名称需要和nginf.conf文件中集群名称一致。随后执行cp指令覆盖镜像文件即可;
4.测试
至此docker 部署nginx反向代理已经完成,预期效果是在网站中输入localhost:8003就会访问不同的端口号实现负载均衡实际效果运行却是。。。
不甘心,有试了几遍,还是这个结果 后来查看了下Kitematic,日志果然有其他问题:
大概就是无法连接到集群里的服务器,推测了下应该是容器间的通信问题,于是验证了下,用IIS而不是容器发布了应用然后用docker部署nginx负载均衡,其他步骤一样,果然可以直接访问而不会出现该问题,于是开始着手解决这个问题
相关推荐
- python列表(List)必会的13个核心技巧(附实用方法)
-
列表(List)是Python入门的关键步骤,因为它是编程中最常用的数据结构之一。以下是高效掌握列表的核心技巧和实用方法:一、理解列表的本质可变有序集合:可随时修改内容,保持元素顺序混合类型:一个列表...
- Python列表(List)一文全掌握:核心知识点+20实战练习题
-
Python列表(List)知识点教程一、列表的定义与特性定义:列表是可变的有序集合,用方括号[]定义,元素用逗号分隔。list1=[1,"apple",3.14]lis...
- python编程中列表常见的9大问题,你知道吗?
-
Python列表常见错误及解决方案列表(list)是Python中最常用的数据结构之一,但在使用过程中经常会遇到各种问题。以下是Python列表使用中的常见错误及其解决方法:一、索引越界错误1.访问...
- python之列表操作(python列表操作函数大全)
-
常用函数函数名功能说明append将一个元素添加到列表中names=['tom']用法:names.append('tommy')注意事项:被添加的元素只会被添加到...
- 7 种在 Python 中反转列表的智能方法
-
1.使用reverse()方法(原地)my_list=[10,12,6,34,23]my_list.reverse()print(my_list)#output:[23,34,6,12,...
- Python教程-列表复制(python中列表copy的用法)
-
作为软件开发者,我们总是努力编写干净、简洁、高效的代码。Python列表是一种多功能的数据结构,它允许你存储一个项目的集合。在Python中,列表是可变的,这意味着你可以在创建一个列表后改变它的...
- 「Python程序设计」基本数据类型:列表(数组)
-
列表是python程序设计中的一个基本的,也是重要的数据结构。我们可以把列表数据结构,理解为其它编程语言中的数组。定义和创建列表列表中的数据元素的索引,和数组基本一致,第一个元素的索引,或者是下标为0...
- Python中获取列表最后一个元素的方法
-
技术背景在Python编程中,经常会遇到需要获取列表最后一个元素的场景。Python提供了多种方法来实现这一需求,不同的方法适用于不同的场景。实现步骤1.使用负索引-1这是最简单和最Pythoni...
- Python学不会来打我(11)列表list详解:用法、场景与类型转换
-
在Python编程中,列表(list)是最常用且功能最强大的数据结构之一。它是一个有序、可变、支持重复元素的集合,可以存储任意类型的对象,包括整数、字符串、布尔值、甚至其他列表。本文将从基础语法开始...
- 零起点Python机器学习快速入门-4-4-列表操作
-
Python列表的基本操作展开。首先,定义了两个列表zlst和vlst并将它们的内容打印出来。接着,使用切片操作从这两个列表中提取部分元素,分别得到s2、s3和s4三个新的列表,并打...
- python入门 到脱坑 基本数据类型—列表
-
以下是Python列表(List)的入门详解,包含基础操作、常用方法和实用技巧,适合初学者系统掌握:一、列表基础1.定义列表#空列表empty_list=[]#包含不同类型元素的列表...
- Python 列表(List)完全指南:数据操作的利器
-
在Python中,列表(list)是一种可变序列(mutablesequence),它允许我们存储和操作一组有序数据(ordereddata)。本教程将从基础定义(basicdefiniti...
- 如何快速掌握 Python中列表的使用
-
学习python知识,好掌握Python列表的使用。从概念上来讲,Python中的列表list是一种有序、可变的容器,可以存储任意类型的数据(包括其他列表)。以下是列表的常用的操作和知识:1....
- Python中的列表详解及示例(python中列表的用法)
-
艾瑞巴蒂干货来了,数据列表,骚话没有直接来吧列表(List)是Python中最基本、最常用的数据结构之一,它是一个有序的可变集合,可以包含任意类型的元素。列表的基本特性有序集合:元素按插入顺序存储可变...
- python数据类型之列表、字典、元组、集合及操作
-
Python数据类型进阶:列表、字典与集合在Python中,数据类型是编程的基础,熟练掌握常用数据结构是成为高级开发者的关键。上一篇文章我们学习到了Python的数据类型:字符串(string)、数...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python自定义函数 (53)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python人脸识别 (54)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)