windows和linux下nginx和tomcat配置备忘

一、windows:

1.进行多个tomcat的配置

 复制tomcat文件夹 


2.解压nginx


3.tomcat的配置端口号


4.双击nginx.exe即可运行:打开浏览器http://localhost:80显示如下页面:


5.打开nginx-1.8.0\conf\nginx.conf这个文件修改配置:


6.重启nginx 

taskkill /f /t /im nginx.exe
start nginx.exe

保存为bat文件 可进行重启nginx操作

7.配置Tomcat中session的共享:

修改server.xml文件 

 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster">    
        <Channel className="org.apache.catalina.tribes.group.GroupChannel">    
            <Membership className="org.apache.catalina.tribes.membership.McastService"    
                address="228.0.0.4"    
                port="45564"    
                frequency="500"    
                dropTime="3000"/>    
        </Channel>    
    </Cluster>  
修改项目的web.xml文件:
web.xml中添加<distributable/>

8.进行测试 使用下边html文件部署到tomcat中进行sessionID测试

<%@ page language="java" import="java.util.*" contentType="text/html;charset=GBK"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

    <title>tomcat</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
   

</head>

<body>
<b>
    sessionID:<%= session.getId() %> <br>

</b>
</body>
</html>
二、linux下进行 nginx和tomcat的配置+共享session

1.安装nginx

下载nginx 使用工具上传到linux

2.安装必要环境

(1)安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:

yum install gcc-c++ 
(2)PCRE PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

yum install -y pcre pcre-devel
(3)zlib zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

yum install -y zlib zlib-devel
(4)openssl OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y openssl openssl-devel

3.进行编译

进入nginx-1.8.0解压后的文件 中使用 ./configure命令

接着在nginx目录下执行 make命令

执行make install命令

4.启动nginx

在nginx-1.8.0文件夹的上一层目录 nginx中找到sbin文件夹 进入文件夹

执行 ./nginx  启动nginx

5 .开放linux的80端口

#/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
保存:
#/etc/rc.d/init.d/iptables save

重新加载防火墙 /etc/init.d/iptables restart

6 .在浏览器输入 linux ip地址 测试nginx是否启动成功

7.配置tomcat集群

将tomcat复制多份后修改server.xml文件


8.开放8081 端口

#/sbin/iptables -I INPUT -p tcp --dport 8081 -j ACCEPT
保存:
#/etc/rc.d/init.d/iptables save

重新加载防火墙 /etc/init.d/iptables restart

9.配置nginx的nginx-1.8.0\conf\nginx.conf这个文件修改配置:
upstream serverlb{
	server 127.0.0.1:8080;
	server 127.0.0.1:8081;
	}
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
			proxy_pass http://serverlb;
            index  index.html index.htm;
        }
10.重新加载nginx配置文件 在nginx文件目录下

./nginx -s reload
可以不关闭nginx的情况下更新配置文件。
11.关闭nginx

关闭命令:相当于找到nginx进程kill。
./nginx -s stop


退出命令:
./nginx -s quit
12.使用redis实现session共享

在tomcat的lib中加入需要的jar包

tomcat7所需要的jar包 http://download.csdn.net/download/qq_26483671/10239792

13.配置

更改每个tomcat/conf下context.xml文件【context】节点下加如下代码: 
         配置redis的链接信息,如果没有密码可以把passowrd项去掉 
   tomcat8的配置代码

<Valve className="com.demo.redis_session.RedisSessionHandlerValve" />
        <Manager className="com.demo.redis_session.RedisSessionManager"
         host="127.0.0.1"
         port="6379"
         database="0"
         password="123456"
         maxInactiveInterval="60" />
tomcat7的配置代码
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
        <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
         host="127.0.0.1"   
         port="6379"   
         database="0"  
         password="123456" 
         maxInactiveInterval="60" /> 

14.重启tomcat
进入到tomcat目录下 cd /usr/local/apache-tomcat-7.0.57/bin
启动tomcat  ./startup.sh
15部署windows下相同页面 进行测试



发布了28 篇原创文章 · 获赞 13 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_26483671/article/details/79256077