nginx搭建tomcat集群_如何将项目部署到tomcat服务器上

(28) 2024-09-06 14:01:01

nginx搭建tomcat集群_如何将项目部署到tomcat服务器上 (https://mushiming.com/)  第1张

NGINX 文件配置

在.conf文件底部添加如下配置:

upstream transes { 
    server 121.37.156.200:8080; server 121.37.156.200:8088; server 121.37.156.200:8087; } server { 
    listen 80; server_name www.transes.com; location / { 
    proxy_pass http://transes; } } 

负载均衡——轮询

nginx搭建tomcat集群_如何将项目部署到tomcat服务器上 (https://mushiming.com/)  第2张

负载均衡——加权轮询

nginx搭建tomcat集群_如何将项目部署到tomcat服务器上 (https://mushiming.com/)  第3张
weight=1 即权重 ,值越大,访问比例越多

upstream transes2 { server 121.37.156.200:8080 weight=1; server 121.37.156.200:8088 weight=2; server 121.37.156.200:8087 weight=5; } server { listen 80; server_name www.transes2.com; location / { proxy_pass http://transes2; } } 

upstream 指令参数

nginx搭建tomcat集群_如何将项目部署到tomcat服务器上 (https://mushiming.com/)  第4张
max_conns

限制一台服务器的最大连接数,默认0

slow_start

缓慢的启动,该参数必须要有权重参数(weight),必须用在集群内且服务器数量大于1,该参数仅在商业版本中可用

down

标识服务器状态,禁用状态,加上该参数后表示当前服务器不可用

backup

备份,表示备用机,只有其他服务器宕机之后才会启用当前服务器

max_fails

最大失败次数,失败次数达到之后nginx会自动将该服务器设置为宕机状态

fail_timeout

配置失败时间(如15s),如果连续两次请求某个服务为失败状态会自动等待15s,并且15s内不会再有新的请求访问该服务

keepalive提高吞吐量

keepalived `

设置长连接处理的数量

`
proxy_http_version

设置长连接http版本为1.1

proxy_set_header

清除connection header 信息

负载均衡——ip_hash

ip_hash 可以保证用户访问可以请求到上游服务中的固定的服务器,前提是用户ip没有发生更改。 使用ip_hash的注意点:
不能把后台服务器直接移除,只能标记down.

nginx搭建tomcat集群_如何将项目部署到tomcat服务器上 (https://mushiming.com/)  第5张
nginx搭建tomcat集群_如何将项目部署到tomcat服务器上 (https://mushiming.com/)  第6张
添加ip_hash算法规则的负载均衡 只需在配置文件中添加ip_hash即可;

upstream transes2 { ip_hash; server 121.37.156.200:8080; server 121.37.156.200:8088; keepalive 32; } 

负载均衡——url_hash

nginx搭建tomcat集群_如何将项目部署到tomcat服务器上 (https://mushiming.com/)  第7张
添加url_hash算法规则的负载均衡配置方法

upstream transes2 { hash $request_uri; server 121.37.156.200:8080; server 121.37.156.200:8088; keepalive 32; } 

负载均衡——least_coon

根据最少连接数进行请求,每次都请求最小链接数的服务

upstream transes2 { hash $request_uri; least_coon; server 121.37.156.200:8080; server 121.37.156.200:8088; keepalive 32; } 

Nginx ——缓存

nginx搭建tomcat集群_如何将项目部署到tomcat服务器上 (https://mushiming.com/)  第8张
expires 指令
nginx搭建tomcat集群_如何将项目部署到tomcat服务器上 (https://mushiming.com/)  第9张

expires [time]

设置浏览器缓存过期时间

expires @[time]

指定浏览器缓存过期的具体时间

expires -[time]

缓存提前失效时间

expires epoch

不设置cache缓存

expires off

默认关闭缓存

expires max

设置缓存永不过期

server {  
        listen       90;
        server_name  localhost;
         
        location / {
            root   /usr/local/tomact-frontend/webapps/foodie-shop;
            index  index.html; 
        }


        location /static {
           # root   /home;
           alias    /home/imooc;
         # expires 10s;
         # expires @22h30m;  
         # expires -1h;    
         # expires epoch; 
         # expires off; 
           expires max;
        }
    
        location /imooc {
           root   /home;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

Nginx ——反向代理缓存

 # proxy_cache_path 设置缓存目录 # keys_zone 设置共享内存以及占用空间大小 # max_size 设置缓存大小 # inactive 超过此时间则被清理 # use_temp_path 临时目录,使用后会影响nginx性能 proxy_cache_path /usr/local/nginx/upstream_cache keys_zone=mycache:5m max_size=1g inactive=1m use_temp_path=off; server { listen 80; server_name 121.37.156.200; location / { proxy_pass http://transes2; # 启用缓存,和keys_zone一致 proxy_cache mycache; # 针对200和304状态码缓存时间为8小时 proxy_cache_valid 200 304 8h; proxy_http_version 1.1; proxy_set_header Connection ""; } } 

动静分离的特点

nginx搭建tomcat集群_如何将项目部署到tomcat服务器上 (https://mushiming.com/)  第10张

THE END

发表回复