nginx优化

一、开启--with-http_stub_status_module模块可以看到访问状态 

[root@proxy ~]# tar  -zxvf   nginx-1.12.2.tar.gz
[root@proxy ~]# cd  nginx-1.12.2
[root@proxy nginx-1.12.2]# ./configure   \
> --with-http_ssl_module						#开启SSL加密功能
> --with-stream								#开启TCP/UDP代理模块
> --with-http_stub_status_module				        #开启status状态页面
[root@proxy nginx-1.12.2]# make && make install	                  #编译并安装    

 编辑配置文件再server添加下面配置

[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
… …
location /status {
                stub_status on;
				 #allow IP地址;
				 #deny IP地址;
        }
… …

 访问网页  http://ip/status 可看到下面的信息

Active connections: 1 
server accepts handled requests
 10 10 3 
Reading: 0 Writing: 1 Waiting: 0

Active connections:当前活动的连接数量。

Accepts:已经接受客户端的连接总数量。

Handled:已经处理客户端的连接总数量。

(一般与accepts一致,除非服务器限制了连接数量)。

Requests:客户端发送的请求数量。

Reading:当前服务器正在读取客户端请求头的数量。

Writing:当前服务器正在写响应信息的数量。

Waiting:当前多少客户端在等待服务器的响应。

 二、优化并发

  (一)修改配置文件

root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
worker_processes  2;					//与CPU核心数量一致
events {
worker_connections 65535;		//每个worker最大并发连接数
}

   (二)修改内核参数ulimit

[root@proxy ~]# ulimit -a						//查看所有属性值
[root@proxy ~]# ulimit -Hn 100000				//设置硬限制(临时规则)
[root@proxy ~]# ulimit -Sn 100000				//设置软限制(临时规则)
[root@proxy ~]# vim /etc/security/limits.conf
	.. ..
*               soft    nofile            100000
*               hard    nofile            100000

#该配置文件分4列,分别如下:
#用户或组    硬限制或软限制    需要限制的项目   限制的值

 三、浏览器本地缓存静态数据

root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires		30d;			//定义客户端缓存时间为30天
}
}

 四、日志切割

[root@proxy ~]# vim /usr/local/nginx/logbak.sh
#!/bin/bash
date=`date +%Y%m%d`
logpath=/usr/local/nginx/logs
mv $logpath/access.log $logpath/access-$date.log
mv $logpath/error.log $logpath/error-$date.log
kill -USR1 $(cat $logpath/nginx.pid)

[root@proxy ~]# crontab -e
03 03 * * 5  /usr/local/nginx/logbak.sh

 五、对页面进行压缩处理

[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
http {
.. ..
gzip on;							//开启压缩
gzip_min_length 1000;				//小文件不压缩
gzip_comp_level 4;				//压缩比率
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
									//对特定文件压缩,类型参考mime.types
.. ..
}

 六、服务器内存缓存(如果需要处理大量静态文件,可以将文件缓存在内存,下次访问会更快。

http { 
open_file_cache          max=2000  inactive=20s;
        open_file_cache_valid    60s;
        open_file_cache_min_uses 5;
        open_file_cache_errors   off;
//设置服务器最大缓存2000个文件句柄,关闭20秒内无请求的文件句柄
//文件句柄的有效时间是60秒,60秒后过期
//只有访问次数超过5次会被缓存
} 

 

posted @ 2020-11-12 16:05  zsh~  阅读(109)  评论(0)    收藏  举报