nginx集群与高并发
本篇介绍nginx,nginx的作用是反向代理和负载均衡。
目录
一、nginx简介
二、nginx安装
三、nginx配置
3.1 在nginx上部署两个项目
3.2 nginx负载均衡
四、nginx负载均衡时session问题
4.1 安装memcached
4.2 配置监听地址
4.3 tomcat服务器中添加jar包
4.4 tomcat配置
4.5 nginx中配置负载均衡(与3.2节相似)
4.5 测试
一、nginx简介
概念:nginx=反向代理+负载均衡
并发量:5万~10万
注意:客户端是与nginx建立的连接,后端tomcat不能直接与客户端交互,需要通过nginx与客户端交互。
二、nginx安装
在此以tengine-2.1.0.tar.gz 为例。
解压
tar -xzvf tengine-2.1.0.tar.gz
安装编译器
yum install gcc pcre-devel openssl-devel -y
进入到解压后的根目录,用下列命令安装到/myapp/nginx目录中
./configure --prefix=/myapp/nginx
make
make install
启动/停止nginx服务器
/myapp/nginx/sbin/nginx # 启动
/myapp/nginx/sbin/nginx -s stop # 停止
配置nginx服务器
路径为:/myapp/nginx/conf/nginx.conf,下面为解释
#运行用户
user nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes 1;#全局错误日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;#工作模式及连接数上限
events {#epoll是多路复用IO(I/O Multiplexing)中的一种方式,#仅用于linux2.6以上内核,可以大大提高nginx的性能use epoll; #单个后台worker process进程的最大并发链接数 worker_connections 1024;# 并发总数是 worker_processes 和 worker_connections 的乘积# 即 max_clients = worker_processes * worker_connections# 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么# 为什么上面反向代理要除以4,应该说是一个经验值# 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000# worker_connections 值的设置跟物理内存大小有关# 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数# 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右# 我们来看看360M内存的VPS可以打开的文件句柄数是多少:# $ cat /proc/sys/fs/file-max# 输出 34336# 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内# 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置# 使得并发总数小于操作系统可以打开的最大文件数目# 其实质也就是根据主机的物理CPU和内存进行配置# 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。# ulimit -SHn 65535 # 该命令用来修改linux中一个进程最多文件描述符的个数为65535!}http {#设定mime类型,类型由mime.type文件定义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 函数(zero copy 方式)来输出文件,#对于普通应用,必须设为 on,#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,#以平衡磁盘与网络I/O处理速度,降低系统的uptime.sendfile on;#tcp_nopush on;#连接超时时间#keepalive_timeout 0;keepalive_timeout 65;tcp_nodelay on;#开启gzip压缩gzip on;gzip_disable "MSIE [1-6].";#设定请求缓冲client_header_buffer_size 128k;large_client_header_buffers 4 128k;#设定虚拟主机配置server {#侦听80端口listen 80;#定义使用 www.nginx.cn访问server_name www.nginx.cn;#定义服务器的默认网站根目录位置root html;#设定本虚拟主机的访问日志access_log logs/nginx.access.log main;#默认请求location / {#定义首页索引文件的名称index index.php index.html index.htm; }# 定义错误提示页面error_page 500 502 503 504 /50x.html;location = /50x.html {}#静态文件,nginx自己处理location ~ ^/(images|javascript|js|css|flash|media|static)/ {#过期30天,静态文件不怎么更新,过期可以设大一点,#如果频繁更新,则可以设置得小一点。expires 30d;}#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.location ~ .php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}#禁止访问 .htxxx 文件location ~ /.ht {deny all;}}
}
nginx的默认conf文件如下:
#user nobody;
worker_processes 1;#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;
}# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}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 on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;#charset koi8-r;#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;##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;# }#}}
三、nginx配置
3.1 在nginx上部署两个项目
通过修改nginx.conf中的配置实现一个nginx部署两个域名的项目,同时监听80端口,当访问不同域名时显示的结果也不同,修改如下:
注意:nginx运行在虚拟机中,windows浏览器中输入域名要在C:\Windows\System32\drivers\etc\hosts中添加“192.168.182.140 www.xuewudu1.com www.xuewudu2.com”,这样本地会将域名解析成虚拟机中的ip地址,从而实现访问。
#user nobody;
worker_processes 1;#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;
}# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}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 on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;
# ------------------------------以下内容有修改!----------------------------------# 添加第一个域名,同样监听80端口,location中为对应uri显示的内容server { listen 80;server_name www.xuewudu1.com;location / {root /mnt;autoindex on;}}# 第二个域名,同样监听80端口,为nginx默认的,只修改了server_name后的域名server {listen 80;server_name www.xuewudu2.com;#charset koi8-r;#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;# }#}}
3.2 nginx负载均衡
在nginx.conf文件中,http内,server外,定义upstream结构,里面填写负载均衡的后端服务器;server中location转发的时候不要直接填写一台后端服务器的IP地址,而是转发到刚才定义的upstream结构的变量中。
以下内容在3.1基础上接着操作
#user nobody;
worker_processes 1;#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;
}# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}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 on;#tcp_nopush on;# --------------修改位置1:连接超时时间为0,为了测试负载均衡的切换---------------keepalive_timeout 0;#keepalive_timeout 65;
# --------------修改位置1结束!---------------#gzip on;# --------------修改位置2:创建upstream变量xxx,里面为后端服务器ip地址---------------upstream xxx {server 192.168.182.141;server 192.168.182.142;}
# --------------修改位置2结束!---------------server {listen 80;server_name www.xuewudu1.com;location / {root /mnt;autoindex on;}}server {listen 80;server_name www.xuewudu2.com;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}# --------------修改位置3---------------
# 当该uri匹配上时会触发负载均衡,因为里面的转发路径为位置2中的upstream变量xxx,它包括多个后端location /test {proxy_pass http://xxx/;}
# --------------修改位置3结束!---------------#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;# }#}}
重启nginx服务实现应用配置
/myapp/nginx/sbin/nginx -s reload
测试
在windows浏览器中输入www.xuewudu2.com/test,不断刷新会实现负载均衡。
四、nginx负载均衡时session问题
由于负载均衡会切换后端服务器,那么在切换到另一台服务器后没用上一台服务器中的内容,需要重新登录等操作。
解决方法:将后端服务器中的session放到同一个地方(内存数据库中,在此使用memcached)。
4.1 安装memcached
在nginx服务器上安装memcached,命令如下
yum -y install memcached
4.2 配置监听地址
memcached -d -m 128m -p 11211 -l 192.168.182.140 -u root -P /tmp/
4.3 tomcat服务器中添加jar包
添加以下jar包放到tomcat安装目录中的lib/下
4.4 tomcat配置
修改session存放的位置
对每个后端服务器配置tomcat,在tomcat里面的context.xml中最后面添加如下内容:
其中红色部分为session存放的服务器的ip地址,在此为nginx服务器的ip地址(因为session存放到这里)192.168.182.140。
修改index页面(两个后端tomcat服务器都要改)
为了显示session是否相同修改默认index页面,便于测试。
vi webapps/ROOT/index.jsp
修改为
from 192.168.182.141<br>session=<%=session.getId*()%>
配置完后重启tomcat服务器使之生效!
4.5 nginx中配置负载均衡(与3.2节相似)
nginx.conf中新建upstream类型如下:
upstream tom {server 192.168.182.141:8080;server 192.168.182.142:8080;
}
nginx.conf中(www.xuewudu2.com中的server中)新建location,如下
location /cat {proxy_pass http://tom/;
}
4.5 测试
在windows浏览器中输入www.xuewudu2.com/cat,刷新会实现负载均衡,并且session不变。
nginx集群与高并发相关推荐
- 集中式、分布式、集群、高并发等概念
目录 集中式.分布式.集群系统 集中式系统 分布式系统 分布式与集群 高并发 什么是高并发 集中式.分布式.集群系统 常见概念 1. 高可用:一年内允许系统不可用的时间 7*24 99.9% 99. ...
- 基于LVS高可用架构实现Nginx集群分流
Nginx实用插件_踩踩踩从踩的博客-CSDN博客 前言 前面文章介绍Nginx的核心及扩展插件必要的性能优化,以及在nginx中如何实用用https:本篇文章会继续讲解重要的概念 lvs高可用框架, ...
- Keepalived+nginx 集群解决单点故障
Keepalived+nginx 集群解决单点故障 nginx作为负载均衡器,所有请求都到了nginx服务器,可见nginx处于一个非常重要的位置,如果nginx服务器宕机,那么后台的服务器将无法提供 ...
- Java架构师:单体部署 ->Nginx 集群 -> + Keepalived“高可用”组件 -> + LVS负载均衡
一.集群阶段开篇概述 1.单体部署 1.1 单台服务器(节点)部署 1.2.多台服务器(节点)部署 集群.分布式.微服务中的各个服务器节点必须互通,必须在同一个局域网(内网要通) 1.3.单体架构的优 ...
- nginx两台文件服务器集群,keepalived结合nginx状态检测脚本实现对web服务器集群的高可用...
实验环境 两台CentOS-7.5虚拟机 web1:10.0.11.203 web2:10.0.11.204 VIP :10.0.11.210 web类型:nginx 客户端:自用笔记本(win10) ...
- 构建一套高逼格 Nginx 集群监控系统!
点击关注公众号,实用技术文章及时了解 搭建了Nginx集群后,需要继续深入研究的就是日常Nginx监控. Nginx如何监控?相信百度就可以找到:nginx-status 通过Nginx-status ...
- Redis + Tomcat + Nginx 集群实现 Session 共享
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者 | 蕃薯耀 链接 | www.cnblogs.com/fan ...
- 蚂蚁集团万级规模 k8s 集群 etcd 高可用建设之路
蚂蚁集团运维着可能是全球最大的 k8s 集群:k8s 官方以 5k node 作为 k8s 规模化的顶峰,而蚂蚁集团事实上运维着规模达到 10k node 规模的 k8s 集群.一个形象的比喻就是,如 ...
- C#session共享+redis_Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享
作者:蕃薯耀 链接:www.cnblogs.com/fanshuyao 一.Session共享使用tomcat-cluster-redis-session-manager插件实现 插件地址见: htt ...
最新文章
- mysql pdo 查询一条数据_pdo mysql怎么输出第1条 第4条 第7条数据
- pandas重置dataframe的索引(reset_index)、如果索引不匹配dataframe操作时候的问题、重置索引(不设置drop=true)远索引生成新的数据列
- 你不知道的windows7 技巧大全【3】
- Django中的缓存的配置与使用
- linux mysql进阶_mysql进阶学习二之搭建主从
- python从入门到实践15章的几个自己的小程序
- Windows下同时安装Anaconda2(Python2)和Anaconda3(Python3)以及tensorflow
- 路由器配置——广播多路访问链路上的OSPF
- Kali Linux 无线渗透测试入门指南 第九章 无线渗透测试方法论
- 计算机系统的输入与输出接口是,计算机输入输出系统与接口技术
- 《给你一个团队,你能怎么管?》读书笔记
- 高等数学复习要点(期末考试同济版)
- 基于ssm汽车4s店维修保养试驾服务管理系统 java毕设项目介绍
- 多维度分析评价体系:高校教学质量大数据应用解决方案
- 微服务2——服务的注册,调用(Nacos服务注册中心+服务调用+调用负载均衡)sca-comsumersca-provider
- html学习 - jquery事件监听详解
- Java.Canvas
- ★Kali信息收集★8.Nmap :端口扫描
- XStream 转换 ListString的方法
- 维特比算法的简单实现