目錄
一.基本安全優(yōu)化
1.隱藏nginx軟件版本信息
2.更改源碼來隱藏軟件名和版本
(1)修改第一個文件(核心頭文件),在nginx安裝目錄下找到這個文件并修改
(2)第二個文件
(3)第三個文件,內置響應信息頁面
(4)第四個文件
(5)重新編譯安裝并重啟
3.更改nginx服務的默認用戶和組
(1)在編譯安裝時指定用戶和組
(2) 更改nginx.conf,在nobody處指定
二.修改參數(shù)優(yōu)化服務性能
1.優(yōu)化nginx的worker進程數(shù),并將進程綁定到不同的cpu上
2.優(yōu)化nginx事件處理模型、最大連接數(shù)和進程最大打開文件數(shù)
3.開啟高效傳輸模式和上傳文件大小的限制
4.配置nginx的gzip壓縮
三.優(yōu)化訪問日志
1.運用shell腳本優(yōu)化nginx的訪問日志
2.舍棄不需要的訪問日志
3.日志權限設置
四.站點目錄和url路徑訪問優(yōu)化
1.根據(jù)擴展名限制程序和文件訪問
2.禁止訪問指定目錄下的所有文件和目錄
(1)拒絕訪問static目錄和以static開頭路徑下的文件
(2)禁止訪問某個目錄并返回狀態(tài)碼
3.禁止非法域名解析(防用戶IP)訪問網站
(1)返回501
(2)永久重寫301
五.nginxweb服務防盜鏈
1.文件名防盜鏈
2.圖片目錄防盜鏈
?文章來源地址http://www.zghlxwxcb.cn/news/detail-648291.html
一.基本安全優(yōu)化
1.隱藏nginx軟件版本信息
一般來講,同一版本號的設備或軟件出現(xiàn)漏洞或問題,那么這一批次就幾乎都有被攻擊的風險1,通過隱藏版本號等敏感信息,在一定成都上可以增強web服務的安全性
隱藏版本使用的是server_tokens off/on;,一般放在http塊內
[root@localhost nginx-1.22.0]# curl -I 192.168.2.190
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Sun, 13 Aug 2023 06:36:39 GMT
Content-Type: text/html
Content-Length: 4
Last-Modified: Thu, 10 Aug 2023 03:52:52 GMT
Connection: keep-alive
ETag: "64d45f14-4"
Accept-Ranges: bytes
[root@localhost conf]# vim nginx.conf
......
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server_tokens off;
......
}
[root@localhost conf]# systemctl restart nginx.service
[root@localhost nginx-1.22.0]# curl -I 192.168.2.190
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 13 Aug 2023 06:38:00 GMT
Content-Type: text/html
Content-Length: 4
Last-Modified: Thu, 10 Aug 2023 03:52:52 GMT
Connection: keep-alive
ETag: "64d45f14-4"
Accept-Ranges: bytes
2.更改源碼來隱藏軟件名和版本
[root@localhost ~]# curl -I 192.168.2.190
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Mon, 14 Aug 2023 02:47:15 GMT
Content-Type: text/html
Content-Length: 4
Last-Modified: Thu, 10 Aug 2023 03:52:52 GMT
Connection: keep-alive
ETag: "64d45f14-4"
Accept-Ranges: bytes
(1)修改第一個文件(核心頭文件),在nginx安裝目錄下找到這個文件并修改
[root@localhost core]# pwd
/usr/local/src/nginx-1.22.0/src/core
[root@localhost core]# vim nginx.h
更改NGINX_VERSION,NGINX_VER,NGINX_VAR為要偽造的版本號和服務名
(2)第二個文件
[root@localhost http]# pwd
/usr/local/src/nginx-1.22.0/src/http
[root@localhost http]# vim ngx_http_header_filter_module.c
修改此處
(3)第三個文件,內置響應信息頁面
[root@localhost http]# pwd
/usr/local/src/nginx-1.22.0/src/http
[root@localhost http]# vim ngx_http_special_response.c
?文章來源:http://www.zghlxwxcb.cn/news/detail-648291.html
?修改此處
(4)第四個文件
[root@localhost v2]# pwd
/usr/local/src/nginx-1.22.0/src/http/v2
[root@localhost v2]# vim ngx_http_v2_filter_module.c
修改此處?
(5)重新編譯安裝并重啟
[root@localhost nginx-1.22.0]# pwd
/usr/local/src/nginx-1.22.0
[root@localhost nginx-1.22.0]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@localhost nginx-1.22.0]# make
[root@localhost nginx-1.22.0]# make install
[root@localhost nginx-1.22.0]# systemctl restart nginx.service
[root@localhost ~]# curl -I 192.168.2.190
HTTP/1.1 200 OK
Server: rpcbind/2.3.30
Date: Mon, 14 Aug 2023 02:47:23 GMT
Content-Type: text/html
Content-Length: 4
Last-Modified: Thu, 10 Aug 2023 03:52:52 GMT
Connection: keep-alive
ETag: "64d45f14-4"
Accept-Ranges: bytes
3.更改nginx服務的默認用戶和組
(1)在編譯安裝時指定用戶和組
[root@localhost nginx-1.22.0]# pwd
/usr/local/src/nginx-1.22.0
[root@localhost nginx-1.22.0]# ./configure --user=nginx --group=nginx
(2) 更改nginx.conf,在nobody處指定
#user nobody;
worker_processes 1;
?
二.修改參數(shù)優(yōu)化服務性能
1.優(yōu)化nginx的worker進程數(shù),并將進程綁定到不同的cpu上
(1)一般在nginx.conf文件中是“worker_processes 1;”,指定了nginx要開啟的進程數(shù),可以設置為cpu的核數(shù),在面對高并發(fā)流量的場合可以進一步設置為cpu核數(shù)*2
(2)nginx的進程多是運行在某個cpu或某個cpu的核內,在優(yōu)化這一硬件資源不均的情況時可以將不同的nginx進程綁定到對應的cpu配置,可以在任務管理器的性能中查看cpu核數(shù)
例如我的是四核就可以這樣來綁定
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
#worker_cpu_affinity可以把不同的進程分給不同的cpu處理,上面4個二進制數(shù)是掩碼,代表的是1-4個核,對應上面4個進程數(shù),這個情況下每個進程會分到一個cpu核處理。
2.優(yōu)化nginx事件處理模型、最大連接數(shù)和進程最大打開文件數(shù)
在Linux上使用epoll的I/O多路服用模型,最大連接數(shù)要根據(jù)具體的服務器性能和程序內存來指定,一般都放在events塊內。最大文件打開數(shù)可以查詢,一般和進程數(shù)放一起。
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 65535;
#可以設置為這個“ulimit -HSn”命令得出來的結果
events {
use epoll;
#事件模塊指令,放在events中指定nginx的工作模式,select-標準工作模式,poll-標準工作模式,kqueue-BDS系統(tǒng)高效工作模式,epoll-Linux平臺首選高效工作模式,rtsig,/dev/poll
worker_connections 1024;
#事件模塊指令,定義每個進程的最大連接數(shù),默認1024,max_client客戶端連接數(shù)=worker_processes*worker_connections,max_clients=worker_processes*worker_connections/4,當然這些設置也受文件系統(tǒng)打開數(shù)量限制,執(zhí)行名“ulimit -n 數(shù)量”后生效
}
3.開啟高效傳輸模式和上傳文件大小的限制
sendfile可以開啟文件的高效傳輸模式,如果將tcp_nopush和tcp_nodelay都設置為on,可以有效防止網絡和磁盤的I/O阻塞,提升效率,可以通過client_max_body_size指定最大上傳文件大小,一般都放在http塊中
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_max_body_size 20M;
......
}
4.配置nginx的gzip壓縮
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
#開啟壓縮
gzip_min_length 1k;
#設置壓縮頁面的最小字節(jié),建議設置為1K以上,小于1K會壓縮變大
gzip_buffers 4 16k;
#壓縮緩沖區(qū)大小,上面的表示是需要4個16k的內存作為壓縮結果流緩存
gzip_http_version 1.1;
#壓縮版本,用于識別http協(xié)議版本,默認1.1
gzip_comp_level 2;
#壓縮比率,指定gzip壓縮比,1的壓縮比最小處理速度最快,9的壓縮比最大傳輸速度快但處理速度最慢,較為消耗CPU資源
gzip_types text/plain application/x-javascript text/css application/xml;
#指定壓縮類型
gzip_vary on;
#開啟vary header,可以讓前端的緩存服務器的緩存經過gzip壓縮頁面
......
}
三.優(yōu)化訪問日志
1.運用shell腳本優(yōu)化nginx的訪問日志
主要功能是將日志以一定的時間格式保存并重載配置,在測試完腳本運行無誤后,可以配合定時任務使得保持訪問日志的整潔,并且每天生成一個新的日志文件以進行記錄
[root@localhost nginx-1.22.0]# whereis nginx
nginx: /usr/sbin/nginx.old /usr/sbin/nginx /etc/nginx
[root@localhost conf]# vim nginx_access_log_mg.sh
#!/bin/bash
cd /usr/local/src/nginx-1.22.0/logs &&\
/bin/mv access.log access_$(date +%F).log
/usr/sbin/nginx -s reload
[root@localhost conf]# bash nginx_access_log_mg.sh
[root@localhost logs]# ll
total 8
-rw-r--r-- 1 root root 1680 Aug 14 13:32 access_2023-08-14.log
-rw-r--r-- 1 root root 0 Aug 14 13:54 access.log
-rw-r--r-- 1 root root 1108 Aug 14 13:32 error.log
2.舍棄不需要的訪問日志
舍棄掉一些圖片資源的訪問記錄,以免消耗大量磁盤資源
server {
listen 192.168.2.190;
server_name www.aabb.com;
location / {
root /usr/local/src/nginx-1.22.0/html/ip;
index index.html index.htm;
}
location ~ .*\.(jpg|JPG|jpeg|JPEG|gif|GIF)$ {
access_log off;
}
}
3.日志權限設置
建議遞歸設置為700
[root@localhost nginx-1.22.0]# chmod -R 700 logs/
[root@localhost nginx-1.22.0]# pwd
/usr/local/src/nginx-1.22.0
四.站點目錄和url路徑訪問優(yōu)化
1.根據(jù)擴展名限制程序和文件訪問
禁止訪問images開頭并php/php5/sh/pl/py結尾,static開頭并以php/php5/sh/pl/py結尾,/data/attachment或/data/avatar開頭以php/php5結尾的文件類型的內容
server {
listen 192.168.2.190;
server_name www.aabb.com;
location / {
root /usr/local/src/nginx-1.22.0/html/ip;
index index.html index.htm;
}
location ~ .*\.(jpg|JPG|jpeg|JPEG|gif|GIF)$ {
access_log off;
}
location ~ ^/images/.*\.(php|php5|sh|pl|py)$ {
deny all ;
}
location ~ ^/static/.*\.(php|php5|sh|pl|py)$ {
deny all;
}
location ~* ^/data/(attachment|avatar)/.*\.(php|php5)$ {
deny all;
}
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
}
2.禁止訪問指定目錄下的所有文件和目錄
(1)拒絕訪問static目錄和以static開頭路徑下的文件
location ~ ^/(static)/ {
deny all;
}
location ~ ^/static {
deny all;
}
(2)禁止訪問某個目錄并返回狀態(tài)碼
location /xxx/ {
return 404;
}
location /xxx/ {
return 403;
}
3.禁止非法域名解析(防用戶IP)訪問網站
(1)返回501
server {
listen 80 default_server;
server_name _;
return 501;
}
(2)永久重寫301
server {
listen 80 default_server;
server_name _;
rewrite ^(.*) http://xxx/$1 permanent;
}
#惡意域名解析IP
server {
if ($host !~ ^www/xxx/.com$){
rewrite ^(.*) http//www.xxx.com$l permanent;
}
五.nginxweb服務防盜鏈
1.文件名防盜鏈
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked xxx.com ;
#valid_referers可以設置允許訪問資源的引用者列表,none表示不允許任何引用這直接訪問,blocked表示拒絕所有引用者,允許xxx.com域名下的引用者訪問資源
if ($invalid_referer) {
rewrite ^/ http://xxx.com/retrun.html;
#如果引用者不在允許列表中,請求將會被重寫到http://xxx.com/retrun.html,引用者在允許列表中則返回請求的資源,如上配置,xxx.com域名下的引用者可以獲取資源
#return 404;
}
}
2.圖片目錄防盜鏈
location /images/ { #匹配/images、開頭的請求
valid_referers none blocked server_names xxx;
#none,blocked含義同文件名防盜鏈,server_names后加上允許的域名列表
if ($invalid_referer) { #如上配置,引用者不在允許域名列表中則403拒絕訪問,在列表中則則返回其請求的資源
return 403;
}
}
?
?
?
?
?
?
到了這里,關于nginx上web服務的基本安全優(yōu)化、服務性能優(yōu)化、訪問日志優(yōu)化、目錄資源優(yōu)化和防盜鏈配置簡介的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!