一.優化Nginx并發量
[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/
Benchmarking 192.168.4.5 (be patient)
socket: Too many open files (24) //提示打開文件數量過多
修改Nginx配置文件,增加并發量
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
worker_processes 2; //與CPU核心數量一致
events {
worker_connections 65535; //每個worker最大并發連接數
use epoll;
}
.. ..
[root@proxy ~]# nginx -s reload
二.優化Linux內核參數(最大文件數量)
[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 ~]# ab -n 2000 -c 2000 http://192.168.4.5/
三.優化Nginx數據包頭緩存
[root@proxy ~]# cat lnmp_soft/buffer.sh
#!/bin/bash
URL=http://192.168.4.5/index.html?
for i in {1..5000}
do
URL=${URL}v$i=$i
done
curl $URL //經過5000次循環后,生成一個長的URL地址欄
[root@proxy ~]# ./buffer.sh
.. ..
<center><h1>414 Request-URI Too Large</h1></center> //提示頭部信息過大
修改Nginx配置文件,增加數據包頭部緩存大小
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
client_header_buffer_size 1k; //默認請求包頭信息的緩存
large_client_header_buffers 4 4k; //大請求包頭部信息的緩存個數與容量
.. ..
}
[root@proxy ~]# nginx -s reload
四.對頁面進行壓縮處理
[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次會被緩存
}
六.瀏覽器本地緩存靜態數據
[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 ~]# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html
[root@proxy ~]# nginx -s reload
到此這篇關于nginx優化的六點方法的文章就介紹到這了,更多相關nginx優化內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!