疯狂

STANDING ON THE SHOULDERS OF GIANTS
posts - 481, comments - 486, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

nginx和tomcat负载简单配置(windows环境)

Posted on 2011-12-07 10:40 疯狂 阅读(6948) 评论(2)  编辑  收藏 所属分类: java性能架构nginx

本文讲了windows环境下的nginxtomcat的一个简单的配置实例包括dwr的的js特殊的地方配置。当然nginx只有才unix下才能表现出好的性能,主要是io模型等方面。
关于linux下的配置可见于网上的这篇文章http://bbs.linuxtone.org/forum.php?mod=viewthread&tid=394
 项目demo的下载:/Files/freeman1984/ndemo.rar,里面包含了简单的测试项目和配置文件,以及nginx的参考文档一份。

项目单独在tomcat下访问路径为: http://localhost:8088/ndemo/server.jsp

Nginx下访问路径:http://www.joe.com/server.jsp  其中(www.joe.com是我在hosts文件里面配置的

一下是nginx的配置:


Nginx.conf
内容:

 

#user nobody;

worker_processes 
2#进程数,和cpu个数相关

pid        logs
/nginx.pid;

events {

    worker_connections 
1024;#每个进程的支持的连接数

}

 

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"';

    sendfile        on;

    

    keepalive_timeout 
65;

 

    include gzip
.conf;#启动数据压缩

 

     

    server {

        
listen       80;

        server_name www
.joe.com;#此部分测试的时候可以在host文件里面加上

        root   E
:\apache-tomcat-6.0.32\webapps;#项目路径,直接和后台使用一个目录

        error_page 
404 /ndemo/404.html;

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

       

        location 
~ ^/(WEB-INF)/ {

            deny all;

        }

        
#此部分主要是dwr的js有写是服务器动态生成的,不能交给nginx处理,这样会出现404,需要单独处理并交给服务器处理。

         location 
/ndemo/dwr/ {    

             proxy_pass http
://localhost:8088/ndemo/dwr/;

             include proxy
.conf;

        }

#其他的静态文件处理dwr的都交给nginx处理

        location 
~ \.


(htm
|html|gif|jpg|jpeg|png|ico|rar|css|js|zip|txt|flv|swf|doc|ppt|xls|pdf)$ {

            access_log off;

            expires 24h;

        }

       
#注意我的上下文是ndemo

       location 
/ndemo/ {

             proxy_pass http
://localhost:8088/ndemo/;

             include proxy
.conf;

        }

        
#访问根目录下的路径也就是webapp,默认跳转到ndemo项目的路径下,也就是跳转到上面的路径

        location 
/ {

             rewrite (
.*)$ /ndemo$1 permanent;

        }

       location 
~ ^/NginxStatus {#nginx的状态监控需要basic密码保护,可使用apache的工具生成,方式可见于http://www.blogjava.net/freeman1984/articles/363943.html}

            stub_status on;

            access_log off;

            auth_basic              
"valid-user";

            auth_basic_user_file ewds_pw;

       }

    }

 

}

 

proxy.conf

 

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

proxy_send_timeout      
300;

proxy_read_timeout      
300;

proxy_buffer_size       4k;

proxy_buffers           
4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

 

gzip.conf

 

gzip            on;

gzip_min_length     
1024;

gzip_types      text
/plain text/css application/x-javascript; 

 

顺便展示单独使用tomcat和使用了nginxgzip的请求对照情况,可以明显发现nginx对静态文件的处理效率非常高。

 

单独使用tomcat

 

使用nginxgzip压缩后的请求情况

 


评论

# re: nginx和tomcat负载简单配置(windows环境)  回复  更多评论   

2011-12-10 13:05 by tb
不错 好资料

# re: nginx和tomcat负载简单配置(windows环境)[未登录]  回复  更多评论   

2015-07-29 00:39 by alex
你下面的访问速度快是因为有浏览器缓存

只有注册用户登录后才能发表评论。


网站导航: