29人参与 • 2025-05-07 • https
[root@localhost ^]# dnf install -y gcc make pcre-deve1 zlib-developenssl-devel perl-extutils-makemaker git wget tar
[root@localhost~]# useradd -m -s /sbin/nologin nginx [root@localhost~]# mkdir-p/var/log/nginx [root@localhost~]# chown -r nginx:nginx /var/log/nginx
[root@localhost ^]# tar zxf nginx-1.26.3.tar.gz [root@localhost ^]# cd nginx-1.26.3 [root@localhost nginx-1.26.3]# ./configure\ --prefix=/usr/local/nginx --user=nginx\ --group=nginx --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module\ --with-http_gzip_static_module \ --with-pcre\ --with-stream [root@localhost nginx-1.26.3]# make & make install
为主程序 nginx创建链接文件
[root@localhost nginx-1.26.3]# in -s /usr/local/nginx/sbin/nginx/usr/local/sbin/
[root@localhost ^]# vi /lib/systemd/system/nginx.service [unit] description=the nginx http and reverse proxy server after=network.target [service] type=forking execstartpre=/usr/local/sbin/nginx -t execstart=/usr/local/sbin/nginx execreload=/usr/local/sbin/nginx -s reload execstop=/bin/kill -s quit $mainpid timeoutstopsec=5 killmode=process privatetmp=true user=root group=root [instal1] wantedby=multi-user.target [root@localhost^]# systemctl daemon-reload [root@localhost~]# systemctl start nginx [root@localhost^]# systemctl enable nginx
在生产环境中,需要隐藏nginx的版本号,以避免泄漏nginx白的版本,使攻击者不能针对特定版本进行攻击。
[root@localhost ~]# curl -i 192.168.10.101 http/1.1 200 ok server: nginx/1.26.3
修改配置文件
[root@localhost^]# vi /usr/local/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; server_tokens off; ....... [root@localhost^]#nginx -t [root@localhost~]#nginx -s reload [root@localhost~]# curl -i 192.168.10.101 http/1.1 200 ok server: nginx
不安全的请求方式,是潜在的安全风险,trace(易引发xst攻击)、put/delete(文件修改风险)、connect(代理滥用),通过正则表达式匹配请求方法,非白名单方法返回444(无响应关闭连接)
修改配置文件
[root@localhost ^]# vi /usr/local/nginx/conf/nginx.conf server { ($request_method !~ (get|head|post)$) { return 444; } }
验证
[root@localhost ^]# curl -xput -i 192.168.10.10] curl: (52) empty reply from server
cc攻击(challenge collapsar攻击)是一种常见的网络j攻击方式,通过大量合法或伪造的小流量请求来耗尽服务器资源,导致正常用户无去访问网站。要在nginx中有效防止cc攻击,可以采用多种策略和方法
cc攻击,也称为连接数攻击或请求速率限制攻击,通过模拟大量用户访问来消耗服务器资源,从而使得正常用户无法正常访问网站。为了防止此类攻击,可以使用nginx提供的模块来限制请求速率和并发连接数
[root@localhost ^]# vi /usr/local/nginx/conf/nginx.conf http { limit_req_zone_$binary_remote_addr_zone=req_limit:10m rate=10r/s; server { location / { root html; index index.html index.php; limit_req zone=req_limit burst=20 nodelay; } } }
安装ab测试工具
[root@localhost ^]# dnf install httpd-tools -y [root@localhost~]# ab -n 300 -c 30 http://192.168.10.101/ [root@localhost ^]# tail -300 /usr/local/nginx/logs/access. log | grep -c 503 279
防盗链是一种重要的安全设置,旨在防止未经授权的用户盗用网站(静态)资源。盗链行为不仅侵犯了内容创作者的版权,还可能导致原网站带宽和资源的过度消耗,影响正常用户的访问速度和体验。
修改 windows 的 c:\windows\system32 drivers\etc\hossts文件,设置域名和ip映射关系
192.168.10.101 www.aaa.com
192.168.10.102 www.bbb.com
修改两台openeuler的hosts文件,设置域名和ip映射关系。
192.168.10.101 www.aaa.com
192.168.10.102www.bbb.com
把图片kgc.png放到源主机(www.aaa.com的工作目录下
[root@localhost ^]# ls /usr/local/nginx/html index. html kgc.png
编辑原网站首页文件
[root@localhost ^]# vi /usr/local/nginx/html/index.html <html> <body> <h1>aaa it work!</h1> <img src="kgc.png"/> <body> </html>
使用浏览器访问进行验证即可
编辑盗链网站首页文件
[root@localhost ^]# dnf -y install httpd [root@localhost~]# vi /usr/local/nginx/html/index.html <html <body> <h1>bbb it work! </hl> <img src="http://www.aaa.com/kgc.png </body> </html> [root@localhost~]# systemctl stop firewalld [root@localhost`]# systemctl start httpd
测试访问盗链网站(使用浏览器)
配置nginx防盗链
[root@localhost ^]# vi /usr/local/nginx/conf/nginx.conf location ^* \. (gif|jpg|jpeg|png|bmp|swf|flv|mp4|webp|ico)s{ root html; valid_referers aaa.com *.aaa.com; if($invalid referer){ return 403; } } [root@localhost^]#nginx -t [root@localhost~]#nginx -s reload
测试访问盗链网站(盗链失败403)
动态黑名单是nginx中一种实时拦截恶意请求的安全机制,它允许在不重启服务的情况下,动态更新需要封禁的ip地址或网段。
相比静态配置的allow/deny指令,动态黑名单更灵活高效,适用于高并发、多变的攻击击防护场景。
编辑黑名单配置文件
[root@localhost ^]# vi /usr/local/nginx/conf/blockips.conf 192.168.1.0/24 1; 192.168.10.102 1;
编辑主配置文件
[root@localhost^]# vi /usr/local/nginx/conf/nginx.conf http { geo $block_ip { default 0; finclude /usr/local/nginx/conf/blockips.conf; } server if ($block_ip) { return 403; } } }
[root@localhost^]# nginx -t [root@localhost~]# nginx -s reload
使用封禁ip测试访问
[root@localhost ~]# curl 192.168.10.101 <html> <head><title>403 forbidden</title></head> <body> <center><h1>403 forbidden</h1></center> <hr><center>nginx</center> </body> </html>
自动添加黑名单
#!/bin/bash #自动封禁访问超过100次的ip awk '{print$1}'/var/log/nginx/access.log|sort|uniq -cəssort -nr | awk'{if($1>100) print $2"1;"}' >/usr/local/nginx/conf/bllockips.conf
众所周知,http(超文本传输协议)是客户端浏览器与web服务器之间的通信协议,而https协议可以认为是http+ssl/tls,在http之下tcp之上加了ss1一层,用于对应用层数据的加解密。
如下所示:
http
ssl
/tsl
tcp
ip
http由于是明文传输,主要存在三大风险:窃 听风险、篡改区险、冒充风险。
通信需要包括以下四个原则:机密性、完整性,身份认证和不可否认。
既然http是明文传输的,那我们给报文加密不就行了,既然要加密,我们肯定需要通信双方协商好密钥吧。一种是通信双方使用同一把密钥,即对称加密的方式来给报文进行加解密。
非对称加密即加解密双方使用不同的密钥,一把作为公钥,可以公开的,把作为私钥,不能公开,公钥加密的密文只有私钥可以解密,私钥签名的内容,也只有公钥可以验签。
数字证书,解决公钥传输信任问题
证书是由站点管理者向ca申请,申请的时候会提交域名、组织单位信息和公钥等数据(这些数据组成了certificate signing request证书签名请求简称csr),ca会根据这些信息生成证书
server传输ca颁发的证书给client,client收到证书后使用系统内置的ca证书的公钥来验签,验签通过证明证书是受信任的,证书受信任那么证书中的公钥也就是受信任的,这样的话就解决了公钥传输过程中被调包的风险。
由于ssl证书需要向ca组织申购,实验采用自签名证书(也就是自己给自己签名并颁发证书,当然这种证书是不被信任的)
创建证书存储目录
[root@localhost ^]# mkdir -p /etc/nginx/ssl
生成自签名证书
[root@localhost ^]# openssl req -x509 -nodes -days :365 -newkey rsa:2048 -keyout/etc/nginx/ssl/nginx-selfsigned.key -out/etc/nginx/ssl/nginx-selfsigned.crt\ -subj "/c=cn/st=beijing/l=beijing/0=myorg/cn=localhost
ps:
ca签名证书:
需要由受信任的第三方证书颁发机构(ca)签发。
流程如下:
自签名证书:
编辑nginx配置文件
[root@localhost ~]#vi /usr/local/nginx/conf/nginx.conf server { listen 443 ssl; #监听 https 端口 server_namelocalhost; #域名或ip #指定证书和私钥路径 ssl_certificate /etc/nginx/ssl/nginx-selfsigned.crt; /etc/nginx/ssl/nginx-selfsigned.key; issl_certificate_key server { listen 80; server_name localhost; return 301 https://$host$request_uri; }
[root@localhost ^]#nginx -t [root@localhost ^]#nginx -s reload
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论