Nginx+Tomcat搭建

Nginx简介

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的Rambler.ru 站点开发的,它已经在该站点运行超过四年多了。Igor 将源代码以类BSD许可证的形式发布。自Nginx 发布四年来,Nginx 已经因为它的稳定性、丰富的功能集、 示例配置文件和低系统资源的消耗而闻名了。目前国内各大门户网站已经部署了Nginx,如新浪、网易、腾讯等;国内几个重要的视频分享网站也部署了Nginx,如六房间、酷6等。 新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx。

前置准备

可能要用到的包、工具

1.yum –y install lrzsz   //上传下载工具,高大上的工具,再次鄙视那些用ftp工具的

2.yum –y install gcc gcc-c++  //两个包gcc和gcc-c++  为了pcre的编译

3.yum –y install openssl*   //ssl的支持

4.yum install pcre*

5.yum install zlib 

6.yum install zlib-devel

7. yum install wget

 

要是编译报错看错误提示,有的可能是少包的支持,没啥yum啥

 

关闭防火墙

首先把防火墙关闭:service iptables stop

再设置为永久关闭:chkconfig iptables off

一定要关,不然本地可能访问不了虚拟机上的apache

 

JDK安装

详情见:http://blog.csdn.net/daisyliu01/article/details/76474171

Tomcat安装

详情见:http://blog.csdn.net/daisyliu01/article/details/75581767

Nginx安装

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:

 

cd/usr/local/

下载nginx安装包

wget http://nginx.org/download/nginx-1.9.3.tar.gz

创建安装目录
 mkdir /usr/local/nginx
解压 nginx

tar -zxvf  nginx-1.9.3.tar.gz

进入目录

cd  nginx-1.9.3  

配置。通常将软件安装在/usr/local/目录下。

./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre-8.21--with-zlib=/usr/local/zlib-1.2.8 --with-http_stub_status_module--with-openssl=/usr/local/openssl-1.0.1c --with-http_ssl_module

编译

make

安装

make  install

 

--with-pcre=/usr/src/pcre-8.21 指的是pcre-8.21 的源码路径。

--with-zlib=/usr/src/zlib-1.2.8 指的是zlib-1.2.8 的源码路径。

--with-openssl=/usr/local/openssl-1.0.1c指的是openssl-1.0.1c的源码路径。


检查是否安装成功
# cd /usr/local/nginx/sbin
# ./nginx -t
结果显示:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动

确保系统的 80 端口没被其他程序占用,

/usr/local/nginx/sbin/nginx

 

检查是否启动成功:

netstat-ano|grep 80 有结果输入说明启动成功


IE输入IP:端口,出现如下表示安装nginx成功

Nginx+Tomcat搭建_第1张图片


修改配置文件

cd/usr/local/nginx/conf

vinginx.conf

 

常用配置介绍

#nginx运行用户和组

user    wwwwww;  

#启动进程,通常设置成和cpu的数量相等

worker_processes  4;

 

#全局错误日志及PID文件

pid/var/run/nginx.pid;

error_log /var/log/nginx/error.log;

 

events {

        #epoll是多路复用IO(I/OMultiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能

use epoll;

                   #单个后台workerprocess进程的最大并发链接数

        worker_connections  10240;

}

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

        include       mime.types;

 

        default_type  application/octet-stream;

 

         error_page400 403 500 502 503 504  /50x.html;

 

        indexindex.html index.shtml

 

        autoindexoff;

 

         fastcgi_intercept_errorson;

 

        sendfile        on;

 

        # Theseare good default values.

        tcp_nopush      on;

        tcp_nodelay     off;

 

        # outputcompression saves bandwidth

        gzip  off;

         #gzip_staticon;

        #gzip_min_length  1k;

        gzip_http_version1.0;

        gzip_comp_level2;

        gzip_buffers  4 16k;

        gzip_proxiedany;

        gzip_disable"MSIE [1-6]\.";

        gzip_types  text/plaintext/html text/css application/x-javascript application/xml application/xml+rsstext/javascript;

        #gzip_varyon;

 

        server_name_in_redirectoff;

 

