【主機運維】nginx優化的六點方法
這篇文章主要介紹了nginx優化的六點方法,有對nginx優化不太熟悉的同學可以參考下
一.優化Nginx并發量
修改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) //提示打開文件數量過多
- [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數據包頭緩存
修改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> //提示頭部信息過大
- [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次會被緩存
- }
六.瀏覽器本地緩存靜態數據
到此這篇關于nginx優化的六點方法的文章就介紹到這了,更多相關nginx優化內容請搜索云狐網以前的文章或繼續瀏覽下面的相關文章
- [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
點個贊! ()
更多相關
相關文章
- linux的目錄結構;Linux系統各個目錄的作用介紹
- mssql2000數據庫通過附加數據庫轉到SQL2008(圖文)
- Win2012配置asp網站(圖文教程)
- win2008 r2系統iis7中如何設置ip限制(圖文)
- IIS7 支持html頁面包含(include)html頁面 IIS設置與代碼編寫
- Win2003+IIS6.0+php5.2.2+MySQL 5.0.41+phpMyAdmin 2.10.1環境配置安裝
- 網站采集入庫502 Bad Gateway錯誤時,修改PHP 配置修改參考
- 出錯了,[騰訊云COSFS]運行時發生錯誤!qcloud_cos.cos_exception.CosClientError: SecretId and SecretKey is Required!
- 騰訊Linux云主機快速掛載云盤命令,設置開機自動掛載詳細圖文教程
- 騰訊輕量云主機win2012系統掛載SSD云硬盤操作步驟(圖文)