一:对nginx的全局配置
user nobody;
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 {
worker_connections 1024;
}
说明:
user:指定nginx worker进程运行用户以及用户组,默认是nobody
work_processes:指定nginx开启的进程数,一般一个就够了。如果是多核cpu,建议和cpu的数量一样多。
worker_connections:指定nginx每个进程数的最大连接数,默认是1024。最大客户端连接数max_clients=worker_processes*max_connections。如果是反向代理,则是max_clients=work_processes*worker_connections/4
补充:
worker_rlimit_nofile:指定一个nginx进程可以打开的最多文件描述符数目。必须要和ulimit命令结合使用。
二 http服务器配置
sendfile:开启高效文件传输模式。
keepalive_timeout:用于设置客户端链接保持活动的超时时间。超过这个时间,服务器会关闭此连接。
三 server虚拟主机配置
charset:设置网页的默认编码格式。
index:设定访问的默认首页地址。
root:指定虚拟主机的网页根目录。
四:StubStatus模块配置(使用该模块必须编译进nginx,./configure --with-http_stub_status_module,查看该模块是否包含stub_status模块,输入以下命令:/usr/local/nginx/sbin/nginx -V)
此模块记录的是nginx启动以来的工作状态。
location /nginxstatus {
stub_status on;
access_log logs/nginxstatus.log;
auth_basic "nginxstatus";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
[root@localhost ~]# htpasswd -c /usr/local/nginx/.htpasswd webadmin
New password:
Re-type new password:
Adding password for user webadmin
然后重新启动服务,在浏览器一栏输入ip地址加上nginxstatus,接着输入用户名和密码,就可以看到:
Active connections: 1
server accepts handled requests
2 2 5
Reading: 0 Writing: 1 Waiting: 0
五:nginx性能优化
1.减小nginx编译后的文件大小
编译nginx时,默认是以debug模式进行,而debug模式会插入许多跟踪之类的信息。
取消debug模式方法:
进入nginx解压后的文件,编辑auto/cc/gcc,找到如下几行:
# debug
CFLAGS="$CFLAGS -g"
将这两行注释掉,就可以取消debug模式。据我的测试,取消debug模式后,编译完成后的文件大小为732k,不取消debug,大小为3.1M。
2.利用TCMalloc优化nginx性能
TCMalloc全称为Thread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个成员。TCMalloc库在内存分配效率和速度上优于glibc,这在很大程度上提高了服务器在高并发情况下的性能,从而降低系统负载.以下是安装过程:
1.下载libunwind,地址是:
[root@localhost ~]# tar xvf libunwind-1.1.tar.gz
[root@localhost libunwind-1.1]# CFLAGES=-fPIC ./configure
[root@localhost libunwind-1.1]# make CFLAGES=-fPIC
[root@localhost libunwind-1.1]# make CFLAGES=-fPIC install
2.安装google-perftools,地址是:
[root@localhost ~]# tar xvf google-perftools-1.8.3.tar.gz
[root@localhost ~]# cd google-perftools-1.8.3
[root@localhost google-perftools-1.8.3]# ./configure
[root@localhost google-perftools-1.8.3]# make && make install
[root@localhost google-perftools-1.8.3]# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
[root@localhost google-perftools-1.8.3]# ldconfig
3.重新编译nginx
[root@localhost nginx-1.4.7]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-google_perftools_module
[root@localhost nginx-1.4.7]# make && make install
4.为google-perftools添加线程目录
[root@localhost ~]# mkdir /tmp/tcmalloc
[root@localhost ~]# chmod 0777 /tmp/tcmalloc/
5.修改nginx的主配置文件
修改nginx.conf文件,在pid行的下面添加如下内容:
#pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc
接着重启nginx服务
6.验证运行状态
[root@localhost nginx]# lsof -n | grep tcmalloc
nginx 6918 nobody 18w REG 253,0 0 417112 /tmp/tcmalloc.6918
nginx 6919 nobody 20w REG 253,0 0 417115 /tmp/tcmalloc.6919
nginx 6920 nobody 22w REG 253,0 0 417113 /tmp/tcmalloc.6920
nginx 6921 nobody 24w REG 253,0 0 417114 /tmp/tcmalloc.6921
阅读(630) | 评论(0) | 转发(0) |