windows 环境下nginx+redis+tomcat环境配置 实现负载均衡 静态代理

 

一. nginx  优缺点  

    请参考 https://blog.csdn.net/a_haogg/article/details/72843820

 二. 开发工具  tomcat 2个   、redis、nginx 及 jar

    nginx下载地址 https://download.csdn.net/download/qq_27259009/10339497

 

    tomcat-redis-session-manager-1.2-tomcat-7.jar

    jedis-2.2.0.jar

    commons-pool-1.6.jar

三. 配置

    

大致的整个配置的架构:

 

 

     

在这个图中,nginx做为反向代理,将客户请求根据权重随机分配给三台tomcat服务器,redis做为三台tomcat的共享session数据服务器。

   1.tomcat

        (1)准备三个tomcat容器

        (2)分别修改三台 tomcat 的  目录下的 config/server.xml 中的端口号(修改端口号防止端口冲突,可以实现三台tomcat在一台服务器上同时启动),要修改三个位置  的 port ,端口号不能相同,建议 8080以后的端口

       

   

       

     

 (3)分别修改 三个tomcat 文件夹下   context.xml

    在<context></context>中加入

            <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve"/>

    <Manager className="com.radiadesign.catalina.session.RedisSessionManager" database="0"        host="192.168.0.117" maxInactiveInterval="60" port="6379"/>

用于和redis建立链接,注意   host是 redis 的访问地址  databse指的是redis的库名

(4) 将上文中提到的三个jar 分别放到 三个tomcat 目录下的lib目录中,这个地方jar文件的版本有可能会有冲突,配置的时候需要多尝试。我这里的版本如下,是验证过可以使用的,通过maven的库都可以下载到。

2. redis 配置(若reids 和 三个tomcat 在同一台主机上,则不需要进行redis的配置),

我是将redis服务器和其中一个tomcat  放在了同一台服务器上,另外两个tomcat放置在另一台服务器上。修改redis目录下的redis.windows.conf 文件。 redis   bind默认绑定本机 127.0.0.1 如果要使另一台服务器的tomcat也能访问就修改 bind 地址  ,地址为redis 服务器的ip地址。注意 此处的ip地址应和 上文tomcat context.xml中加入这段代码中的 host的ip地址相同。

  

  

redis.windows.conf修改后 下文会提到redis的启动方式

3.nginx配置

   打开nginx目录下的 nginx_config.xml 修改其中的配置

 

    在访问静态文件时 ,已jsp页面为例

原来的访问路径是

<script type="text/javascript" src="<%=basePath%>/pc_back/jquery-easyui-1.5/locale/easyui-lang-zh_CN.js"></script>

添加静态代理后

<script type="text/javascript" src="/jquery-easyui-1.5/locale/easyui-lang-zh_CN.js"></script>

附上完整配置

 
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        
        location ~ \.(jsp|do)$ {
        	proxy_next_upstream http_502 http_504 error timeout invalid_header;
   			proxy_pass http://myServer;
    		# 真实的客户端IP
    		proxy_set_header   X-Real-IP        $remote_addr; 
    		# 请求头中Host信息
    		proxy_set_header   Host             $host; 
   			# 代理路由信息,此处取IP有安全隐患
    		proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    		# 真实的用户访问协议
    		proxy_set_header   X-Forwarded-Proto $scheme;

            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 ~\.(html|htm|gif|jpg|bmp|png|js|css){
        	root E:static\pc_back;
        	#expires 30d;
        }
        #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;
    #    }
    #}

}

四.工具启动和项目访问

       启动建议 先启动redis(启动redis和要通过命令启动,让redis启动时读取redis.windows.conf文件的配置)、再启动  tomcat、最后启动nginx。

     window 窗口命令 进入redis  -config目录 输入   redis-server.exe redis.windows.conf 启动redis.

    

       所有配置完成,工具启动后。访问项目时直接访问 nginx代理服务器所在的服务器地址 +端口 (默认80)+项目名

        

            

 

 

 

 

 

               

  

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值