一、windows的Nginx搭建

1、配置多个tomcat

这里一两个为例。

nginx origin怎么加 nginx怎么配_nginx origin怎么加

2、修改tomcat的配置文件,将端口进行修改

一共三个端口。


nginx origin怎么加 nginx怎么配_解决方案_02


nginx origin怎么加 nginx怎么配_解决方案_03


nginx origin怎么加 nginx怎么配_解决方案_04


这里可能会出现一个坑,无法正常启动多个tomcat,这里可以参考一下以下链接

可能是启动参数的问题

https://jingyan.baidu.com/article/4f7d5712eb23221a21192757.html

3、安装windows版的nginx

下载链接如下
http://nginx.org/en/download.html

解压nginx,最后目录结构不要有英文

nginx origin怎么加 nginx怎么配_tomcat_05


访问http://localhost:80 nigix默认的端口就是80,访问出现下面的内容表示开启nginx成功

nginx origin怎么加 nginx怎么配_解决方案_06

注意:这里可能还有一个坑,nginx启动可能会闪退

解决方案一:修改nginx/conf目录下的nginx.conf文件,将图中标注的配置改为其他端口。

nginx origin怎么加 nginx怎么配_tomcat_07


解决方案二:参考以下链接修改80端口的系统程序运行端口


4、配置集群

配置nginx/conf/nginx.conf文件

nginx origin怎么加 nginx怎么配_Nginx_08


然后通过任务管理器关闭nginx,再启动nginx

nginx origin怎么加 nginx怎么配_nginx origin怎么加_09


通过浏览器访问http://localhost/项目名,会自动切换访问网站所属的tomcat

5、Tomcat集群的session共享

解决方案一:一种使用tomcat广播机制完成session的共享(不推荐的方式)

修改多个个tomcat中的server.xml:把Cluster的注释打开

nginx origin怎么加 nginx怎么配_Nginx_10


在项目中web.xml中添加一个配置,如下图:

nginx origin怎么加 nginx怎么配_解决方案_11


重启所有tomcat即可解决

解决方案二:一种使用redis服务器的方式完成session的共享(推荐的方式),这里后期在博客中再具体讲解。

二、Linux的Nginx搭建

1、环境安装

nginx是C语言开发,建议在linux上运行,本教程使用Centos6.4作为安装环境

gcc
安装Nginx需要将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,就需要安装gcc。

yum install gcc -c++

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

yum install -y pcre -devel

注:pcre-devel是使用pcre开发的一个二次开发库。Nginx也需要这个库。

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

yum install -y zlib zlib-devel

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

yum install -y openssl openssl-devel

2、编译并安装

第一步:把nginx的源码包上传至linux服务器
第二步:解压源码包。 tar -zxf nginx-1.8.0.tar.gz -C /var/temp/nginx
第三步:进入nginx-1.8.0文件夹。使用configure命令创建makefile。

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

第五步:make

第六步 :make install

nginx origin怎么加 nginx怎么配_解决方案_12