nginx, tomcat 集群

来源:互联网 发布:网络带来的利与弊简写 编辑:程序博客网 时间:2024/06/05 20:36

http://pengranxiang.iteye.com/blog/1135909


目标:同一台Linux主机上 安装 Nginx 和 两个 Tomcat 的集群

 

1.  下载安装 Nginx

 

地址:http://nginx.org/download/nginx-1.0.4.tar.gz

 

Shell代码  收藏代码
  1. cd /usr/local/src/nginx  
  2.   
  3. wget http://nginx.org/download/nginx-1.0.4.tar.gz  
  4.   
  5. tar zxvf nginx-1.0.4.tar.gz  
  6.   
  7. cd nginx-1.0.4  
  8.   
  9. ./configure  
  10.   
  11. make   
  12.   
  13. make install   
 

2. Tomcat的下载安装配置,请参照 http://pengranxiang.iteye.com/admin/blogs/1135072

 

3. 配置 Nginx 负载均衡 来集成 两个Tomcat

 

修改配置文件:$NGNINX_HOME/conf/nginx.conf

 

Conf代码  收藏代码
  1. #Nginx所用用户和组,window下不指定  
  2. user  nobody;  
  3.   
  4. #工作的子进程数量(通常等于CPU数量或者2倍于CPU)  
  5. worker_processes  1;  
  6.   
  7. #错误日志存放路径  
  8. #error_log  logs/error.log;  
  9. #error_log  logs/error.log  notice;  
  10. error_log  logs/error.log  info;  
  11.   
  12. #指定pid存放文件  
  13. pid        logs/nginx.pid;  
  14.   
  15.   
  16. events {  
  17. #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。  
  18. use epoll;  
  19.   
  20. #允许最大连接数    
  21. worker_connections  1024;  
  22. }  
  23.   
  24.   
  25. http {  
  26.     #mine.types内定义各文件类型映像  
  27.     include       mime.types;  
  28.   
  29.     default_type  application/octet-stream;  
  30.       
  31.     #定义日志格式  
  32.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  33.     #                  '$status $body_bytes_sent "$http_referer" '  
  34.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  35.   
  36.     #access_log  logs/access.log  main;  
  37.   
  38.     sendfile        on;  
  39.     #tcp_nopush     on;  
  40.   
  41.     #keepalive_timeout  0;  
  42.     keepalive_timeout  65;  
  43.   
  44.     #gzip  on;  
  45.   
  46.     #负载均衡集群设置  
  47.     upstream tomcats {  
  48.         server localhost:8080 weight=1;  
  49.         server localhost:9080 weight=1;  
  50.           
  51.         #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。  
  52.         #同一机器在多网情况下,路由切换,ip可能不同  
  53.         ip_hash;  
  54.     }     
  55.   
  56.     server {  
  57.         listen       80;  
  58.         server_name  localhost;  
  59.   
  60.         #charset koi8-r;  
  61.   
  62.         #access_log  logs/host.access.log  main;  
  63.   
  64.         location / {  
  65.             index  index.shtml;  
  66.             proxy_pass  http://tomcats;  
  67.             proxy_set_header    X-Real-IP   $remote_addr;  
  68.             client_max_body_size    100m;  
  69.         }  
  70.   
  71.         #error_page  404              /404.html;  
  72.   
  73.         # redirect server error pages to the static page /50x.html  
  74.         #  
  75.         error_page   500 502 503 504  /50x.html;  
  76.         location = /50x.html {  
  77.             root   html;  
  78.         }  
  79.   
  80.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  81.         #  
  82.         #location ~ \.php$ {  
  83.         #    proxy_pass   http://127.0.0.1;  
  84.         #}  
  85.   
  86.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  87.         #  
  88.         #location ~ \.php$ {  
  89.         #    root           html;  
  90.         #    fastcgi_pass   127.0.0.1:9000;  
  91.         #    fastcgi_index  index.php;  
  92.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  93.         #    include        fastcgi_params;  
  94.         #}  
  95.   
  96.         # deny access to .htaccess files, if Apache's document root  
  97.         # concurs with nginx's one  
  98.         #  
  99.         #location ~ /\.ht {  
  100.         #    deny  all;  
  101.         #}  
  102.     }  
  103.   
  104.   
  105.     # another virtual host using mix of IP-, name-, and port-based configuration  
  106.     #  
  107.     #server {  
  108.     #    listen       8000;  
  109.     #    listen       somename:8080;  
  110.     #    server_name  somename  alias  another.alias;  
  111.   
  112.     #    location / {  
  113.     #        root   html;  
  114.     #        index  index.html index.htm;  
  115.     #    }  
  116.     #}  
  117.   
  118.   
  119.     # HTTPS server  
  120.     #  
  121.     #server {  
  122.     #    listen       443;  
  123.     #    server_name  localhost;  
  124.   
  125.     #    ssl                  on;  
  126.     #    ssl_certificate      cert.pem;  
  127.     #    ssl_certificate_key  cert.key;  
  128.   
  129.     #    ssl_session_timeout  5m;  
  130.   
  131.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
  132.     #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;  
  133.     #    ssl_prefer_server_ciphers   on;  
  134.     #    location / {  
  135.     #        root   html;  
  136.     #        index  index.html index.htm;  
  137.     #    }  
  138.     #}  
  139.   
  140. }  
 

安装以上配置即可实现集群。

 

关于Nginx启动

 

直接执行: $NGINX_HOME/sbin/nginx 即可

 

为了方便,可以自定义一个脚本文件

 

vim nginx.sh

 

Sh代码  收藏代码
  1. #!/bin/bash  
  2.   
  3.   
  4. case $1 in  
  5.     start)  
  6.         /usr/local/nginx/sbin/nginx;  
  7.         ;;  
  8.     stop)  
  9.         kill -2 `ps -ef|grep "/usr/local/nginx/sbin/nginx" | grep -v "grep"|awk '{print $2}'`  
  10.         ;;  
  11.     restart)  
  12.         $0 stop  
  13.         $0 start  
  14.         ;;  
  15.     *)  
  16.         echo $"Usage; $0{start|stop|restart}"  
  17.         exit 1  
  18. esac  
  19. exit 0  

0 0
原创粉丝点击