nginx与tomcat(二) 服务器部署Nginx+tomcat
一. windows服务器安装Nginx
-
下载稳定版本的nginx“http://nginx.org/en/download.html”
- 解压放在e盘下
-
开启nginx
- 双击exe文件
- 命令行开启
- 双击exe文件
-
访问localhost测试
-
关闭nginx
-
常见问题:
- 如果端口被占用,则更改nginx解压目录下的nginx.conf
二. linux服务器安装Nginx
-
下载Nginx安装包和依赖包“http://nginx.org/en/download.html”
-
Nginx依赖包下载地址:
- gzip模块需要zlib库(在http://www.zlib.net/下载http://zlib.net/zlib-1.2.8.tar.gz)
- rewrite模块需要pcre库(在http://www.pcre.org/下载ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/)
- ssl功能需要openssl库(在http://www.openssl.org/下载http://www.openssl.org/source/openssl-fips-2.0.9.tar.gz)
-
命令安装其他前期准备类库
- yum install perl
- yum install gcc
- yum install gcc-c++
- yum -y install net-tools
-
将安装文件放到home下新建一个文件夹aaa
- 安装opensssl
- #解压安装文件
- tar -zxvf openssl-fips-2.0.9.tar.gz
- cd openssl-fips-2.0.9
- #prefix配置安装路径
- ./config --prefix=/opt/openssl-fips-2.0.9
- make
- make install
- 安装zlib
- #解压安装文件
- tar -zxvf zlib-1.2.8.tar.gz
- cd zlib-1.2.8
- #prefix配置安装路径
- ./configure --prefix=/opt/zlib-1.2.8
- make
- make install
- 安装pcre
- #解压安装文件
- tar -zxvf pcre-8.37.tar.gz
- cd pcre-8.37
- #prefix配置安装路径
- ./configure --prefix=/opt/pcre-8.37
- make
- make install
- 安装nginx
- #解压安装文件
- tar -zxvf nginx-1.8.0.tar.gz
- cd nginx-1.8.0
- #with-pcre指定依赖包位置,prefix配置安装路径
- ./configure --with-pcre=../pcre-8.37 --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-fips-2.0.9 --prefix=/opt/nginx-1.8.0
- make
- make install
- 安装opensssl
-
检测:
- 运行:"./nginx-t"出现下图,则成功
三. 配置访问tomcat
-
更改nginx.conf配置文件
- ,在server上面添加"upstream 自定义名称{server ip:端口号}"。在server中更改location的配置,添加proxy_pass
-
刷新资源
- 然后访问localhost直接就跳转到对应的url了
四. nginx特点的应用
-
反向代理
- 上面的例子就是反向代理
-
静态资源与动态资源分离
- 当匹配jsp的时候转发给tomcat,静态资源直接访问文件夹下的文件
-
负载均衡+反向代理
- 在upstream tomcat1中配置两个server并且定义权重(weight),数值越大,访问几率越高