在.net core中使用nginx做负载均衡
off999 2025-01-04 22:24 37 浏览 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负载均衡,其他步骤一样,果然可以直接访问而不会出现该问题,于是开始着手解决这个问题
相关推荐
- appstore应用商店下载(AppStore应用商店下载入口)
-
可能因为1.你的软件原来在其他国家下载的,你现在账户不支持那个软件的更新,只要更改到相应的地区就好了2.可能你网不好(?˙ー˙?)3.你的pad原来登录的账户和现在不一样,所以你现在...
- 联想售后人工客服24小时电话
-
联想服务中心朝阳区望京店距您12.2KM营业时间:周一至周日9:00-18:00疫情期间停业好评度:98%已服务人数61945联想服务中心昌平区天通苑店距您5.1KM营业时间:周一至周日9:...
- 鼠标dpi键有什么作用(鼠标的dpi键)
-
鼠标DPI(DotsPerInch)键的作用是调节鼠标的定位精度和速度。DPI是鼠标的定位精度单位,指的是鼠标在平面上每移动一英寸能准确定位的最大信息数。通常情况下,鼠标DPI越高,鼠标指针在屏幕...
- 无线网登录密码忘记了怎么办
-
1、先查看一下wifi链接的网关是多少,如果是默认的多为192.168.1.1或者192.168.0.1这个地址。2、然后打开ie浏览器,输入查看到的网关地址,比如192.168.1.1,会弹出登录对...
- 微软重装系统(微软重装系统工具)
-
win10重装跳过账号登录的方法步骤如下:1、重装系统到“登录你的Microsoft账户”这一界面后,点击下方的“创建一个新账户”。2、接着进入...
- 迷你世界激活码生成器下载(迷你世界激活码生成器app下载)
-
序列号相当于导航的身份证,表示该导航对应的版本和机型。是厂家随机编号的,说不出什么意思。在Excel中,您可以使用以下方法快速生成序列号:方法一:填充法1.在第一个目标单元格中输入起始序号(No)值...
-
- 手机怎么设置wifi网络(手机如何设置wifi网络)
-
1、在手机上打开浏览器,在浏览器中输入192.168.1.1,并点击访问。2、在跳转的界面中,输入无线网络的管理名称和管理密码,并点击下方的确认。3、之后设置无线的名称和无线密码,设置完成后,点击下方的保存。4、之后即可完成设置,路由器重启...
-
2025-12-17 04:51 off999
- win11分区教程(win11磁盘分区形式)
-
在安装win11时,可以选择自定义分区,进入磁盘管理界面,选择要安装win11的磁盘,点击新建分区,设置分区大小和分区盘符,注意不要覆盖原有数据分区。建议将系统分区和数据分区分开,以便备份和维护。安装...
- win7双系统怎么切换(双系统win7切换win10)
-
要切换到其他操作系统,必须使用另一个启动设备或安装系统。以下是一些步骤:1.获取安装媒体(光盘或USB驱动器),包含您要安装的操作系统。2.在Windows7上打开电脑,将安装媒体插入计算机。3...
- project2010激活码(project2010永久激活码)
-
首先我们在网上下载project2010,因为project2010是付费使用的,所以我们可以先使用试用版,试用版期限是30天。首先我们来创建一个项目,然后使用project2010来进行管理。我们可...
- 迈克菲无法卸载(迈克菲无法卸载怎么解决)
-
一,找到迈克菲的图标二,右键,点击更改设置——实时扫描三,点击关闭,即可。1,控制面板中-管理工具-服务-停止MCAFEEFRAMEWORKMANAGER服务2,开始中—运行—输入cmd—输入“C...
- 爱普生打印机驱动(爱普生打印机驱动怎么安装)
-
1.首先进入爱普生网站。找到驱动以及手册证书下载2.选择打印机的类型,勾选正确的打印机型号。3.根据自己的电脑操作系统选择对应的软件版本点击下载。4.下载了LQ-630KII的驱动,双击运行。5.运行...
- windows7显示不是正版怎么办
-
不必担心,是不是正版影响并不大,不会影响你正常使用电脑,无非就是心里对于正版和盗版的道德观念会有点小执念罢了,但这些都不影响系统的正常使用。如果你非要安装正版,可以去买个正版光盘,安装系统后输入正版激...
- vivo手机锁屏密码忘了怎么办
-
如下:1输入密码在vivo的锁屏页面,连续输入五次错误密码,弹出新页面。2/4选择忘记密码在新的页面中,找到页面中的忘记密码这个选项,点击它。3/4进行身份验证切换到身份验证的页面后,回答两个问题,再...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
python入门到脱坑 输入与输出—str()函数
-
宝塔面板如何添加免费waf防火墙?(宝塔面板开启https)
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
失业程序员复习python笔记——条件与循环
-
使用 python-fire 快速构建 CLI_如何搭建python项目架构
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
