一、简介:

Tomcat在高并发环境下处理动态请求时性能很低,而在处理静态页面更加脆弱。虽然Tomcat的最新版本支持epoll,但是通过Nginx来处理静态页面要比通过Tomcat处理在性能方面好很多。

二、下载安装:

下载nginx

http://nginx.org/en/download.html

下载解压后放到C:\nginx-1.0.4(官网这样要求的,不知道放其它盘有没有问题)

启动nginx.exe,然后在浏览器输入127.0.0.1即可

配置自己的项目测试

第二环节我们使用了默认的nginx.conf 。Nginx的配置文件都存于目录conf文件下,其中nginx.conf是它的主配置文件。

以下为我加上注释并配置的新的虚拟server]

  1. #运行用户
  2. #user  nobody;
  3. #开启进程数 <=CPU数
  4. worker_processes  1;
  5. #错误日志保存位置
  6. #error_log  logs/error.log;
  7. #error_log  logs/error.log  notice;
  8. #error_log  logs/error.log  info;
  9. #进程号保存文件
  10. #pid        logs/nginx.pid;
  11. #等待事件
  12. events {
  13. #Linux下打开提高性能
  14. #use epoll;
  15. #每个进程最大连接数(最大连接=连接数x进程数)
  16. worker_connections  1024;
  17. }
  18. http {
  19. #文件扩展名与文件类型映射表
  20. include       mime.types;
  21. #默认文件类型
  22. default_type  application/octet-stream;
  23. #日志文件输出格式 这个位置相于全局设置
  24. #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  25. #                  '$status $body_bytes_sent "$http_referer" '
  26. #                  '"$http_user_agent" "$http_x_forwarded_for"';
  27. #请求日志保存位置
  28. #access_log  logs/access.log  main;
  29. #设定请求缓冲
  30. client_header_buffer_size 1k;
  31. large_client_header_buffers 4 4k;
  32. #打开发送文件
  33. sendfile        on;
  34. #tcp_nopush     on;
  35. #keepalive_timeout  0;
  36. keepalive_timeout  65;
  37. #客户端上传文件大小控制
  38. client_max_body_size 8m;
  39. #打开gzip压缩
  40. #gzip  on;
  41. #设定负载均衡的服务器列表
  42. #upstream mysvr {
  43. #    #weigth参数表示权值,权值越高被分配到的几率越大
  44. #    #本机上的Squid开启3128端口
  45. #    #server 192.168.8.1:3128 weight=5;
  46. #    #server 192.168.8.2:80 weight=1;
  47. #    #server 192.168.8.3:80 weight=6;
  48. #}
  49. #第一个虚拟主机
  50. server {
  51. #监听IP端口
  52. listen       80;
  53. #主机名
  54. server_name  localhost;
  55. #root
  56. #设置字符集
  57. #charset koi8-r;
  58. #本虚拟server的访问日志 相当于局部变量
  59. #access_log  logs/host.access.log  main;
  60. #日志文件输出格式
  61. #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  62. #                  '$status $body_bytes_sent "$http_referer" '
  63. #                  '"$http_user_agent" "$http_x_forwarded_for"';
  64. location / {
  65. root   html;
  66. index  index.html index.htm;
  67. }
  68. #静态文件缓存时间设置
  69. #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${
  70. #    expires 30d;
  71. #}
  72. #静态文件缓存时间设置
  73. #location ~ .*\.(js|css)?${
  74. #    expires 1h;
  75. #}
  76. #对本server"/"启用负载均衡
  77. #location / {
  78. #    proxy_pass http://mysvr;
  79. #    proxy_redirect off;
  80. #    proxy_set_header Host $host;
  81. #    proxy_set_header X-Real-IP $remote_addr;
  82. #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  83. #    client_max_body_size 10m;
  84. #    client_body_buffer_size 128k;
  85. #    proxy_connect_timeout 90;
  86. #    proxy_send_timeout 90;
  87. #    proxy_read_timeout 90;
  88. #    proxy_buffer_size 4k;
  89. #    proxy_buffers 4 32k;
  90. #    proxy_busy_buffers_size 64k;
  91. #    proxy_temp_file_write_size 64k;
  92. #}
  93. #设定查看Nginx状态的地址
  94. #location /NginxStatus {
  95. #    stub_status on;
  96. #    access_log on;
  97. #    auth_basic “NginxStatus”;
  98. #    auth_basic_user_file conf/htpasswd;
  99. #}
  100. #error_page  404              /404.html;
  101. # redirect server error pages to the static page /50x.html
  102. #
  103. error_page   500 502 503 504  /50x.html;
  104. location = /50x.html {
  105. root   html;
  106. }
  107. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  108. #
  109. #location ~ \.php$ {
  110. #    proxy_pass   http://127.0.0.1;
  111. #}
  112. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  113. #
  114. #location ~ \.php$ {
  115. #    root           html;
  116. #    fastcgi_pass   127.0.0.1:9000;
  117. #    fastcgi_index  index.php;
  118. #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  119. #    include        fastcgi_params;
  120. #}
  121. # deny access to .htaccess files, if Apache's document root
  122. # concurs with nginx's one
  123. #
  124. #location ~ /\.ht {
  125. #    deny  all;
  126. #}
  127. }
  128. # another virtual host using mix of IP-, name-, and port-based configuration
  129. server {
  130. #多监听
  131. listen       localhost:8666;
  132. #主机名
  133. server_name  LIULJ2576;
  134. #WEB文件路径
  135. root         E:/Portal;
  136. #默认首页
  137. index        HomePage.html;
  138. #location / {
  139. #    #这里相当于局部变量
  140. #    root   E:/Portal;
  141. #    index  HomePage.html;
  142. #}
  143. }
  144. # HTTPS server HTTPS SSL加密服务器
  145. #
  146. #server {
  147. #    listen       443;
  148. #    server_name  localhost;
  149. #    ssl                  on;
  150. #    ssl_certificate      cert.pem;
  151. #    ssl_certificate_key  cert.key;
  152. #    ssl_session_timeout  5m;
  153. #    ssl_protocols  SSLv2 SSLv3 TLSv1;
  154. #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  155. #    ssl_prefer_server_ciphers   on;
  156. #    location / {
  157. #        root   html;
  158. #        index  index.html index.htm;
  159. #    }
  160. #}
  161. }

