详解Ngigx+Tomcat配置动静分离,负载均衡
由于公司使用过Ngnix,对于刚接触Nginx来说,感觉有些好奇,于是研究了下。
本人在windows下使用的版本是nginx-1.8.1:
1.启动Ngnix
双击nginx-1.8.1文件夹中nginx.exe,当任务管理器中存在两个nginx进程时,则说明启动成功!
2.Ngnix常用命令
- nginx-sstop强制关闭
- nginx-squit安全关闭
- nginx-sreload改变配置文件的时候,重启nginx工作进程,来时配置文件生效
- nginx-sreopen打开日志文件
3.Nginx配置
下面配置综合了网上的资料,记下,防止自己忘记。
#Nginx所用用户和组
#usernobody;
#工作的子进程数量(通常等于CPU数量或者2倍于CPU)
worker_processes1;
#错误日志存放路径
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#指定pid存放文件
#pidlogs/nginx.pid;
events{
#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue
#useepoll;
#使用epoll模型提高性能win下不需要
#useepoll;
#允许最大连接数
worker_connections1024;
}
http{
#扩展名与文件类型映射表
includemime.types;
#默认类型
default_typeapplication/octet-stream;
#定义日志格式
#log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
#'$status$body_bytes_sent"$http_referer"'
#'"$http_user_agent""$http_x_forwarded_for"';
#access_loglogs/access.logmain;
#启用内核复制模式,应该保持开启达到最快IO效率
sendfileon;
#tcp_nopushon;
#keepalive_timeout0;
#HTTP1.1支持持久连接alive
#降低每个连接的alive时间可在一定程度上提高可响应连接数量,所以一般可适当降低此值
keepalive_timeout65;
#启动gzip压缩功能设置,有效降低网络流量
gzipon;
gzip_min_length1k;#最小1K
gzip_buffers416k;
gzip_http_version1.0;
gzip_comp_level2;
gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
gzip_varyon;
#静态文件缓存
#最大缓存数量,文件未使用存活期
open_file_cachemax=655350inactive=20s;
#验证缓存有效期时间间隔
open_file_cache_valid30s;
#有效期内文件最少使用次数
open_file_cache_min_uses2;
#xbqadd
#upstream作负载均衡,在此配置需要轮询的服务器地址和端口号,max_fails为允许请求失败的次数,默认为1.
#weight为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。
upstreamhostname{
server127.0.0.1:9000max_fails=0weight=2;
server127.0.0.1:9001max_fails=0weight=2;
}
server{
listen8181;
server_namelocalhost;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
root/img;#在nginx-1.8.1文件夹中新建img文件夹,用于存放静态资源
location/{
#roothtml;
#indexindex.htmlindex.htm;
#xbqadd
proxy_passhttp://hostname;
#下面三条指令允许重新定义和添加一些将被转移到被代理服务器的请求头部信息
#请求头中Host信息
proxy_set_headerHost$host;
#真实的客户端IP
proxy_set_headerX-Real-IP$remote_addr;
#代理路由信息,此处取IP有安全隐患
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
#真实的用户访问协议
proxy_set_headerX-Forwarded-Proto$scheme;
#默认值default,
#后端response302时tomcatheader中location的host是http://192.168.1.62:8080
#因为tomcat收到的请求是nginx发过去的,nginx发起的请求urlhost是http://192.168.1.62:8080
#设置为default后,nginx自动把响应头中locationhost部分替换成当前用户请求的host部分
#网上很多教程将此值设置成off,禁用了替换,
#这样用户浏览器收到302后跳到http://192.168.1.62:8080,直接将后端服务器暴露给浏览器
#所以除非特殊需要,不要设置这种画蛇添足的配置
proxy_redirectdefault;
client_max_body_size10m;#允许客户端请求的最大单文件字节数
client_body_buffer_size128k;#缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout90;#nginx跟后端服务器连接超时时间
proxy_read_timeout90;#连接成功后,后端服务器响应时间
proxy_buffer_size4k;#设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers632k;#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size64k;#高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size64k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
#xbqadd
#配置Nginx动静分离,定义的静态页面直接从/usr/nginxStaticFile(Nginx发布目录)读取。
location~\.(gif|jpg|jpeg|png|css|js|php)${
#expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力E:/staticResource;
expires7d;
}
#xbqadd
#启用nginxstatus监听页面
location/nginxstatus{
stub_statuson;
access_logon;
}
#error_page404/404.html;
#redirectservererrorpagestothestaticpage/50x.html
#
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
#proxythePHPscriptstoApachelisteningon127.0.0.1:80
#
#location~\.php${
#proxy_passhttp://127.0.0.1;
#}
#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
#
#location~\.php${
#roothtml;
#fastcgi_pass127.0.0.1:9000;
#fastcgi_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
#includefastcgi_params;
#}
#denyaccessto.htaccessfiles,ifApache'sdocumentroot
#concurswithnginx'sone
#
#location~/\.ht{
#denyall;
#}
}
#anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration
#
#server{
#listen8000;
#listensomename:8080;
#server_namesomenamealiasanother.alias;
#location/{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
#HTTPSserver
#
#server{
#listen443ssl;
#server_namelocalhost;
#ssl_certificatecert.pem;
#ssl_certificate_keycert.key;
#ssl_session_cacheshared:SSL:1m;
#ssl_session_timeout5m;
#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;
#location/{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。