最近自己搭了一套nginx的環境,集群部署了公司的一個項目,中間解決了session共享的問題。記錄如下,以備日后查看。
1.環境
windows10 家庭中文版,jdk 7, tomcat 7.0.27 , nginx-1.10.1 windows, Redis-x64-2.8.2402 windows.
tomcat 我拷貝了一份,並修改server.xml:
<Server port="8006" shutdown="SHUTDOWN">
<!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> --> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 --> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation --> <!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
這樣,兩個tmcat啟起來就時兩台服務器。
注意事項:windows用戶必須用Administrator, 否則redis不能正常使用
2. redis依賴包
commons-pool2-2.2,jedis-2.5.2,tomcat-redis-session-manager-2.0.0 ,
注意事項:
如果使用jdk 6,啟動時會提示 “java.lang.UnsupportedClassVersionError: com/orangefunction/tomcat/redissessions/RedisSessionHandlerValve : Unsupported major.minor version 51.0” , jdk7的版本號時51,所以應該是最小支持jdk7,其他版本的tomcat-redis-session-manager可能支持jdk的其他版本,大家自行實驗。
3. nginx 主要配置
#設定負載均衡的服務器列表
upstream mysvr {
server localhost:8080;
server localhost:8081;
#ip_hash;
}
server {
listen 80; #監聽端口
server_name cq.demo.com; #域名可以有多個,用空格隔開
charset utf-8;
#access_log logs/host.access.log main;
location / {
proxy_pass http://mysvr; #請求轉向mysvr 定義的服務器列表
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_send_timeout 10;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#靜態文件,nginx自己處理
location ~ .*\.(html|gif|jpg|jpeg|bmp|png|ico|txt|js|css|woff2|woff|ttf)$
{ root html/static;
#過期30天,靜態文件不怎么更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。
expires 30d;
#proxy_pass http://mysvr;
}
}
4. tomcat修改context.xml配置
Context標簽加入:
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />
先寫到這,比較亂,沒有按照操作順序,只是記錄了操作中的重點,其實操作順序是次要的,解決操作中遇到的問題才是最重要的。