#设定负载均衡的服务器列表

        upstreamportals {

                  server172.16.68.134:8082 max_fails=2 fail_timeout=30s;

                  server172.16.68.135:8082 max_fails=2 fail_timeout=30s;

                  server172.16.68.136:8082 max_fails=2 fail_timeout=30s;

                  server172.16.68.137:8082 max_fails=2 fail_timeout=30s;

        }

 

        #upstreamoverflow {

         #       server10.248.6.34:8090 max_fails=2fail_timeout=30s;       

         #       server10.248.6.45:8080 max_fails=2fail_timeout=30s;       

        #}

 

        server {

                                     #侦听8080端口

                listen       8080;

                server_name  127.0.0.1;

 

                   #403、404页面重定向地址

                   error_page  403 =http://www.e100.cn/ebiz/other/217/403.html;

                   error_page  404 =http://www.e100.cn/ebiz/other/218/404.html;

                   proxy_connect_timeout      90;

                   proxy_send_timeout         180;

                   proxy_read_timeout         180;

 

                   proxy_buffer_size64k;

                   proxy_buffers4 128k;

                   proxy_busy_buffers_size128k;

 

 

                   client_header_buffer_size16k;

                   large_client_header_buffers4 64k;

 

                #proxy_send_timeout         3m;

                #proxy_read_timeout         3m;

                #proxy_buffer_size          4k;

                #proxy_buffers              4 32k;

 

                proxy_set_headerHost $http_host;

                proxy_max_temp_file_size0;

                #proxy_hide_headerSet-Cookie;

                  

         #       if ($host!= 'www.e100.cn' ) {

         #                rewrite^/(.*)$ http://www.e100.cn/$1 permanent;

         #       }

 

 

               location /{

                       deny all;

               }

 

                   location ~^/resource/res/img/blue/space.gif {

                    proxy_passhttp://tecopera;

               }

 

               location =/ {

                   rewrite^(.*)$  /ebiz/event/517.html last;

               }

 

 

 

                   location =/ebiz/event/517.html {

                    add_headerVary Accept-Encoding;

                    root /data/web/html;

                    expires10m;

               }

 

 

 

 

               location =/check.html {

                    root/usr/local/nginx/html/;

                    access_logoff;

               }

 

               location =/50x.html {

                    root/usr/local/nginx/html/;

                    expires1m;

                    access_logoff;

               }

 

 

              location =/index.html {

                       add_headerVary Accept-Encoding;

#定义服务器的默认网站根目录位置

                    root/data/web/html/ebiz;

                    expires10m;

               }

#定义反向代理访问名称

                   location ~^/ecps-portal/* {

                   # expires10m;

#重定向集群名称

                    proxy_passhttp://portals;

                    #proxy_passhttp://172.16.68.134:8082;

               }

 

                   location ~^/fetionLogin/* {

                   # expires10m;

                    proxy_passhttp://portals;

                    #proxy_passhttp://172.16.68.134:8082;

                }

 

                   #location  ~^/business/*{                                                                      

                #   # expires10m;                                                                                

                #    proxy_passhttp://172.16.68.132:8088;                                                                   

                #    #proxy_passhttp://172.16.68.134:8082;                                                       

                #}

 

                   location ~^/rsmanager/* {

                    expires10m;

                    root/data/web/;

                    #proxy_passhttp://rsm;

               }

#定义nginx处理的页面后缀

                   location~* (.*)\.(jpg|gif|htm|html|png|js|css)$  {

                            root/data/web/html/;

#页面缓存时间为10分钟

                         expires10m;

                   }

 

#设定查看Nginx状态的地址     

               location~* ^/NginxStatus/ {

                    stub_statuson;

                    access_logoff;

                    allow10.1.252.126;

                    allow10.248.6.49;

                    allow127.0.0.1;

                    deny all;

               }

         #       error_page   405 =200@405;

         #       location@405

         #       {

         #                proxy_passhttp://10.248.6.45:8080;

         #       }  

 

               access_log  /data/logs/nginx/access.logcombined;

               error_log   /data/logs/nginx/error.log;

        }

         server {

                listen       8082;

 

                server_name  _;

               location =/check.html {

                    root/usr/local/nginx/html/;

                    access_logoff;

               }

                  

        }

         server {

                   listen       8088;

                   server_name  _;

                   location ~^/* {

                   root/data/web/b2bhtml/;

                   access_logoff;

         }                

         }

        server {

                listen       9082;

                server_name  _;

 

        #        location ~^/resource/* {

        #            expires10m;

         #           root/data/web/html/;

         #       }

 

                location  / {

                     root/data/web/html/sysMaintain/;

                       if (!-f$request_filename) {

                            rewrite^/(.*)$ /sysMaintain.html last;

                           }

                }

        }

 

}

******   ******   ******  ******   ******   ******  ******   ******   ******  ******  

10.Nginx负载均衡的相关配置代码如下:

upstream mytomcats

      server 192.168.12.132:8080; 

      server 192.168.12.133:8080; 

      server 192.168.12.134:8080; 

 

server{ 

      listen 80; 

      server_name www.ixdba.net; 

location~* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ { 

      root /web/www/html/; 

}  

 

location /{ 

    root  html;

          index index.html index.htm;

          proxy_pass http://mytomcats

          proxy_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; 

          client_max_body_size 10m; 

          client_body_buffer_size 128k; 

          proxy_connect_timeout 90; 

          proxy_send_timeout 90; 

          proxy_read_timeout 90; 

          proxy_buffer_size 4k; 

          proxy_buffers 4 32k; 

          proxy_busy_buffers_size 64k; 

          proxy_temp_file_write_size 64k;

}

}



测试负载

在两个tomcat下加入工程test

mkdir /usr/local/tomcat1/webapps/test

mkdir /usr/local/tomcat2/webapps/test

在两个test工程下分别创建test.jsp,内容如下

<%@ pagecontentType="text/html;charset=UTF-8"%>

Tomcat_test1

<% out.print( "test111" );%>

 

<%@ pagecontentType="text/html;charset=UTF-8"%>

Tomcat_test2

<% out.print( "test222" );%>

 

启动tomcat+nginx

启动nginx:/usr/local/nginx/sbin/nginx

启动tomcat:

/usr/local/tomcat1/bin/startup.sh

/usr/local/tomcat2/bin/startup.sh

 

查看进程

在两个IE中输入ip:nginx端口/工程/jsp页面,最好是不同浏览器,出现如下表示搭建成功


Nginx+Tomcat搭建_第2张图片

在两个IE中输入ip:nginx端口/工程/jsp页面,最好是不同浏览器,出现如下表示搭建成功





你可能感兴趣的:(Linux)