Nginx和tomcat的融合,其实就是对jsp解析的转发,当用户请求解析jsp文件时,Nginx把相应的jsp文件转发给8080端口解析,这样就实现了在80端口对jsp文件进行解析,其实还是tomcat在工作,仅仅是Nginx做了个转发功能.核心Nginx.conf代码如下,在里面添加这段代码就可以了
server{
listen 80;
server_name localhost;
index index.html index.htm index.jsp;#设定访问的默认首页地址
root /home/www/web/ROOT;#设定网站的资源存放路径
#limit_conn crawler 20;
listen 80;
server_name localhost;
index index.html index.htm index.jsp;#设定访问的默认首页地址
root /home/www/web/ROOT;#设定网站的资源存放路径
#limit_conn crawler 20;
下面是需要添加的代码
location ~ .*.jsp$ #所有jsp的页面均交由tomcat处理
{
index index.jsp;
proxy_pass http://localhost:8080;#转向tomcat处理
}
{
index index.jsp;
proxy_pass http://localhost:8080;#转向tomcat处理
}
验证配置文件是否正确:
# /usr/local/nginx/sbin/nginx –t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
出现这个提示说明没有问题。
启动nginx,停止nginx
# /usr/local/nginx/sbin/nginx
查看Nginx主进程号
ps -ef | grep “nginx: master process” | grep -v “grep” | awk -F ‘ ‘ ‘{print $2}’
显示进程号
kill -HUP 6302 //杀掉主进程
停止nginx的命令
# /usr/local/nginx/sbin/nginx -s stop 或者
kill -HUP cat /usr/local/nginx/nginx.pid`
nginx启动好后启动tomcat,此时输入http://主机ip地址即可看到网站,默认nginx是80,tomcat是8080,但是现在通过80可以访问静态和动态页面。