Nginx
Nginx简介
概念
Nginx是一款高性能的反向代理服务器;也是一个IMAP、POP3、SMTP代理服务器;也是一个Http服务器。也就是说Nginx本身就可以托管网站,进行Http服务处理,也可以作为反向代理服务器使用。
反向代理
反向代理正好与正向代理相反,对于客户端而言代理服务器就像是原始服务器,并且客户端不需要进行任何特别的设置。客户端向反向代理的命名空间(name-space)中的内容发送普通请求,接着反向代理将判断向何处(原始服务器)转交请求,并将获得的内容返回给客户端。
特点
- 跨平台:可以在linux上和win上运行
- 配置简单:在win运行的时候只需要更改 conf/nginx.conf
- 非阻塞、高并发连接:数据复制时,磁盘I/O的第一阶段是非阻塞的。官方测试能支持5万并发连接,实际生产中能跑2~3万并发连接数(得益于Nginx采用了最新的epoll事件处理模型(消息队列)。
- Nginx代理和后端Web服务器间无需长连接;
- Nginx接收用户请求是异步的,即先将用户请求全部接收下来,再一次性发送到后端Web服务器,极大减轻后端Web服务器的压力。
- 发送响应报文时,是边接收来自后端Web服务器的数据,边发送给客户端。
- 网络依赖性低,理论上只要能够ping通就可以实施负载均衡,而且可以有效区分内网、外网流量。
- 支持内置服务器检测。Nginx能够根据应用服务器处理页面返回的状态码、超时信息等检测服务器是否出现故障,并及时返回错误的请求重新提交到其它节点上。
- 采用Master/worker多进程工作模式
- 此外还有内存消耗小、成本低廉(比F5硬件负载均衡器廉价太多)、节省带宽、稳定性高等特点。
内部进程模型
Nginx是以多进程的方式来工作的,当然Nginx也是支持多线程的方式的,只是我们主流的方式还是多进程的方式,也是Nginx的默认方式
Nginx在启动后,会有一个master进程和多个worker进程。master进程主要用来管理worker进程,包含:接收来自外界的信号,向各worker进程发送信号,监控 worker进程的运行状态,当worker进程退出后(异常情况下),会自动重新启动新的worker进程。而基本的网络事件,则是放在worker进程中来处理了。多个worker进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的。一个请求,只可能在一个worker进程中处理,一个worker进程,不可能处理其它进程的请求。worker进程的个数是可以设置的,一般我们会设置与机器CPU核数一致,这里面的原因与Nginx的进程模型以及事件处理模型是分不开的。
实际应用
可以没事ping百度的网址,不同地区返回的ip地址是不一样的,代表使用了反向代理来根据地区进行服务器的分配
Nginx集群搭建
win平台
工具
nginx:找个官网下载一个
apache-tomcat-7.0.56:听说这个版本对这个兼容的比较好
目标
实现高性能负载均衡的Tomcat集群
正题
-
解压两个Tomcat可以分别命名为:
- apache-tomcat-7.0.56-1
- apache-tomcat-7.0.56-2
-
修改这两个的端口(网上的教程那都是,随便用一个)
-
启动两个tomcat,并访问
-
**(重点)**修改conf/nginx.conf配置文挡
#user nobody;
worker_processes 1;#工作进程的个数,一般与cpu的个数一致
#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 {
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;#开启高效文件传输模式,sendfile指定nginx是否调用sendfile函数传输文件,普通应用设为on;如果用来进行下载等磁盘IO重负载应用,可设为off,以平衡磁盘与网络IO的处理速度,降低系统的负载。注意:如果图片显示不正常,应设为off。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;#长连接超时时间,单位为秒
#gzip on;#启用gzip压缩
#服务器集群配
upstream jeffrey.com{#服务器集群名称
server 127.0.0.1:18080 weight=1;#服务器配置,weight是权重,权重越大,分配的概率越大。
server 127.0.0.1:28080 weight=2;#要是在自己电脑里面配置可以把127.0.0.1换成localhost,端口号按照自己配置的tomcat走
}
#当前的nginx配置
server {
listen 8888;#侦听端口号
server_name localhost;#当前服务的域名.如果是localhost:8888的请求,则交给jeffrey.com的集群来处理。
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
proxy_pass http://jeffrey.com ; #与服务器集群的名称一致
proxy_redirect default;
}
#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常用命令(用小黑框)
- 启动 start nginx / nginx.exe
- 停止 nginx.exe -s stop / nginx.exe -s quit (stop快速停止/quit有序停止)
- 重载 nginx.exe -s reload (主要用于配置修改后的重新载入) -
- 查看nginx版本 nginx -v