搭建nginx+tomcat+Java的负载均衡环境

一、简介:
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]
#运行用户#user nobody;#开启进程数 <=CPU数worker_processes 1;#错误日志保存位置#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#进程号保存文件#pid logs/nginx.pid; #等待事件events { #linux下打开提高性能 #use epoll; #每个进程最大连接数(最大连接=连接数x进程数) 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"';#请求日志保存位置 #access_log logs/access.log main;#设定请求缓冲 client_header_buffer_size 1k; large_client_header_buffers 4 4k;#打开发送文件 sendfile on; #tcp_nopush on;#keepalive_timeout 0; keepalive_timeout 65;#客户端上传文件大小控制 client_max_body_size 8m;#打开gzip压缩 #gzip on;#设定负载均衡的服务器列表 #upstream mysvr { # #weigth参数表示权值 , 权值越高被分配到的几率越大 # #本机上的Squid开启3128端口 # #server 192.168.8.1:3128 weight=5; # #server 192.168.8.2:80 weight=1; # #server 192.168.8.3:80 weight=6; #}#第一个虚拟主机 server { #监听IP端口 listen 80; #主机名 server_name localhost; #root#设置字符集 #charset koi8-r; #本虚拟server的访问日志 相当于局部变量 #access_log logs/host.access.log main; #日志文件输出格式 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';location / { root html; index index.html index.htm; }#静态文件缓存时间设置 #location ~ .*.(gif|jpg|jpeg|png|bmp|swf)${# expires 30d; #}#静态文件缓存时间设置 #location ~ .*.(js|css)?${# expires 1h; #}#对本server"/"启用负载均衡 #location / { # proxy_pass http://mysvr; # 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; #}#设定查看Nginx状态的地址 #location /NginxStatus { # stub_status on; # access_log on; # auth_basic “NginxStatus”; # auth_basic_user_file conf/htpasswd; #}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }# proxy the php scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} }# another virtual host using mix of IP-, name-, and port-based configurationserver { #多监听listen localhost:8666; #主机名 server_name LIULJ2576; #WEB文件路径 root E:/Portal; #默认首页 index HomePage.html;#location / { # #这里相当于局部变量 # root E:/Portal; # index HomePage.html; #} }# HTTPS server HTTPS SSL加密服务器 # #server { # listen 443; # server_name localhost;# ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key;# ssl_session_timeout 5m;# ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on;# location / { # root html; # index index.html index.htm; # } #} }#号为注释内容 , 我们在cmd下运行nginx
启动成功 , 出错的话 , 可以查询日志(日志路径是配置文件指定的 , 你可以修改存到其它位置)


推荐阅读