#号为注释内容,我们在cmd下运行nginx

启动成功,出错的话,可以查询日志(日志路径是配置文件指定的,你可以修改存到其它位置)

访问一下第二个server 配置的localhost:8666地址,结果出现

三、Nginx可以通过以下两种方式来实现与Tomcat的耦合:

将静态页面请求交给Nginx,动态请求交给后端Tomcat处理。

将所有请求都交给后端的Tomcat服务器处理,同时利用Nginx自身的负载均衡功能进行多台Tomcat服务器的负载均衡。

下面通过两个配置实例分别讲述这两种实现

一、动态页面和静态页面分离的实例

这里假定Tomcat服务器的IP地址为192.168.12.130,同时Tomcat服务器开放的服务器端口为8080。Nginx相关配置代码如下:

  1. server {
  2. listen 80;
  3. server_name www.ixdba.net;
  4. root /web/www/html;
  5. location /img/ {
  6. alias /web/www/html/img/;
  7. }
  8. location ~ (\.jsp)|(\.do)$ {
  9. proxy_pass http://192.168.12.130:8080;
  10. proxy_redirect off;
  11. proxy_set_header Host $host;
  12. proxy_set_header X-Real-IP $remote_addr;
  13. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14. client_max_body_size 10m;
  15. client_body_buffer_size 128k;
  16. proxy_connect_timeout 90;
  17. proxy_send_timeout 90;
  18. proxy_read_timeout 90;
  19. proxy_buffer_size 4k;
  20. proxy_buffers 4 32k;
  21. proxy_busy_buffers_size 64k;
  22. proxy_temp_file_write_size 64k;
  23. }
  24. }

在这个实例中,首先定义了一个虚拟主机www.ixdba.net,然后通过location指令将/web/www/html/img/目录下的静态文件交给Nginx来完成。最后一个location指令将所有以.jsp、.do结尾的文件都交给Tomcat服务器的8080端口来处理,即http://192.168.12.130:8080

需要特别注意的是,在location指令中使用正则表达式后,proxy_pass后面的代理路径不能含有地址链接,也就是不能写成http://192.168.12.130:8080/,或者类似http://192.168.12.130:8080/jsp的形式。在location指令不使用正则表达式时,没有此限制。

2、多个tomcat负载均衡的实例

这里假定有3台Tomcat服务器,分别开放不同的端口,地址如下:

1
2
3
192.168.12.131:8000 
192.168.12.132:8080 
192.168.12.133:8090

Nginx的相关配置代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
upstream mytomcats { 
      server 192.168.12.131:8000
      server 192.168.12.132:8080
      server 192.168.12.133:8090
  
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 / { 
          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; 

最新文章

  1. PYTHON 写函数,计算传入字符串中【数字、字母、空格、以及其他的个数】
  2. ssh 文件权限影响登录
  3. 2.ReactNative Properties|States|Styles 笔记
  4. maven3.2.3+eclipse4.4+JDK1.8+win8.1_64bit环境搭建
  5. 127.0.0.1、0.0.0.0和本机IP地址的区别和使用
  6. CODEVS 1817 灾后重建 Label:Floyd || 最短瓶颈路
  7. game of life
  8. [转]windows下srand48()和drand48()的问题
  9. linux (centos 6.4)安装自定义分区方案(转载)
  10. PAT_1040 有几个PAT
  11. angularJS vs backbone
  12. 三星I9308(移动版)正确​Root的方法,进入正确的recovery的关键(网上很多方法是误导)
  13. 原生sql语句执行
  14. art patchoat
  15. Django中的Form表单
  16. OJ2237第k小数题解
  17. [您有新的未分配科技点]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
  18. Django模型中value函数运用
  19. 上次被人说TK不好咯,这次给你整个高大上的
  20. mysql字符集问题汇总

热门文章

  1. 监控web服务方法
  2. toStirng()与Object.prototype.toString.call()方法浅谈
  3. WordPress 添加Meta Box的方法步骤
  4. [已解决] MyBatis 中bind用法
  5. shell脚本 空格
  6. java中快速排序的理解以及实例
  7. js相关总结
  8. 覆盖的面积(HDU 1255 线段树)
  9. noi 2989 糖果
  10. LA 4287 等价性证明