zoukankan      html  css  js  c++  java
  • Nginx与tomcat 实现负载均衡和动静分离

    一 配置
    1. nginx介绍就不多说了,我使用的是阿里云的centos ,使用yum命令安装 的,很容易。
    2. nginx的配置文件:nginx.conf配置
    我参考文章,点击查看

    配置文件如下:
    # For more information on configuration, see:
    #   * Official English Documentation: http://nginx.org/en/docs/
    #   * Official Russian Documentation: http://nginx.org/ru/docs/
    
    user              nginx;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log;
    #error_log  /var/log/nginx/error.log  notice;
    #error_log  /var/log/nginx/error.log  info;
    
    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  0;
        keepalive_timeout  65;
    
        #gzip  on;
        #负载均衡设置,暂时未用到,后面会使用到
        upstream www.###.cn {
        #ip_hash;
            server 127.0.0.1:8080;
        }
        server {
            listen       80;
            server_name  www.lexinli.cn;
            index index.html index.jsp;
            root /home/wwwftp/ROOT;
            #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
            location ~ .*.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ 
            { 
                  root /home/wwwftp/ROOT; 
                  #expires定义用户浏览器缓存的时间为1天,如果静态页面不常更新,可以设置更长这样可以节省带宽和缓解服务器的压力
                  expires      1d; 
             } 
            #所有jsp、do的动态请求都交给后面的tomcat处理 
            location ~ (.jsp)|(.action)$ 
            { 
    
                  #tomcat地址
                  proxy_pass http://127.0.0.1:8080;  
                  proxy_redirect off;  
                  proxy_set_header HOST $host;  
                  proxy_set_header X-Real-IP $remote_addr;  
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
                  client_max_body_size 10m;  
                  client_body_buffer_size 128k;  
                  proxy_connect_timeout 90;  
                  proxy_send_timeout 90;  
                  proxy_read_timeout 90;  
                  proxy_buffer_size 4k;  
                  proxy_buffers 4 32k;  
                  proxy_busy_buffers_size 64k;  
                  proxy_temp_file_write_size 64k;  
            }      
            #location / {  
            #        proxy_connect_timeout   3;  
            #        proxy_send_timeout      30;  
            #        proxy_read_timeout      30;  
            #            proxy_pass http://www.lexinli.cn;  
            #   }
        }
        # Load config files from the /etc/nginx/conf.d directory
        # The default server is in conf.d/default.conf
        include /etc/nginx/conf.d/*.conf;
    
    }
    

    配置完成后 /usr/sbin/nginx -s reload 重新加载配置文件

    二. 测试效果:
    测试Nginx 和Tomcat高并发的情况下处理静态页面性能如何?
    采用了 Linux ab网站压力测试命令来测试一下性能
    ab -c 10 -n 1000表示同时处理100个请求并运行1000次index.html文件
    1.测试一下Nginx 处理静态页面的性能
    访问nginx的:
    ab -c 10 -n 1000 http://www.lexinli.cn/index.html
    每秒请求数结果:Requests per second: 7095.22 [#/sec] (mean)
    直接访问tomcat的:
    ab -c 10 -n 1000 http://www.lexinli.cn:8080/index.html
    每秒请求数结果:Requests per second: 1675.85 [#/sec] (mean)
    静态交给Nginx处理,动态请求交给Tomcat,提高了性能

  • 相关阅读:
    Redis 连接命令
    Redis 脚本
    Redis 事务
    Redis 发布订阅
    Redis HyperLogLog
    Redis 有序集合(sorted set)
    Redis 集合(Set)
    Redis 列表(List)
    Redis 哈希(Hash)
    特定消费者的限制流量
  • 原文地址:https://www.cnblogs.com/luleiitlife/p/8545075.html
Copyright © 2011-2022 走看看