nginx + tomcat的简单部署

本文介绍了Nginx作为高性能HTTP和反向代理服务器的优势,如轻量级、抗并发能力强,并通过对比测试展示了在不同并发情况下与Apache的性能差异。接着详细讲解了Nginx的部署过程,包括环境准备、编译安装、配置文件详解,以及如何配置反向代理以实现与Tomcat的协同工作,为H5网站提供稳定服务。
摘要由CSDN通过智能技术生成

                NGINX 的介绍及部署配置

   一、Nginx 是一款高性能的HTTP 和反向代理服务器,其相比于apache 服务器具有诸多优点。

1. 轻量级,同样起web 服务,比apache 占用更少的内存及资源 。

2. 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能 。

3.高负载能力,nginx处理静态文件好,耗费内存少,Nginx 静态处理性能比 Apache 高 3倍以上 。

4. Nginx 可以作为一个反向代理服务器,外网客户端反向代理服务器的攻击并不会使得网页信息遭到破坏,增强了Web服务器的安全性。

   针对***** 网站我们使用apacheworkbanch进行如下测试:

并发1,请求1000:

Nginx

Apache

nginx说明

总耗时

51.732 s

52.338 s

耗时短

失败请求

0

0

 

吞吐率

692.04 Kb/s

684.13 Kb/s

吞吐率高

平均耗时(99%)

234 ms

172 ms

不稳定

 

并发100,请求1000:

Nginx

Apache

nginx说明

总耗时

54.414 s

55.339 s

耗时短

失败请求

0

8

       无失败

吞吐率

682.51 Kb/s

666.39 Kb/s

吞吐率高

平均耗时(99%)

29142 ms

29928

稳定

 

 

并发500,请求1000:

Nginx

Apache

nginx说明

总耗时

65.665 s

77.895 s

耗时短

失败请求

0

3

无失败

吞吐率

664.77 Kb/s

532.16 Kb/s

吞吐率高

平均耗时(99%)

40507ms

57365

稳定

 

 

并发1000,请求1000:

Nginx

Apache(曾崩溃)

nginx说明

总耗时

64.873 s

58.216 s

耗时长

失败请求

126

107

失败多

吞吐率

516.69Kb/s

656.26 Kb/s

吞吐率低

平均耗时(99%)

64623ms

42136

不稳定

 

并发2000,请求2000:

Nginx

Apache(崩溃)

nginx说明

总耗时

159.222 s

      

 

失败请求

934

