nginx優化
- 一、自定義報錯頁面?
- 二、優化Nginx并發量
- 三、優化Nginx服務的安全配置
- 1, 修改版本資訊,并隱藏具體的版本號
- 2, 拒絕非法的請求
- 四、撰寫日志切割腳本
- 五、Nginx的一些模塊
一、自定義報錯頁面?
優化前,客戶端使用瀏覽器訪問不存在的頁面,會提示404檔案未找到

操作步驟:
1,vim打開組態檔,修改error_page行
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
47 error_page 404 /404.jpg;
2,網上下載一張圖片,命名為404.jpg放到下面的位置
[root@nginx html]# pwd
/usr/local/nginx/html
[root@nginx html]# ls
404.jpg 50x.html index.html
優化后,客戶端使用瀏覽器訪問不存在的頁面,會提示自己定義的404頁面

二、優化Nginx并發量
優化前使用ab高并發測驗
[root@nginx html]# ab -n 2000 -c 2000 http://192.168.4.100/
Benchmarking 192.168.4.100 (be patient)
socket: Too many open files (24)
修改Nginx組態檔,增加并發量
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
… …
worker_processes 2; //與CPU核心數量一致
events {
worker_connections 65535; //每個worker最大并發連接數
}
… …
優化Linux內核引數(最大檔案數量)
除了nginx本身對并發量有限制,linux系統本身對檔案的訪問也有限制,默認情況下linux系統僅允許一個檔案同時被打開1024次,
臨時修改限制,修改為支持10萬
[root@nginx html]# ulimit -Hn 100000
[root@nginx html]# ulimit -Sn 100000
測驗
[root@nginx html]# ab -n 2000 -c 2000 http://192.168.4.100/
Percentage of the requests served within a certain time (ms)
50% 10
66% 19
75% 39
80% 40
90% 55
95% 62
98% 62
99% 62
100% 239 (longest request)
三、優化Nginx服務的安全配置
1, 修改版本資訊,并隱藏具體的版本號
默認Nginx會顯示版本資訊以及具體的版本號,這些資訊給攻擊者帶來了便利性,便于他們找到具體版本的漏洞,
優化前:
[root@nginx html]# curl -I http://192.168.4.100
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 28 Dec 2020 16:47:23 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 28 Dec 2020 15:42:55 GMT
Connection: keep-alive
ETag: "5fe9fcff-264"
Accept-Ranges: bytes
優化:
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
… …
http{
server_tokens off; #在http下面手動添加這么一行
… …
}
優化后
[root@nginx html]# curl -I http://192.168.4.100
HTTP/1.1 200 OK
Server: nginx
2, 拒絕非法的請求
網站使用的是HTTP協議,該協議中定義了很多方法,可以讓用戶連接服務器,獲得需要的資源,但實際應用中一般僅需要get和post,
優化前:GET和HEAD都可以
[root@nginx ~]# curl -i -X GET http://192.168.4.100
HTTP/1.1 200 OK
Server: nginx
[root@nginx ~]# curl -i -X HEAD http://192.168.4.100
HTTP/1.1 200 OK
Server: nginx
優化:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
在server{}里面加一個if陳述句
if ($request_method !~ ^(GET|POST)$ ) {
return 404;
}
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload
優化后:HEAD請求出錯
[root@nginx ~]# curl -i -X GET http://192.168.4.100
HTTP/1.1 200 OK
Server: nginx
[root@nginx ~]# curl -i -X HEAD http://192.168.4.100
HTTP/1.1 404 Not Found
Server: nginx
四、撰寫日志切割腳本
1.撰寫腳本
[root@nginx ~]# 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)
2.創建計劃任務
crontab -e
03 03 * * 5 /usr/local/nginx/logbak.sh
計劃任務可以隨便寫
五、Nginx的一些模塊
ngx_http_core_module(核心模塊,包含http、server_name、root等配置引數)
ngx_http_access_module(訪問控制模塊,包含allow和deny配置引數)
ngx_http_auth_basic_module(用戶認證模塊,包含auth_basic等配置引數)
ngx_http_charset_module(字符集模塊,包含charset utf8等配置引數)
ngx_http_fastcgi_module(fastcgi模塊,包含fastcgi_pass等配置引數)
ngx_http_gzip_module(壓縮模塊,包含gzip、gzip_type等配置引數)
ngx_http_limit_conn_module(限制并發量模塊,包含limit_conn等引數)
ngx_http_log_module(日志模塊,包含access_log等配置引數)
ngx_http_proxy_module(代理模塊,包含proxy_pass等配置引數)
ngx_http_rewrite_module(地址重寫模塊,包含rewrite、break、last等配置引數)
ngx_http_ssl_module(加密模塊,包含ssl_certificate、ssl_certificate_key等引數)
ngx_http_stub_status_module(狀態模塊,包含stub_status配置引數)
ngx_http_upstream_module(調度器模塊,包含upstream、hash、ip_hash等配置引數)
ngx_stream_core_module(4層代理模塊)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/241940.html
標籤:其他
