当前位置: 首页> 汽车> 维修 > Nginx优化与防盗链

Nginx优化与防盗链

时间:2025/8/23 19:57:16来源:https://blog.csdn.net/2301_82109773/article/details/139420587 浏览次数: 0次

nginx优化:

nginx应用配置文件的优化:

 nginx的性能优化:
开启网页压缩gzip on;
页面缓存expires 缓存时间;
连接保持超时keepalive_timeout  服务端超时时间  客户端超时时间;
设置工作进程数work_processes  与服务器CPU数量相同或auto
设置工作进程连接数worker_connections    worker_rlimit_nofile
工作进程静态绑核worker_cpu_affinity
开启高效文件传输模式sendfile on;   tcp_nopush on;    tcp_nodelay on;
IO多路复用use epoll;
连接优化multi_accept on;(一个进程同时接受多个网络连接)    accept_mutex on;(以串行方式接入新连接,防止惊群现象发生)

  nginx的安全优化:       
隐藏版本号server_tokens off;      修改源代码 nginx.h
防盗链valid_referers   if ($invalid_referer) {rewrite ....}  rewrite地址重写
访问控制deny/allow
设置运行用户/组user 用户名 组名;
限制请求数limit_req_zone   limit_req
限制连接数limit_conn_zone  limit_conn
 日志分割

shell脚本 + crontab 


系统内核优化:

/etc/sysctl.conf         

内核参数配置文件

#用于解决系统存在大量TIME WAIT状态连接的问题
net.ipv4.tcp_syncookies=1表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击
net.ipv4.tcp_tw_reuse=1表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接
net.ipv4.tcp_tw_recycle=1表示开启TCP连接中TIME-WAIT sockets的快速回收
net.ipv4.tcp_fin_timeout=30修改MSL值,系统默认的TIMEOUT时间

#如果连接数本身就很多,可再优化TCP的可用端口范围,进一步提升服务器的并发能力       

net.ipv4.tcp_keepalive_time=1200#当keepalive启用时,TCP发送keepalive探测消息的频率,确认客户端是否断网
net.ipv4.ip_local_port_range=1024 65535#用于向外连接的端口范围。缺省情况下很小,为32768 60999
net.ipv4.tcp_max_syn_backlog=8192#SYN队列长度,默认为1024,加大队列长度为8192,可容纳更多等待连接的网络连接数
net.ipv4.tcp_max_tw_buckets=5000#表示系统同时保持TIME WAIT的最大数量
net.core.somaxconn=65535#一个端口能够监听的最大连接数

#如果需要IP路由转发 net.ipv4.ip_forward=1


/etc/security/limits.conf   内核资源限制文件

* softnoproc65535打开系统进程数
*hardnoproc65535
*soft  nofile 65535进程打开文件数
*hardnofile65535

                    

你用过哪些nginx模块?

http_stub_status_module访问状态统计模块
http_gzip_module  网页压缩模块
http_rewrite_moduleURL地址重写模块
http_ssl_modulehttps安全加密模块
http_auth_basic_module网页用户认证模块
http_fastcgi_modulefastcgi转发模块
http_image_filter_module图片处理模块
http_mp4/flv_modulemp4/flv视频格式模块
http_limit_req_module限制请求数模块
http_limit_conn_module限制连接数模块
http_proxy_module代理转发模块
http_upstream_*_module 负载均衡模块
stream   四层代理转发模块

   
           

配置Nginx隐藏版本号

  • 隐藏Nginx版本号,避免安全漏洞泄露
  • Nginx隐藏版本号的方法
#####################  快速配置一台nginx服务器   #######################systemctl stop firewalld.service 
setenforce 0df
mount /dev/sr0 /mnt
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ makeuseradd -M -s /sbin/nologin nginx
cat /etc/passwd | grep nginx:cd /opt/[root@l1 opt]# ls
nginx-1.26.0.tar.gz  rhtar xf nginx-1.26.0.tar.gz cd nginx-1.26.0/./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_modulemake -j2 && make installcd /usr/local/nginx/ls sbin/
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx -tcd /usr/lib/systemd/systemvim nginx.service——————————————————————————————————————————————————————————————————————————————————[Unit]
Description=nginx
After=network.target[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target——————————————————————————————————————————————————————————————————————————————————systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
netstat -lntp | grep nginx

 修改配置文件法   
                   

关键字:Nginx优化与防盗链

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: