ubuntu nginx安装及相关linux性能參数优化

一.安装
下载源代码,解压:tar -xzvf nginx-1.4.7.tar.gz
./configure
make && make install

改动默认nginx的监听port80为81

启动:/usr/local/nginx/sbin/nginx
停止:/usr/local/nginx/sbin/nginx -s stop

配置文件路径:/usr/local/nginx/conf/nginx.conf


linux系统内核參数优化:
fs.file-max=999999 //表示一个进程最多能够打开的文件句柄数
net.ipv4.tcp_tw_reuse=1 //重用TCP中TIME_WAIT状态的socket
net.ipv4.tcp_keepalive_time=600 //监控对方连接是否正常的心跳发送间隔时间,秒为单位
net.ipv4.tcp_fin_timeout=30 //当tcp处于FIN_WAIT_2状态时,socket保持的最长时间
net.ipv4.tcp_max_tw_buckets=5000 //处于TIME_WAIT最大的socket数量,默觉得180 000,超过这个数目的socket马上被清除
net.ipv4.tcp_rmem=4096 32768 262142 //tcp接收缓存(tcp接收滑动窗体),最小值,默认值,最大值
net.ipv4.tcp_wmem=4096 32768 262142 //tcp发送缓存(tcp发放滑动窗体),最小值,默认值,最大值
net.core.netdev_max_backlog=8096 //当内核处理速度慢于网卡接收数据包的速度时,存放这些来不及处理的数据包队列最大长度
net.core.rmem_default=262144 //tcp默认接收缓存区大小
net.core.wmem_default=262144 //tcp默认发送缓存区大小
net.core.rmem_max=2097152 //tcp最大接收缓存区大小
net.core.wmem_max=2097152 //tcp最大发送缓存区大小
net.ipv4.tcp_max_syn.backlog=999999 //tcp三次握手协议建立阶段内核保存sync请求队列的最大长度,超过这个长度的请求被丢弃


编缉/etc/sysct.conf文件,vim /etc/sysctl.conf

添�上述配置(不能用 // 作凝视)

fs.file-max=999999
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_keepalive_time=600
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_max_tw_buckets=5000
net.ipv4.tcp_rmem=4096 32768 262142
net.ipv4.tcp_wmem=4096 32768 262142
net.core.netdev_max_backlog=8096
net.core.rmem_default=262144
net.core.wmem_default=262144
net.core.rmem_max=2097152
net.core.wmem_max=2097152
net.ipv4.tcp_max_syn.backlog=999999 


之后保存,并运行sysctl -p命令使用其生效

查看是否生效:

sysctl -a |grep fs.file-max,若显演示样例如以下:

fs.file-max = 999999

则表明配置成功


二.配置nginx作为反向代理server
 改动/usr/local/nginx/conf/nginx.conf,
 在配置文件里的server下添加�一个locaiton例如以下
 location /webTest{
             proxy_pass http://localhost:8080/webTest;
             proxy_redirect default;
 }
 
 这里的proxy_redirect default; 当存在重定义时,我们将实际serverhttp://localhost:8080/webTest返回的地址解析成用户请求代理server的地址
 即这里用户当前请求http://localhost/webTesst,交互nginx反向代理server后,nginx匹配到上面的location,于是将请求交给实际serverhttp://localhost:8080/webTest。实际serverhttp://localhost:8080/webTest(能够是一台物理机)处理完请求后将响应送到代理server,代理server终于将结果返回给client。
 这里假设配置了 proxy_redirect default; 则会将发生重定向的实际server响应中的location改成代理server的地址(http://localhost/webTesst)指示client浏览器
须要又一次以get方法请求location中的服务。


posted @ 2014-11-12 09:11  hrhguanli  阅读(300)  评论(0编辑  收藏  举报