龙岩天宫山索道多少钱,河南网站排名优化哪家好,邢台网站建设策划,做wordpress 下载站一.基本安全优化
1.隐藏nginx软件版本信息
2.更改源码来隐藏软件名和版本
#xff08;1#xff09;修改第一个文件#xff08;核心头文件#xff09;#xff0c;在nginx安装目录下找到这个文件并修改
#xff08;2#xff09;第二个文件
#xff08;3#xff09;…一.基本安全优化
1.隐藏nginx软件版本信息
2.更改源码来隐藏软件名和版本
1修改第一个文件核心头文件在nginx安装目录下找到这个文件并修改
2第二个文件
3第三个文件内置响应信息页面
4第四个文件
5重新编译安装并重启
3.更改nginx服务的默认用户和组
1在编译安装时指定用户和组
2 更改nginx.conf在nobody处指定
二.修改参数优化服务性能
1.优化nginx的worker进程数并将进程绑定到不同的cpu上
2.优化nginx事件处理模型、最大连接数和进程最大打开文件数
3.开启高效传输模式和上传文件大小的限制
4.配置nginx的gzip压缩
三.优化访问日志
1.运用shell脚本优化nginx的访问日志
2.舍弃不需要的访问日志
3.日志权限设置
四.站点目录和url路径访问优化
1.根据扩展名限制程序和文件访问
2.禁止访问指定目录下的所有文件和目录
1拒绝访问static目录和以static开头路径下的文件
2禁止访问某个目录并返回状态码
3.禁止非法域名解析防用户IP访问网站
1返回501
2永久重写301
五.nginxweb服务防盗链
1.文件名防盗链
2.图片目录防盗链 一.基本安全优化
1.隐藏nginx软件版本信息
一般来讲同一版本号的设备或软件出现漏洞或问题那么这一批次就几乎都有被攻击的风险1通过隐藏版本号等敏感信息在一定成都上可以增强web服务的安全性
隐藏版本使用的是server_tokens off/on;一般放在http块内
[rootlocalhost 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
[rootlocalhost conf]# vim nginx.conf
......
http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server_tokens off;
......
}
[rootlocalhost conf]# systemctl restart nginx.service
[rootlocalhost 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: bytes2.更改源码来隐藏软件名和版本
[rootlocalhost ~]# 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: bytes1修改第一个文件核心头文件在nginx安装目录下找到这个文件并修改
[rootlocalhost core]# pwd
/usr/local/src/nginx-1.22.0/src/core
[rootlocalhost core]# vim nginx.h 更改NGINX_VERSIONNGINX_VERNGINX_VAR为要伪造的版本号和服务名 2第二个文件
[rootlocalhost http]# pwd
/usr/local/src/nginx-1.22.0/src/http
[rootlocalhost http]# vim ngx_http_header_filter_module.c
修改此处 3第三个文件内置响应信息页面
[rootlocalhost http]# pwd
/usr/local/src/nginx-1.22.0/src/http
[rootlocalhost http]# vim ngx_http_special_response.c 修改此处 4第四个文件
[rootlocalhost v2]# pwd
/usr/local/src/nginx-1.22.0/src/http/v2
[rootlocalhost v2]# vim ngx_http_v2_filter_module.c
修改此处 5重新编译安装并重启
[rootlocalhost nginx-1.22.0]# pwd
/usr/local/src/nginx-1.22.0
[rootlocalhost 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 --usernginx --groupnginx --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_SOURCE2 -fexceptions -fstack-protector-strong --paramssp-buffer-size4 -grecord-gcc-switches -m64 -mtunegeneric -fPIC --with-ld-opt-Wl,-z,relro -Wl,-z,now -pie[rootlocalhost nginx-1.22.0]# make[rootlocalhost nginx-1.22.0]# make install[rootlocalhost nginx-1.22.0]# systemctl restart nginx.service[rootlocalhost ~]# 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: bytes3.更改nginx服务的默认用户和组
1在编译安装时指定用户和组
[rootlocalhost nginx-1.22.0]# pwd
/usr/local/src/nginx-1.22.0
[rootlocalhost nginx-1.22.0]# ./configure --usernginx --groupnginx
2 更改nginx.conf在nobody处指定
#user nobody;
worker_processes 1;二.修改参数优化服务性能
1.优化nginx的worker进程数并将进程绑定到不同的cpu上
1一般在nginx.conf文件中是“worker_processes 1;”指定了nginx要开启的进程数可以设置为cpu的核数在面对高并发流量的场合可以进一步设置为cpu核数*2
2nginx的进程多是运行在某个cpu或某个cpu的核内在优化这一硬件资源不均的情况时可以将不同的nginx进程绑定到对应的cpu配置可以在任务管理器的性能中查看cpu核数
例如我的是四核就可以这样来绑定
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
#worker_cpu_affinity可以把不同的进程分给不同的cpu处理上面4个二进制数是掩码代表的是1-4个核对应上面4个进程数这个情况下每个进程会分到一个cpu核处理。
2.优化nginx事件处理模型、最大连接数和进程最大打开文件数
在Linux上使用epoll的I/O多路服用模型最大连接数要根据具体的服务器性能和程序内存来指定一般都放在events块内。最大文件打开数可以查询一般和进程数放一起。
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系统高效工作模式epoll-Linux平台首选高效工作模式rtsig/dev/pollworker_connections 1024;#事件模块指令定义每个进程的最大连接数默认1024max_client客户端连接数worker_processes*worker_connectionsmax_clientsworker_processes*worker_connections/4当然这些设置也受文件系统打开数量限制执行名“ulimit -n 数量”后生效
}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;#设置压缩页面的最小字节建议设置为1K以上小于1K会压缩变大gzip_buffers 4 16k;#压缩缓冲区大小上面的表示是需要4个16k的内存作为压缩结果流缓存gzip_http_version 1.1;#压缩版本用于识别http协议版本默认1.1gzip_comp_level 2;#压缩比率指定gzip压缩比1的压缩比最小处理速度最快9的压缩比最大传输速度快但处理速度最慢较为消耗CPU资源gzip_types text/plain application/x-javascript text/css application/xml;#指定压缩类型gzip_vary on;#开启vary header可以让前端的缓存服务器的缓存经过gzip压缩页面
......
}三.优化访问日志
1.运用shell脚本优化nginx的访问日志
主要功能是将日志以一定的时间格式保存并重载配置在测试完脚本运行无误后可以配合定时任务使得保持访问日志的整洁并且每天生成一个新的日志文件以进行记录
[rootlocalhost nginx-1.22.0]# whereis nginx
nginx: /usr/sbin/nginx.old /usr/sbin/nginx /etc/nginx
[rootlocalhost 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
[rootlocalhost conf]# bash nginx_access_log_mg.sh
[rootlocalhost 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.log2.舍弃不需要的访问日志
舍弃掉一些图片资源的访问记录以免消耗大量磁盘资源
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
[rootlocalhost nginx-1.22.0]# chmod -R 700 logs/
[rootlocalhost nginx-1.22.0]# pwd
/usr/local/src/nginx-1.22.0四.站点目录和url路径访问优化
1.根据扩展名限制程序和文件访问
禁止访问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禁止访问某个目录并返回状态码
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;}
}