一.Nginx下载与运行
Nginx下载地址:
http://nginx.org/en/download....
下载相应零碎版本:
下载完后解压能够间接应用:
抉择 nginx.exe 右键“以管理员身份运行”,Nginx应用的端口是 “80” 端口号,如果有其余过程在应用 “80” 端口号,需先将其敞开。
拜访地址:http://localhost:80
如有上图提醒,示意Nginx失常运行
在工作管理器中能够查看Nginx的两个过程
1.Nginx主程序过程(占内存大的是主程序)
2.Nginx守护过程
二.Nginx常用命令:
阐明:nginx命令要在nginx.exe所在的目录中执行cmd
命令 | 阐明 |
---|---|
start nginx | 启动 |
nginx -s reload | 重启 |
nginx -s stop | 敞开 |
nginx -t | 查看配置文件是否有误 |
三.Nginx集群规定:
1.轮询策略(默认):
阐明:依据配置文件中的程序,顺次拜访tomcat服务器
#配置域名代理 server { listen 80; server_name manage.jt.com; location / { #代理 服务器拜访地址 #proxy_pass http://localhost:8091; proxy_pass http://jtWindows; } } #构建tomcat服务器集群 #1.轮询策略(默认):按配置程序顺次拜访服务器 upstream jtWindows { server localhost:8081; server localhost:8082; server localhost:8083; }
2.权重策略:
阐明:因为公司外部物理服务器的解决能力可能有高有低,所以须要让高性能的服务器解决更多的申请,采纳权重策略。
能够应用 weight 设置拜访比例,进行拜访服务器
#配置域名代理 server { listen 80; server_name manage.jt.com; location / { #代理 服务器拜访地址 #proxy_pass http://localhost:8091; proxy_pass http://jtWindows; } } #构建tomcat服务器集群 #2.权重策略:是按设置的比例(应用 weight 设置)进行拜访服务器 upstream jtWindows { server localhost:8081 weight=6; server localhost:8082 weight=3; server localhost:8083 weight=1; }
3.IPHash策略:(优先级最高)
阐明:应用tomcat集群搭建服务器后,例如用户在写文章,当电脑呈现问题(宕机)时,重启了电脑后,代理服务器会重新分配服务器端口,用户重启前的操作将不能再获取,需从新操作?应该如何优化?
解决方案:
1) 能够让用户与服务器进行绑定
2) 将用户的操作信息保留到第三方,之后无论从哪台服务器获取都是雷同的后果
IPHash算法:
应用 hash 算法把(ip与端口号)得出的哈希码值,进行求余(如配置三台服务器就是 %3)后,与服务进行匹配拜访
#配置域名代理 server { listen 80; server_name manage.jt.com; location / { #代理 服务器拜访地址 #proxy_pass http://localhost:8091; proxy_pass http://jtWindows; } } #3.IPHash(优先级最高):应用 hash算法把(ip与端口号)得出的哈希码值后,进行求余(如配置三台服务器就是 %3)与服务进行匹配拜访 # max_fails=1 :设定最大的失败次数 # fail_timeout=60s :设定失败的超时工夫(周期时间) # 如果 Nginx申请达到了最大的失败次数的服务器,则在规定的工夫内,不会再把申请发给该 tomcat服务器,直到下一个周期 upstream jtWindows { server localhost:8081 weight=6; server localhost:8082 weight=3; server localhost:8083 weight=1; }
4.Nginx属性:
server:定义代理的服务器
listen:配置监听端口号
server_name:配置监听域名名称
location:开始反向代理
/ :示意拦挡所有
root:配置代理的是目录
index:默认拜访的页面
proxy_pass:代理服务器拜访地址
upstream:定义代理服务器集群
down:该属性示意以后服务器不参加负载(可能是宕机)
backup:该属性用来标识备用服务器,失常状况下,用户不会再次拜访备用服务,除非以后服务器忙碌/以后服务器宕机,才有可能拜访备用服务器
max_fails=1:设定最大的失败次数
fail_timeout=60s:设定失败的超时工夫(周期时间)
如果 Nginx申请达到了最大的失败次数的服务器,则在规定的工夫内,不会再把申请发给该 tomcat服务器,直到下一个周期
5.Nginx配置文件 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;}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 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; # 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; # } #} #配置图片服务器 server { #配置监听端口,个别条件下都是80端口 listen 80; #配置拜访域名 server_name image.jt.com; location / { # root代表映射文件目录 root E:/jt-image; } } #配置域名代理 server { listen 80; server_name manage.jt.com; location / { #代理 服务器拜访地址 #proxy_pass http://localhost:8091; proxy_pass http://jtWindows; } } #构建tomcat服务器集群 # upstream 关键字,定义集群 #规定: #1.轮询策略(默认):按配置程序顺次拜访服务器 #2.权重策略:是按设置的比例(应用 weight 设置)进行拜访服务器 #3.IPHash(优先级最高):应用 hash算法把(ip与端口号)得出的哈希码值后,进行求余(如配置三台服务器就是 %3)与服务进行匹配拜访 # down:该属性示意以后服务器不参加负载(可能是宕机) # backup:该属性用来标识备用服务器,失常状况下,用户不会再次拜访备用服务,除非以后服务器忙碌/以后服务器宕机,才有可能拜访备用服务器 # max_fails=1 :设定最大的失败次数 # fail_timeout=60s :设定失败的超时工夫(周期时间) # 如果 Nginx申请达到了最大的失败次数的服务器,则在规定的工夫内,不会再把申请发给该 tomcat服务器,直到下一个周期 upstream jtWindows { ip_hash; #server localhost:8081 weight=6 down; #server localhost:8083 weight=1 backup; server localhost:8081 max_fails=1 fail_timeout=60s; server localhost:8082 max_fails=1 fail_timeout=60s; server localhost:8083 max_fails=1 fail_timeout=60s; } #编辑nginx配置文件必须在协定外部实现}
四.tomcat集群搭建流程图
五.动静获取tomcat端口号
package com.jt.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class PortController { //动静获取以后端口号信息 @Value("${server.port}") private Integer port; @RequestMapping("/port") public String getPort(){ return "以后端口号:"+port; }}
六.Nginx实现tomcat服务器高可用
高可用HA (High Availability):当服务器产生故障时,无需人为的干涉,能够自动化的实现故障的迁徙。
阐明:当服务器宕机,如果由人工来实现,效率较低,能够通过程序主动切换来实现。
#配置域名代理 server { listen 80; server_name manage.jt.com; location / { #代理 服务器拜访地址 #proxy_pass http://localhost:8091; proxy_pass http://jtWindows; } } # max_fails=1 :设定最大的失败次数 # fail_timeout=60s :设定失败的超时工夫(周期时间) # 如果 Nginx申请达到了最大的失败次数的服务器,则在规定的工夫内,不会再把申请发给该 tomcat服务器,直到下一个周期 upstream jtWindows { server localhost:8081 max_fails=1 fail_timeout=60s; server localhost:8082 max_fails=1 fail_timeout=60s; server localhost:8083 max_fails=1 fail_timeout=60s; }