重新在./configure安裝模塊後,模塊會刷新,按照當前的正安裝的模塊來運行!!
一般服務器模塊填寫 ./configure --with-http_ssl_module --with-http_stub_status_module --with-stream
1.如果客戶端訪問服務器提示“Too many open files”如何解決 (併發訪問量過多)
2.如何解決客戶端訪問頭部信息過長的問題
3.如何讓客戶端瀏覽器緩存數據
4.如何自定義返回給客戶端的404錯誤頁面
5.如何查看服務器狀態信息
6.開啓gzip壓縮功能,提高數據傳輸效率
7.如何隱藏nginx header 版本號
八. 服務器內存緩存
9.Nginx安全優化包括:刪除不要的模塊、修改版本信息、限制併發、拒絕非法請求、防止buffer溢出
一,如果客戶端訪問服務器提示“Too many open files”如何解決 (併發訪問量過多)
1,需要優化Nginx的併發量
測試:用ab(httpd-tools)來模擬併發量 訪問192.168.4.5
[proxy] ab -n 2000 -c 2000 http://192.168.4.5/ (斜線必須有的) (模擬2000併發量訪問)
顯示Too many open files (24) 顯示打開文件數量過渡
1.1.lscpu 查看cpu相關信息
解決:修改配置文件,增加併發量性能(1個worker爲多少併發量)
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1; (數字是cpc核數,要根據cpu核數來填寫,cat /proc/cpuinfo | grep process查看多少核數)
......
events {
worker_connections 60000; (每塊cpu處理的量爲多少)(這個爲併發量,剛剛兩千併發超過了1024了。升級高過2000就好)
1.2.打開操作linux系統的內核限制
ulimit -a 查看所有的屬性值
ulimit -Hn 10000 設置硬限制 不可以超過兩萬
ulimit -Sn 10000 設置軟限制 不可以超過來萬 (以上來種限制都是臨時的,永久的在配置文件裏面)
測試ab -n 2000 -c 2000 http://192.168.4.5/
vim /etc/security(安全)/limits(限制).conf
.. ..
* soft nofile 100000
* hard nofile 100000
測試ab -n 2000 -c 2000 http://192.168.4.5/
二,如何解決客戶端訪問頭部信息過長的問題
2.1 寫個腳本測試頭部信息
[root@proxy ~]# cat 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> //提示頭部信息過大
2.2修改Nginx配置文件,增加數據包頭部緩存大小
vim /usr/local/nginx/conf/nginx.conf
寫在http裏面
client_header_buffer_size 1k; //默認請求包頭信息的緩存
large_client_header_buffers 4 4k; //大請求包頭部信息的緩存個數與容量
重新加載並用腳本測試 nginx -s reload ;bash buffer.sh
!!!如果還顯示頭部信息過長,可以把緩存單位調大一點1M
三,如何讓客戶端瀏覽器緩存數據(靜態做測試)
3.1 先查看緩存
about:cache顯示瀏覽器緩存信息
點擊List Cache Entries可以查看詳細信息,記下等會兒做對比
清空瀏覽器本地緩存信息Cookie,緩存,登陸狀態,瀏覽和下載歷史
3.2 修改nginx配置文件,定義對靜態頁面的緩存時間
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天
}
}
3.3 拷貝一張圖片到nginx的頁面裏 cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html
重加載 nginx -s reload
訪問[root@client ~]# firefox http://192.168.2.5/day.jpg
3.4 再次在Firefox地址欄內輸入about:cache,查看本地緩存數據,查看是否有圖片以及過期時間是否正確。
四,如何自定義返回給客戶端的404錯誤頁面
4.1 首先訪問一個不存在的網頁查看404回覆
[root@client ~]# firefox http://192.168.2.5/xxxxx
4.2 修改Nginx配置文件,自定義報錯頁面
vim 裏面 /error 搜索 把後面改寫成 error_page 404 /40x.html;
4.3 在定義的頁面隨便寫入信息 達到訪問後頁面提示效果 vim /usr/local/nginx/html/40x.html
GUN DAN
重加載 nginx -s reload
4.4 測試 [root@client ~]# firefox http://192.168.2.5/xxxxx
常見http狀態碼
五 如何查看nginx服務器狀態信息
5.1 nginx裝 --with-http_stub_status_module 模塊
5.2 書寫配置文件vim /usr/local/nginx/nginx.conf
'''
localtion /status {
stub_status on;
}
...
5.3 重加載 nginx -s reload
查看狀態 curl http://192.168.4.5/status
六. 開啓gzip壓縮功能,提高數據傳輸效率
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裏寫入server_tokens off
http { server_tokens off }
八. 服務器內存緩存
http {
open_file_cache max=2000 inactive=20s;
open_file_cache_valid 60s;
open_file_cache_min_uses 5; (超過5次訪問爲熱點數據,熱點數據會提前緩存 )
open_file_cache_errors off;
//設置服務器最大緩存2000個文件句柄,關閉20秒內無請求的文件句柄
//文件句柄的有效時間是60秒,60秒後過期
//只有訪問次數超過5次會被緩存
}
九.Nginx安全優化包括:刪除不要的模塊、修改版本信息、限制併發、拒絕非法請求、防止buffer溢出。
優化Nginx服務的安全配置
1) 刪除不需要的模塊
Nignx是模塊化設計的軟件,需要什麼功能與模塊以及不需要哪些模塊,都可以在編譯安裝軟件時自定義,使用--with參數可以開啓某些模塊,使用--without可以禁用某些模塊
[root@proxy nginx-1.12]# ./configure \
>--without-http_autoindex_module \ //禁用自動索引文件目錄模塊
>--without-http_ssi_module
[root@proxy nginx-1.12]# make
[root@proxy nginx-1.12]# make install
2)修改顯示在頁面的軟件名nginx,通過如下方法可以修改該信息。
必須cd 到nginx版本目錄下執行執行[root@proxy nginx-1.12]# vim +48 src/http/ngx_http_header_filter_module.c
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
//下面是我們修改後的效果:
static u_char ngx_http_server_string[] = "Server: Jacob" CRLF;
static u_char ngx_http_server_full_string[] = "Server: Jacob" CRLF;
static u_char ngx_http_server_build_string[] = "Server: Jacob" CRLF;
//修改完成後,再去編譯安裝Nignx,版本信息將不再顯示爲Nginx,而是Jacob
[root@proxy nginx-1.12]# ./configure
[root@proxy nginx-1.12]# make && make install
[root@proxy nginx-1.12]# killall nginx
[root@proxy nginx-1.12]# /usr/local/nginx/sbin/nginx //啓動服務
[root@proxy nginx-1.12]# curl -I http://192.168.4.5 //查看版本信息驗證
3) 限制併發量
DDOS攻擊者會發送大量的併發連接,佔用服務器資源(包括連接數、帶寬等),這樣會導致正常用戶處於等待或無法訪問服務器的狀態。
Nginx提供了一個ngx_http_limit_req_module模塊,可以有效降低DDOS攻擊的風險,操作方法如下:
配置文件修改
http{
… …
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
server_name localhost;
limit_req zone=one burst=5;
}
}
//備註說明:
//limit_req_zone語法格式如下:
//limit_req_zone key zone=name:size rate=rate;
//上面案例中是將客戶端IP信息存儲名稱爲one的共享內存,內存空間爲10M
//1M可以存儲8千個IP信息,10M可以存儲8萬個主機連接的狀態,容量可以根據需要任意調整
//每秒中僅接受1個請求,多餘的放入漏斗
//漏斗超過5個則報錯
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
3.1) 拒絕非法的請求
網站使用的是HTTP協議,該協議中定義了很多方法,可以讓用戶連接服務器,獲得需要的資源。但實際應用中一般僅需要get和post。
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
http{
server {
listen 80;
#這裏,!符號表示對正則取反,~符號是正則匹配符號
#如果用戶使用非GET或POST方法訪問網站,則retrun返回444的錯誤信息
if ($request_method !~ ^(GET|POST)$ ) {
return 444;
}
}
}
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
4) 防止buffer溢出
當客戶端連接服務器時,服務器會啓用各種緩存,用來存放連接的狀態信息。
如果攻擊者發送大量的連接請求,而服務器不對緩存做限制的話,內存數據就有可能溢出(空間不足)。
修改Nginx配置文件,調整各種buffer參數,可以有效降低溢出風險。
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
http{
client_body_buffer_size 1K;
client_header_buffer_size 1k;
client_max_body_size 1k;
large_client_header_buffers 2 1k;
… …
}
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload