nginx優(yōu)化的六點方法
更新時間:2021年01月23日 15:37:48 作者:PHP開發(fā)社區(qū)
這篇文章主要介紹了nginx優(yōu)化的六點方法,有對nginx優(yōu)化不太熟悉的同學可以參考下
一.優(yōu)化Nginx并發(fā)量
[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配置文件,增加并發(fā)量
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
worker_processes 2; //與CPU核心數量一致
events {
worker_connections 65535; //每個worker最大并發(fā)連接數
use epoll;
}
.. ..
[root@proxy ~]# nginx -s reload
二.優(yōu)化Linux內核參數(最大文件數量)
[root@proxy ~]# ulimit -a //查看所有屬性值 [root@proxy ~]# ulimit -Hn 100000 //設置硬限制(臨時規(guī)則) [root@proxy ~]# ulimit -Sn 100000 //設置軟限制(臨時規(guī)則) [root@proxy ~]# vim /etc/security/limits.conf .. .. * soft nofile 100000 * hard nofile 100000 #該配置文件分4列,分別如下: #用戶或組 硬限制或軟限制 需要限制的項目 限制的值
優(yōu)化后測試服務器并發(fā)量
[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/
三.優(yōu)化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次循環(huán)后,生成一個長的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; //大請求包頭部信息的緩存?zhèn)€數與容量
.. ..
}
[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次會被緩存
}
六.瀏覽器本地緩存靜態(tài)數據
[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優(yōu)化的六點方法的文章就介紹到這了,更多相關nginx優(yōu)化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Ubuntu環(huán)境下安裝部署Nginx詳細步驟(有網)
Nginx是一個開源的?HTTP?網絡服務器,下面這篇文章主要給大家介紹了關于Ubuntu環(huán)境下安裝部署Nginx(有網)的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2024-01-01
Apache和Nginx的優(yōu)缺點詳解_動力節(jié)點Java學院整理
Nginx和Apache一樣,都是HTTP服務器軟件,在功能實現上都采用模塊化結構設計,都支持通用的語言接口。下面通過本文給大家分享Apache和Nginx比較 功能對比,感興趣的朋友參考下吧2017-08-08
解決Nginx 配置 proxy_pass 后 返回404問題
這篇文章主要介紹了Nginx 配置 proxy_pass 后 返回404問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Nginx中l(wèi)imit_req模塊和limit_conn模塊的使用
本文主要介紹了Nginx中l(wèi)imit_req模塊和limit_conn模塊的使用,通過limit_req和limit_conn模塊,可以有效實現精確的請求頻率和連接數控制,下面就來具體介紹一下2024-05-05

