×
新网 > 虚机资讯 > 正文

nginx第二章 nginx+tomcat负载均衡

nginx+tomcat做负载均衡只需修改配置文件nginx.conf,nginx配置参数说明: #使用哪个用户启动nginx 前面是用户,后面是组 user www www; #nginx工作的进程数量,一般认为配置值与机器核数相等为佳 worker_processes 2; # [ debug | info | notice | warn

 

nginx+tomcat做负载均衡只需修改配置文件nginx.conf,nginx配置参数说明:

#使用哪个用户启动nginx 前面是用户,后面是组
user www www;

#nginx工作的进程数量,一般认为配置值与机器核数相等为佳
worker_processes 2;

5 (1).jpg

# [ debug | info | notice | warn | error | crit ] 错误日志的位置
error_log /var/htdocs/logs/nginx_error.log crit;

#进程号保存文件
pid /usr/local/nginx/nginx.pid;

#最大文件描述符 建议设置启动nginx的shell可以打开的最大文件描述符

#修改/etc/sysctl.conf,增加fs.file-max=6553560,fs.file-max是指系统所有进程一共可以打开的文件数量

#可以使用ulimit -Hn/-Sn查看该值,可以修改/etc/security/limits.conf,增加以下两行,*表示对所有用户有效

 

#* soft nofile 65535

#* hard nofile 65535

 

#运行/sbin/sysctl -p命令,重新登录shell生效

worker_rlimit_nofile 65535;

events
{
# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
use epoll; #使用epoll(linux2.6的高性能方式,了解epoll相关知识和原理可上网络搜索)
worker_connections 51200; #每个进程最大连接数(最大连接=连接数x进程数)
}

http
{
#文件扩展名与文件类型映射表,具体查看同目录下的mime.types
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"\';


log_format download \'$remote_addr - $remote_user [$time_local] \'
\'"$request" $status $bytes_sent \'
\'"$http_referer" "$http_user_agent" \'
\'"$http_range" "$sent_http_content_range"\';

#默认编码
charset gb2312,utf-8;

server_names_hash_bucket_size 128;
#开启高效文件传输模式
sendfile on;
#以下两个选项用于防止网络阻塞 参考http://i.cn.yahoo.com/nesta2001zhang/blog/p_104/
tcp_nopush on;
tcp_nodelay on;


#长链接超时时间
keepalive_timeout 300;


#fastcgi连接超时时间,下面的看字面意思都能理解个大概了,就不解释了.
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_temp_path /dev/shm;

#打开gzip压缩
gzip on;
#最小压缩文件大小,一般大于2k的文件使用gzip能提升效率,否则很小的文件压缩反倒更消耗服务器性能
gzip_min_length 2k;
#压缩缓冲区
gzip_buffers 48k;
#压缩版本(默认1.1,前端为squid2.5使用1.0)
gzip_http_version 1.1;
#压缩类型,默认就已经包含text/html 所以下面就不用再写了,当然写上去的话,也不会有问题,但是会有一个warn
gzip_types text/plain application/x-javascript text/css text/html text/javascript application/xml;
#错误页面
error_page 404 /404.html;
error_page 403/404.html;
#上传文件大小限制
client_max_body_size 20m;
#设定请求头缓存,如果请求header过大,会使用large_client_header_buffers来读取
client_header_buffer_size 16k;
large_client_header_buffers 464k;
#设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的Squid开启3128端口
server localhost:8080 weight=5;
server 127.0.0.1:8080 weight=1;
}
#下面开始虚拟主机的配置
server
{
listen 80;
server_name localhost;

#设定本虚拟主机的访问日志
access_log logs/access.log main;


#如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid
#如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root #应用的根目录;

#刷新时间,根据静态文件修改的频度来调整,开发测试阶段可以短一些,生产阶段可以长一些
expires 24h;
}

#对 "/" 启用负载均衡
location / {

#http://后面跟upstream的名字
proxy_passhttp://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状态的地址,安装时必须加配置参数--with-http_stub_status_module
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
}
}

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading