Nginx+Tomcat配置

来源:互联网 发布:上海奉贤云计算公司 编辑:程序博客网 时间:2024/06/05 15:38

为什么使用Nginx?

Apache

  • 经典的Web服务器
  • 除了慢没有别的缺点了
  • Apache2对fcgi支持并不好
  • 非常好用的proxy和proxy_ajp(很多人用它作为tomcat的前端)
  • 不支持epoll(这年头,epoll几乎是性能的必备)

Nginx

  • 速度快,占用资源少
  • 杀手级的proxy和rewrite
  • 非常不错的静态文件能力
  • 最适合作为整个网站的前端服务(将php、svn等不同请求发送往后端apache)
  • 其他功能马马虎虎

代理 服务器,也是一个 IMAP/POP3/SMTP代理服务器。已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而被人们广泛使用了。
大多数人选择它主要考虑到的是它的高并发和负载均衡。


安装Nginx
1. tar zxvf  nginx0.8.36.tar.gz

2、编译安装nginx
   cd nginx-0.7.44
   ./configure--with-http_stub_status_module --with-http_ssl_module--prefix=/usr/local/nginx
   执行后,可能会提示 ./configure: error: the HTTP rewritemodule requires the PCRE library.这个错误,是缺少pcre 包,可用yum install pcrepcre-devlel来解决

3、make && makeinstall

4、nginx安装成功后的安装目录为/usr/local/nginx
5.编辑配置文件nginx.conf
#user  www www; 
worker_processes8;   #启动进程数,可根据机器核数来修改
error_log /usr/local/nginx/logs/nginx_error.log  crit;#全局错误日志及PID文件
pid       /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events
   {
    use epoll;
    worker_connections 65535; #工作模式及连接数上限
   

http                 #设定http服务器,利用它的反向代理功能提供负载均衡支持
   {
   include      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;  #定义的日志格式和存放路径
    
    #GeneralOptions
   server_names_hash_bucket_size 128;
   client_header_buffer_size 32k;
   large_client_header_buffers 4 32k;
   client_body_buffer_size   8m; #256k
   server_tokens off;
   ignore_invalid_headers  on;
   recursive_error_pages   on;
   server_name_in_redirect off; 
   sendfile                on;
 
   #timeouts
   keepalive_timeout 60;
   #client_body_timeout   3m;
   #client_header_timeout 3m;
   #send_timeout         3m;
     
    #TCPOptions
   tcp_nopush  on;
    tcp_nodelayon;
  
    #sizelimits
   client_max_body_size      50m;

    gzipon;
   gzip_min_length  1k;
   gzip_buffers    4 16k;
   gzip_http_version 1.0;
   gzip_comp_level 2;
   gzip_types      text/plain application/x-javascript text/css application/xml;
    gzip_varyon;
  
    upstreamxxx.com {
        ip_hash;
        server x.x.x.x:8080 max_fails=0 weight=1;
        server x.x.x.x:8080 max_fails=0 weight=1; #8080为tomcat端口
    }

   server {
       listen 80 default;
       rewrite ^(.*) http://www.xxx.com/ permanent;
       #charset koi8-r;
       #access_log logs/host.access.log  main;
       #error_page 404             /404.html;
       # redirect server error pages to the static page /50x.html
       #
       error_page   500 502 503504  /50x.html;
       location = /50x.html {
       root   html;
       }
    }

   server
       {
           listen     80;
           server_name www.xxx.com;
           index index.php index.html index.htm;
           root   /http;
           access_log /data/logs/access.xxx.com.log combined;
           error_log  /data/logs/error_test.xxx.log;
           
           #expires                        
           location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
             {
                  expires 30d;
              }
 
           location ~ .*\.(js|css)?$
             {
                  expires 24h;
               
 
           location /nginxstatus {
               stub_status on;
               access_log off;
            }

         # location ~ .*\.jsp?$
           location /
           {
               proxy_passhttp://npduxiu;
               proxy_redirect off;
             proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header Host $http_host;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header Connection Close;
               proxy_set_header X-Forwarded-For $remote_addr;
               error_page  404             /404.html;
               error_page   500 502 503504  /50x.html;
             location = /50x.html {
             root  html;
             }
          }
        
       }
}

6、修改/usr/local/nginx/conf/nginx.conf配置文件后,用命令/usr/local/nginx/sbin/nginx -t检查配置文件是否正确:

7、启动nginx的命令
  /usr/local/nginx/sbin/nginx

8、停止nginx的命令
  /usr/local/nginx/sbin/nginx -s stop
9、在不停止Nginx服务的情况下加载Nginx配置
  kill -HUP `cat/usr/local/nginx/nginx.pid` 

10.nginx网站开启后(已启用stub_stauts),可用http://www.xxx.com/nginxstatus查看nginx状态
   active connections --对后端发起的活动连接数
   server accepts handledrequests -- nginx 总共处理的连接, 成功创建了几次握手  总共处理了多少个请求
   reading -- nginx读取到客户端的Header信息数
   writing -- nginx返回给客户端的Header信息数
   waiting -- 开启keep-alive 的情况下,这个值等于active - (reading +writing)

安装Tomcat
下面开始Tomcat的安装了,那就更简单了,网上文档也是一大把
Tomcat安装
一、jdk的安装
   安装的jdk为:jdk-6u21-linux-x64.bin
   1.shjdk-6u17-linux-x64-rpm.bin
   2.安装程序在问您是否愿意遵守刚才看过的许可协议。输入"y"或 "yes" 回车
   3.执行后,把后成的文件夹命名为jdk,放到/usr/local/下(路径可自己选,但在变量中要指出)
   4.vi /etc/profile
         在里面添加如下内容
         export JAVA_HOME=/usr/local/jdk
         export JAVA_BIN=/usr/local/jdk/bin
         export PATH=$PATH:$JAVA_HOME/bin
         exportCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
   5. source/etc/profile  使刚才填加的生效
   6.验证
    java -version
    屏幕输出:
    java version "1.6.0_13"
    Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
    Java HotSpot(TM) Server VM (build 11.3-b02, mixed mode)
   安装JDK1.6完毕.
 
二、 Tomcat的安装
   下载apache-tomcat-6.0.18.tar.gz,解压后更名或更改存放路径(可自行规定)
 
   nginx与tomcat的结合,主要用的是nginx中的upstream,后端可包括有多台tomcat来处ginx的upstream目前支持4种方式的分配

1、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 
例如:
    upstreambakend {
        server 192.168.0.14 weight=10;
        server 192.168.0.15 weight=10;
   }

2、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session 的问题。
例如:
    upstreambakend {
        ip_hash;
        server 192.168.0.14:88;
        server 192.168.0.15:80;
   }

3、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
    serverserver1;
    serverserver2;
    fair;
}

4、url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

upstream backend {
    serversquid1:3128;
    serversquid2:3128;
   hash   $request_uri;
    hash_methodcrc32;
}

tips:

upstream bakend{#定义负载均衡设备的Ip及设备状态
ip_hash;
    server127.0.0.1:9090 down;
    server127.0.0.1:8080 weight=2;
    server127.0.0.1:6060;
    server127.0.0.1:7070 backup;
}
在需要使用负载均衡的server中增加
proxy_pass http://bakend/;

每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1.weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On 可以讲clientpost过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录

location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
0 0
原创粉丝点击