- 環(huán)境
centos6.5 nginx-1.8.1 tomcat7.0.75 redis2.8 - nginx搭建
- 安裝nginx 依賴包
yum install -y pcre pcre-devel
yum install -y openssl openssl-devel
- 安裝nginx
\#tar -zxvf nginx-1.8.1.tar.gz //解包
\#mv nginx-1.8.1 nginx //進入目錄
\#cd nginx //進入目錄
./configure --user=chenshb --group=chenshb --prefix=/data1/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-cc-opt='-O3' --with-cpu-opt=opteron --with-http_gzip_static_module
//面我們指定把nginx安裝到/data1/nginx目錄下并且安裝Nginx性能檢測模塊和SSL安全模塊以及FLV模塊支持
make && make install //編譯和安裝
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local //添加到開機自啟動
- 目錄說明:4個文件目錄 conf-->配置文件 html--->程序目錄 logs--->日志目錄 sbin--->命令目錄
- Nginx相關(guān)操作命令
# /usr/local/nginx/sbin/nginx // 啟動nginx
# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid` //停止nginx
#kill -HUP `cat /usr/local/nginx/logs/nginx.pid` //重啟nginx
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}' 輸入此命令查看Nginx主進程號屏幕顯示的即為Nginx主進程號,例如: 6302
這時偎血,執(zhí)行以下命令即可使修改過的Nginx配置文件生效:
#kill -HUP 6302- 平滑重啟nginx:(比如你編輯了nginx.conf配置了文件诸衔,那么無需要重新啟動nginx
執(zhí)行以下命令即可立即加載!)
對于Nginx 0.8.x以上的版本颇玷,現(xiàn)在平滑重啟Nginx配置非常簡單笨农,執(zhí)行以下命令即可:
#/usr/local/nginx/sbin/nginx -s reload
#ps -aux | grep nginx
- 配置nginx (具體可參考nginx配置文件的詳解)
- 修改nginx.conf
# erdo
user root;
worker_processes 4;
events {
worker_connections 8192;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 200m;
client_body_temp_path /usr/local/nginx/client_body_temp;
fastcgi_buffers 4 64k;
fastcgi_buffer_size 64k;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_connect_timeout 300;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_disable "msie6";
gzip_min_length 1024;
gzip_http_version 1.0;
gzip_types text/plain text/xml application/xml text/javascript text/css application/x-javascript application/javascript;
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_set_header X-Forwarded-For2 $proxy_add_x_forwarded_for;
#add_header X-Frame-Options "SAMEORIGIN";
# deny ip/empty_server_name access
server {
listen 80;
server_name _;
return 403;
}
include /usr/local/nginx/conf/vhost.d/*conf;
}
# vim:set et ts=2 sw=2: #
- 在目錄vhost.d下增加tomcatfedin.conf
upstream turn_server {
server localhost:80;
}
upstream tomcat {
#ip_hash;
server 192.168.172.102:8080 max_fails=3 weight=1 fail_timeout=60s;
server 192.168.172.102:8081 max_fails=3 weight=1 fail_timeout=60s;
}
server {
listen 80;
server_name 192.168.172.102;
server_name_in_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;
proxy_set_header X-Forwarded-Proto $scheme;
access_log /usr/local/nginx/logs/error_log;
error_log /usr/local/nginx/logs/error_log;
root /usr/local/nginx/html;
index index.php index.html;
# issues1096
if ($request_uri ~ " ") {
return 404;
}
# issues529 940
location ~* ^/(gtyc|web)/login\.php {
return 404;
}
location ~* ^/tool/dologin\.php {
return 404;
}
# issues529 940
#location ~* ^/ebook/(book_index|denglu|dinggou)\.php {
# return 404;
# }
location ~* ^/ePubManager {
proxy_pass http://turn_server;
}
#tomcat
location ~* ^/tf/tm1 {
proxy_pass http://localhost:8080;
}
location ~* ^/tf/tm2 {
proxy_pass http://localhost:8081;
}
location ~* ^/docs {
proxy_pass http://tomcat;
}
location ~* {
# proxy_pass http://turn_server;
proxy_pass http://tomcat;
}
# include /usr/local/nginx/conf/vhost.d/comm;
}
- tomcat 安裝(具體可以參考網(wǎng)絡(luò)上)
安裝兩個相同配置的tomcat8080 tomcat8081
在tomcat ROOT下新建JSP文件
unzip apache-tomcat-6.0.44.zip
mv apache-tomcat-6.0.44 tomcat6
-
啟動nginx tomcat與簡單測試
修改ROOT 目錄下index.html 在各自的服務(wù)器上加入8080或8081;訪問http://192.168.172.102
則輪替出現(xiàn)如下圖所示;表示已完成nginx+tomat集群的搭建
tomcat集群間session的共享
session 存在數(shù)據(jù)庫中(會加大數(shù)據(jù)庫的IO,增加數(shù)據(jù)庫的負擔(dān))
session存在memcache或者redis中
ngin中x的ip_hash帖渠,能夠?qū)⒛硞€IP的請求定向到同一臺后端;
upstream nginx.example.com
{
server 192.168.172.102:8081;
server 192.168.172.102:8080;
ip_hash;
}
server
{
listen 80;
location /
{
proxy_pass
http://nginx.example.com;
}
}
- session共享
- redis 配置 192.168.172.102:6379
- 兩個tomcat7配置: 在server.xml或context.xml中配置
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="192.168.172.103"
port="6379"
database="0"
maxInactiveInterval="60"/>
建議配置在context.xml 需在$tomcat/lib/下加增加JAR包:commons-pool-1.6.jar谒亦、jedis-2.1.0.jar、tomcat-redis-session-manager-1.2-tomcat-7.jar 下載地址空郊。
- tomcat 第二種配置 如果需要支持redis集群的配置時份招,可以參考來開源項目https://github.com/jcoleman/tomcat-redis-session-managers; tomcat配置:
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.172.103"
port="6379"
database="0"
maxInactiveInterval="60"/>
此配置及jar包下載參考https://github.com/izerui/tomcat-redis-session-manager;
- 測試session 共享示例
在兩個tomcat7 下加入 test.jsp
<%@ page language="java" %>
<html>
<head><title>Tomcat8081</title></head>
<body>
<h1><font color="red">Tomcat8081.test</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("test","test"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
測試效果:
出現(xiàn)以上效果說是有session共享成功