1125(完成875

     

吞吐率

397.66 Kb/s

 

 

平均耗时(99%)

158749

 

 

 

并发100,请求2000:

Nginx

Apache

nginx说明

总耗时

107.602

107.487 s

耗时长

失败请求

0

5

       无失败

吞吐率

680.49 Kb/s

680.27 Kb/s

吞吐率稍低

平均耗时(99%)

31712

30890

不稳定

 

并发100,请求5000:

Nginx

Apache

nginx说明

总耗时

265.685s

265.217 s

耗时长

失败请求

14

22

      失败少

吞吐率

678.81 Kb/s

679.47 Kb/s

吞吐率略高

平均耗时(99%)

42964

43496

稳定

 

 

根据测试数据我们可以总结出:

当请求数与并发数较少时,nginx处理能力略高于apache

当请求数过多则与apache服务器处理能力相近,但nginx能尽量保证请求不被丢失。

   当并发数持续增大至1000时,nginx性能略有降低,处理能力偏弱于appche,但仍能保证服务器不崩溃。

   当并发持续增至2000,服务器仍能正常不至于崩溃。

   (:由于并发数过大时,云服务器经常会主动断开测试请求,故此次不继续深入测试nginx服务器能承受的并发压力)

 

   二、Nginx(1.10.2)部署文档如下:

   环境准备:

   Centos 6.5

   Nginx-1.10.2.tar.gz

   可使用下载:

wget -c https://nginx.org/download/nginx-1.10.2.tar.gz

  

1.安装gcc编译器

yuminstall gcc-c++ 

 

2.安装pcrenginxhttp模块使用pcre来解析正则表达式。

yuminstall -y pcre pcre-devel

 

3.安装zlibnginx使用zlibhttp包的内容进行gzip

yuminstall -y zlib zlib-devel

 

4.安装openssl 。使nginx不仅支持http协议,还支持https

yuminstall -y openssl openssl-devel

 

5.解压nginx-1.10.2.tar.gz到一个空闲的目录,目前解压于(/mnt/)

tar-zxvf  nginx-1.10.2.tar.gz   /mnt/

6.建立日志文件目录

mkdir /mnt/nginxLogs/

 

 

7.cd到解压的nginx目录下,执行配置文件

cd  /mnt/nginx-1.10.2/

./configure \

--prefix=/usr/local/nginx \

--error-log-path=/mnt/nginxLogs/error.log \

--http-log-path=/mnt/nginxLogs/access.log \

--pid-path=/mnt/nginxLogs/nginx.pid \

--with-http_gzip_static_module \

--with-http_ssl_module \

--with-http_stub_status_module \

--with-pcre

(以上是对nginx的初步配置,根据需要可以进行不同的配置

--prefix=/usr/local/nginx  安装目录

--error-log-path=/mnt/nginxLogs/error.log 错误日志

--http-log-path=/mnt/nginxLogs/access.log  访问日志

--pid-path=/mnt/nginxLogs/nginx.pid 记录运行nginx master进程pid

--with-http_gzip_static_module   添加静态压缩模块,需在配置文件中开启

--with-http_ssl_module   添加安全传输协议模块(https) ,需在配置文件中开启

--with-http_stub_status_module  添加nginx状态监控模块,需在配置文件中开启

--with-pcre   添加路径正则解析,自动检测

)

 

8.编译

make

 

9.安装

make install

 

10.启动

cd /usr/local/nginx/sbin/

   ./nginx               // 启动

    ./nginx-s stop       // 停止

./nginx -s reload     // 重新读取配置

(nginx 默认监听80端口,若80端被占用,则无法启动,故我们需要在配置文件中对nginx服务器进行配置)

 

 

三、nginx配置文档如下:

1.vim 打开配置文件:

vim /usr/local/nginx/conf/nginx.conf  (配置文件中以#开头表明为注释)

以下为配置文本中的内容:

#this  is  visit user, default is  nobody and  noted,

#but if  we usenobody  in centos,

#static  resource   could not  visit 

#because  of  we have  permission denied ,even  the chmod  is  755

#配置文件中默认的用户为nobody,同时是屏蔽着的.但是服务器要访问静态资源文件,#必须对用户进行提权操作,此处用户设置为root用户。并访问目录chmod755

user  root;

#对外提供web服务时的worker进程数。最优值取决于许多因素,包括但不限于CPU

#核的数量、存储数据的硬盘数量及负载模式。当服务器IO能力不足时,可设置为可用#的CPU内核数/设置为“auto”

worker_processes  1;

 

#配置错误日志,及错误等级

#error_log logs/error.log;

#error_log logs/error.log  notice;

#error_log logs/error.log  info;

 

#配置master  pid 文件位置

#pid       logs/nginx.pid;

 

# events模块中包含nginx中所有处理连接的设置

events {

#设置可由一个worker进程同时打开的最大连接数。如果设置worker_rlimit_nofile,

#我们可将这个值设得很高(不推荐);也由系统的可用socket连接数限制(~ 64K)。

   worker_connections  1024;

}

 

# HTTP模块控制着nginx http处理的所有核心特性

http {

    #加载mime.types中的指令

    include       mime.types;

 

    #设置http 默认请求类型,使用mime.types中的值

    default_type   application/octet-stream;

   

    #配置日志格式

    #log_format  main '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent"$http_referer" '

    #                  '"$http_user_agent""$http_x_forwarded_for"';

 

    #配置日志输出位置

#access_log logs/access.log  main;

   

    #在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)

    sendfile        on;

 

    #在一个数据包里发送所有头文件,而不一个接一个的发送

    #tcp_nopush     on;

   

#客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接。

#我们将它设置低些可以让ngnix持续工作的时间更长,但是大文件上传时可能会#不成功(超时)。

   #keepalive_timeout  0;

   keepalive_timeout  65;

    

    #采用gzip压缩的形式发送数据。这将会减少我们发送的数据量

    #gzip  on;

 

    #here isconfigure  proxy routing,each routingcould have many back address

    #集群配置,8080的集群配置

    #可配置多个server,并分配不同的权重

       # ip_hash; 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个

#后端服务器,可以解决session的问题。

    upstream   cluster8080{

       #this is theproxy server address  of  port 81 in   120.25.226.96

       #修改tomcat服务器的端口为81,以至于nginx服务器能运行在8080端口上

       #同时81端口可禁用外网访问

       server  120.25.226.96:81;

    } 

   

    #集群配置,8090的集群配置,暂时没开启

    upstream  cluster8090{

       #this isthe  proxy  server address  of port 82  in  120.25.226.96

#修改tomcat服务器的端口为82,以至于nginx服务器能运行在8090端口上

       #同时82端口可禁用外网访问

       server  120.25.226.96:82;

    }

 

    #this  is the  reverse  server configure

    #反向代理服务器配置,可配置多个服务器

    server {

        #监听8080端口,该端口不能被其他程序占用

        listen       8080;  #the  server  is listening  port 81

        #server

       server_name  cluster8080;  # proxy server  has been  configured

        #配置编码

        #charsetkoi8-r;

 

        #配置访问日志

#access_log logs/host.access.log  main;

       

        #when we  visit the  epsot background manage  server

        #the root  path will  tirm  to login.jsp

        #if don't  have this the  path  may point to index.jsp

        #匹配后台管理系统根路径

        #由于后台管理系统使用的jsp页面,故要单独配置默认访问路径

        location  ^~ /***/ {

           #设置访问根目录

           root/home/Epsot_96/Public/tomcat-7.0.67-Dev/webapps;

           #设置默认访问

index login.jsp;

#设置主机地址为用户访问的地址

#(若不配置request中存在的nginx访问地址)

          proxy_set_header Host $host:$server_port;

           #转发用户IP

          proxy_set_header X-Real-IP $remote_addr;

           #增加请求地址到转发中

          proxy_set_header x-Forwarded-For $proxy_add_x_forwarded_for;

           #设置用户请求协议

          proxy_set_header x-Forwarded-Proto $scheme; #getScheme,isSecure,sendRedirect

           #代理服务器地址

           proxy_passhttp://cluster8080;  #this is theproxy  path 

        }

       

 

        #when we  visit a jsp  page

        #we shouldconfigure the proxy  params

        #so that  the tomcat  server  can get  the right  request object

        #匹配jsp文件,配置同上

        location  ~ \.jsp$ {      

           root/home/Epsot_96/Public/tomcat-7.0.67-Dev/webapps;

           proxy_set_header Host $host:$server_port;

          proxy_set_header X-Real-IP $remote_addr;

          proxy_set_header x-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_set_header x-Forwarded-Proto $scheme; #getScheme,isSecure,sendRedirect

           proxy_passhttp://cluster8080;  #this is theproxy  path 

        }

 

 

 

           #we need to  configure a location  to match  the  static resource #that  #users upload

        #the  request path may format at  /epsot/source/

        #访问8080服务器静态资源路径。

        location  ^~ /static/source/  {

           #替换掉虚拟目录,设置为真是物理路径

           alias  /mnt/source/;

           #尝试访问服务器静态资源文件,若失败交由@tornado路径处理

          try_files  $uri  @tornado;

        }

 

         #first we  match the  static  resource to  dealed  by absolute path

        #匹配静态资源文件,包括html,js,css,图片文件等

        location  ~ \.(html|js|css|jpg|jpeg|ico|png|gif)$ {

           #访问静态文件的路径

           root/home/Epsot_96/Public/tomcat-7.0.67-Dev/webapps;

           #尝试访问服务器静态资源文件,若失败交由@tornado路径处理

          try_files  $uri  @tornado;

        }

       

        #thislocation  is to deal  the resource in our server

        #静态资源失败处理代理服务器路径

        location  @tornado{

           proxy_passhttp://cluster8080;

        }       

       

        #其他未匹配到路径处理

        location  / {

           #设置访问根目录

           root  html;

           #首页设置

           index  login.html index.html  index.htm;

           #代理服务器设置

           proxy_passhttp://cluster8080;  #this is theproxy  path 

        }

       

#错误页

       #error_page  404              /404.html;

 

        # redirectserver error pages to the static page /50x.html

        #远程服务器出错返回 /50x.html

        error_page   500 502 503 504  /50x.html;

 

 

        #服务器错误放回页面

location = /50x.html {

            root   html;

        }

 

 

    }

 

 

 

    #this  is the  second  reverse server  configure

    #both  of server  could  run at same time

    #but  they are  matched the  two different  enviroment,

    #the second  reverse server  is  matched bate  enviroment.

    #一个http模块中可配置多个服务器,监听不同的端口。并转发给不同的代理服务#器,此处可以配置8090端口服务器的反向代理服务器。配置基本类似,主要替 #换代理服务器线路与物理访问地址

    #server{

    #    listen  8090;

    #    server_name cluster8090;

 

    #   location ~ \.jsp$  {        

    #      proxy_pass  http://cluster8090;

    #   }

 

    #   location ~ \.(html|js|css|jpg|jpeg|ico|png|gif)$ {

    #       root /home/Epsot_96/Public/tomcat-7.0.67-Beta/webapps;

    #    }

       

 

    #    location / {

    #       root html;

    #       index login.html index.html   index.htm;

    #       proxy_pass http://cluster8090;

    #    }

 

    #    error_page 500 502  503 504  /50x.html;

    #    location = /50x.html{

    #       root html;

    #    }

    # }

 

 

 

 

    # HTTPS server

    #此处配置https协议服务器内容

    #server {

    #    listen      443 ssl;

    #    server_name localhost;

 

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_cache    shared:SSL:1m;

    #    ssl_session_timeout  5m;

 

    #    ssl_ciphers HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers  on;

 

    #    location / {

    #        root  html;

    #        index index.html index.htm;

    #    }

    #}

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值