高性能web服务器nginx---实战篇
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了高性能web服务器nginx---实战篇,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4718字,纯文字阅读大概需要7分钟。
内容图文
Nginx 设计架构图:
2 、安装
2.1 Nginx 依赖关系
yum install -y openssl-devel pcre-devel libevent
2.2 安装 nginx 依赖 pcre 库 , 使 Nginx 支持 HTTP Rewrite 模块
tar xf pcre-VERSION.tar.gz
cd pcre-VERSION
./configure
make && make install
2.3 Nginx 编译安装
# 添加 Nginx 系统用户
useradd -r -s /sbin/nologin -M nginx
tar xf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
make && make install
# Nginx PATH 环境变量
echo ‘export PATH=/usr/local/nginx/sbin:$PATH‘ > /etc/profile.d/nginx.sh
. /etc/profile.d/nginx.sh
3 、配置
3.1 Nginx 配置文件结构:
3.2 配置文件详解
3.3 日常维护技巧
配置正确性检查
nginx 启动、关闭、重启
3.4 开启目录浏览功能
例如:
localtion /soft {
# 开启 Nginx 目录浏览功能
autoindex on;
# 文件大小从 KB 开始显示
autoindex_exact_size off;
# 显示文件修改时间为服务器本地时间
autoindex_localtime on;
}
4 、 nginx 常用功能
反向代理
1 、多域名跳转应用实例
2 、 Nginx 重定向实现新旧域名过渡
rewrite
if ($host != ‘www.sharelinux.cn‘) {
rewrite ^/(.*)$ http://www.sharelinux.com/$1permanent;
}
3 、 alias 和 root 区别 举例说明
4 、 Location 命令应用配置
=
/
^~
~*
匹配模式、匹配优先级举例
URL 重写
1 、 if 命令
2 、 rewrite 命令
3 、 set 命令
4 、 break 命令
模块
5 、案例: web 缓存服务器
nginx_ngx_cache-2.1.tar.gz
--add-module
6 、案例:负载均衡器
负载均衡算法
轮询、 weight 、 ip _hash 、 fair 、 url_hash 、
HTTP Upstream
down 、 backup 、 max _fails 、 fail_timeout
调度器算法为 ip_hash 时,后端服务器在负载均衡器调度中的状态不能是 weight 、 backup
upstream myserver{
server 192.168.1.11:80 weight=3 max_fails=3 fail_timeout=20s;
server 192.168.1.12:80 weight=1 max_fails=3 fail_timeout=20s;
server 192.168.1.13:80 weight=4 max_fails=3 fail_timeout=20s;
}
location / {
proxy_pass http://myserver;
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
include conf/proxy.conf;
}
proxy_next_upstream 参数定义故障转移策略,当后端服务器节点返回 500, 、 502 、 503 、 504 、和执行超时等错误时,自动将请求转发到 upstream 负载均衡组中的另一台服务器,实现故障转移。
7 、 nginx 性能优化技巧
1 、减小编译后的文件大小
关闭 debug 模式
2 、为特定 CPU 指定 CPU 类型编译优化
在编译 Nginx 时,默认的 FCC 编译参数是 "-o" ,要优化 GCC 编译,可以使用以下两个参数
--with-cc-opt=‘-O3‘
--with-cpu-opt=CPU
# 为特定的 CPU 编译,有效的值包括:
# pentium 、 pentiumpro 、 pentium 3 、 pentium 4 、 athlon 、 opteron 、 amd64 、 sparc32 、 sparc64 、 ppc64
确定 CPU 类型: cat /proc/cpuinfo |grep "model name"
3 、 TCMalloc 优化 Nginx 的性能 ( Thread-Caching Malloc )
开源工具 google -perftools
与标准的 glibc 库的 malloc 相比, TCMalloc 库在内存分配效率和速度上要高很多,这在很大程度上提高了服务器在高并发情况向的性能,从而减低系统负载。为 Nginx 添加 TCMalloc 库支持。
安装 TC Malloc 库,需要安装 libunwind(32 位操作系统不需要安装 ) 和 google-perftools 两个软件包, libunwind 库为基于 64 位 CPU 和操作系统的程序提供了基本函数调用链和函数调用寄存器功能。
3.1 、按 libunwind 库
下载: http://download.savannah.gnu.org/releases/libunwind/
http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz
tar xf libunwind-1.1.tar.gz
cd libunwind-1.1
CFLAGS=-f PIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
3.2 、 google-perftools
https://code.google.com/p/gperftools/
https://googledrive.com/host/0B6NtGsLhIcf7MWxMMF9JdTN3UVk/gperftools-2.2.tar.gz
tar xf gperftools-2.2.tar.gz
cd gperftools-2.2
./configure
make && make install
echo ‘/usr/local/lib‘/etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
3.3 、重新编译 Nginx ,在编译安装过程中添加 "--with-google_perftools_module" 选项重新编译 Nginx
./configure --prefix=/usr/local/nginx --with-google_perftools_module --with-http_stub_status_module
make && make install
3.4 、为 google-perftools 添加线程目录
mkdir -p /tmp/tcmalloc
chmod 0777 /tmp/tcmalloc
3.5 、修改 Nginx 主配置文件,在 pid 这一行添加如下代码:
# pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;
3.6 、验证运行状态
lsof -n | grep tcmall
4 、 Nginx 内核参数优化
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp.ip_local_port_range = 1024 65000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.core.somaxconn = 262144
net.core.netudv_max_backlog = 262144
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
本文出自 “心静梵音” 博客,请务必保留此出处http://masters.blog.51cto.com/6516495/1597131
原文:http://masters.blog.51cto.com/6516495/1597131
内容总结
以上是互联网集市为您收集整理的高性能web服务器nginx---实战篇全部内容,希望文章能够帮你解决高性能web服务器nginx---实战篇所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。