Nginx负载均衡配置与spring+redissession共享

来源:互联网 发布:unity3d卡牌游戏技能 编辑:程序博客网 时间:2024/06/06 00:57

ngnix配置(安装启动等百度搜索,很多很多。)

#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;#每个设备的状态设置为:#1.down 表示单前的server暂时不参与负载#2.weight 默认为1.weight越大,负载的权重就越大。#3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误#4.fail_timeout:max_fails次失败后,暂停的时间。#5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。#负载均衡 , 其算法包括:loop,weight,ip_hash,fair,url_hashupstream proxy_pass_backend {  server 192.168.128.1:8080;server 192.168.128.1:8081;#server 192.168.128.141:15672;      }    server {        listen       80;        #浏览器输入192.168.128.141,访问到这个80端口        server_name  proxy_pass_backend;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {           #root   html;            #index  index.html index.htm;            #输入这个URL就走代理做负载均衡proxy_pass http://proxy_pass_backend/;proxy_redirect default;        }                #图片结尾不能有/location ~*\.(jpg)$ {proxy_pass http://proxy_pass_backend;proxy_redirect default;}#单独指定静态文件路径location ~ .*\.(js|css)$ {proxy_pass http://proxy_pass_backend;}        #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;        }}


spring-mvc.xml

<!-- session 共享 2017-08-15 --><bean id="redisHttpSessionConfiguration"class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"><property name="maxInactiveIntervalInSeconds" value="600" /></bean>

spring-redis.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"><bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"><property name="hostName" value="127.0.0.1" /><property name="port" value="6379" /><!-- <property name="password" value="nitro15$" /> --><property name="poolConfig" ref="jedisPoolConfig" /> </bean><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><!-- 最大连接数, 默认8个 --><property name="maxTotal" value="1024" /><!-- 最大空闲连接数 --><property name="maxIdle" value="200" /><!-- 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1 --><property name="maxWaitMillis" value="5000" /><!-- 在获取连接的时候检查有效性, 默认false --><property name="testOnBorrow" value="true" /> </bean></beans>

web.xml

<!-- session 共享 2017-08-15 --><filter><filter-name>springSessionRepositoryFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping><filter-name>springSessionRepositoryFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- #session 共享 2017-08-15 -->


pom.xlm

<!-- session 共享 2017-08-15 --><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId><version>1.2.2.RELEASE</version></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.1</version></dependency><!-- #session 共享 2017-08-15 -->

server {    listen       88;    server_name  test.ehsure.com;    #charset koi8-r;    #access_log  /var/log/nginx/host.access.log  main;    location / {proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;index index.html index.htm;        proxy_pass http://test.ehsure.com;proxy_redirect off;proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        # Forward the user's IP address to Rails        proxy_set_header X-Real-IP $remote_addr;        # needed for HTTPS        # proxy_set_header X_FORWARDED_PROTO https;        #proxy_set_header X-Forwarded-For $remote_addr;        proxy_set_header X-Forwarded-For $http_x_forwarded_for;        proxy_set_header Host $host:$server_port;  -- 防止NGINX重定向丢失端口号        #proxy_redirect off;        proxy_redirect default;        proxy_connect_timeout 60;        proxy_read_timeout 60;        proxy_send_timeout 60;        root   /usr/share/nginx/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   /usr/share/nginx/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;    #}}