Nginx Web 服务优化实战

5 篇文章 0 订阅

web 优化一览总结表

安全优化

1. 隐藏 nginx 版本信息优化
	修改 nginx 配置文件实现优化
	server_tokens off;
2. 修改 nginx 版本信息优化修改 nginx 源码配置文件
	nginx-xxx/src/core/nginx.h
	nginx-xxx/src/http/ngx_http_header_filter_module.c
	nginx-xxx/src/http/ngx_http_special_response.c
3. 修改 nginx 软件 work_processes 进程用户信息
	修改 nginx 默认用户,利用配置文件参数实现
	修改 nginx 默认用户,采用编译参数方式实现
	user www www;
4. 优化 nginx 服务上传文件限制 
  	client_max_body_size 设置客户端请求报文主体最大尺寸
5. Nginx 图片及目录防盗链解决方案
	根据 HTTP referer 实现防盗链
	Nginx 图片及目录防盗链解决方案
	用户从哪里跳转过来的(通过域名)
	根据 cookie 防盗链
6 .cookie 和 session 区别?
	通过加密变换访问路径实现防盗链
	在产品设计上解决盗链方案
7. Nginx 站点目录文件及目录权限优化
	只将用户上传数据的目录设置为 755,用户和组使用 nginx
	其余目录和文件为 755/644,用户和组使用 root
8. Nginx 防爬虫优化 利用 robots.txt 机器人协议防止爬虫(君子协议)
	在网站站点目录下面,放 robots.txt 文件
	利用$http_user_agent 变量阻止爬虫代理访问(2 种方法)
9.Nginx 防爬虫优化(spider|bot)
	利用程序开发验证码信息,阻止进行爬虫

性能优化

1. 修改 nginx 软件 work_processes进程数量
	woker_processes 8
	一般和 CPU 的核数设置一致;高并发可以和 CPU 核数 2 倍
	lscpu
	/proc/cpuinfo
	top 按 1
	1 个核心处理 1 件事情
	Intel 超线程技术
	1 个核心处理 2 件事情
2. 优化 nginx 服务进程均匀分配到不同 CPU 进行处理
	利用 worker_cpu_affinity 进行优化(cpu 亲和力)
	4 颗 CPU 优化配置参数为 0001 0010 0100 1000
	2 颗 CPU 优化配置参数为 0101 1010
	worker_cpu_affinity 0101 1010;
	worker_cpu_affinity auto;
	Linux 系统设置 cpu 亲和力 taskset
3. 优化 nginx 事件处理模型 
	利用 use epoll 参数修改事件模型为 epoll 模型事件模型指定配置参数放置在 event 区块中
4. 优化 nginx 单进程客户端连接数 利用 worker_connections 连接参数进行调整
	用户最大并发连接数=worker 进程数*worker 连接数
5. 优化 nginx 服务进程打开文件数 
 	利用 worker_rlimit_nofile 参数进行调整(65535)
6. 优化 nginx 服务数据高效传输模式
	利用 sendfile on		开启高效传输模式
	tcp_nopush on		表示将数据积攒到一定的量再进行传输
	tcp_nodelay on 		表示将数据信息进行快速传输
7. 优化 nginx 服务超时信息 keepalive_timeout 优化客户端访问 nginx 服务端超时时间
	client_header_timeout 优化服务端读请求头超时时间
	client_body_timeout 优化两个请求主体发送间隔超时时间
	send_timeout 优化两个响应信息的间隔超时时间
8. 优化 nginx 服务与 FastCGI 连接缓存与缓冲信息
	优化利用 FastCGI 与 PHP 连接缓冲信息
	优化利用 FastCGI 与 PHP 连接缓存信息
9. 配置 Nginx gzip 压缩实现性能优化(下面有案例)
	利用 gzip 命令进行对数据信息压缩优化
	从而节省网站带宽资源 gzip 配置
10. 配置 Nginx expires 实现让客户端缓存数据
	利用 location 匹配相应要缓存的信息,利用 expires 参数结
	合时间信息进行缓存。
	把数据缓存到浏览器中.
	expires 客户端缓存图片或静态资源设置

日志优化

1.配置 Nginx 服务相关日志操作 进行日志文件轮询切割
部分日志内容不进行记录,节省系统磁盘空间
对日志文件进行授权
#logrotate 日志切割工具 + 系统定时任务
kill -USR1 `cat /var/run/nginx.pid`

Nginx 站点目录及文件 URL 访问控制

在动态解析配置前面,设置限制特定目录下扩展名文件解析
限制指定目录 uri 信息访问,利用禁止策略和返回错误状态码
限制用户访问,利用白名单和黑名单方式
禁止非法域名解析访问企业网站

感知优化

1. Nginx 错误页面的优雅显示 对错误代码实行本地页面跳转
优雅显示错误页面放到本地单独目录下,进行优雅显示
改变状态码为新的状态码,并显示指定的文件内容
错误状态码 URL 重定向
将错误状态码重定向到一个 location

全面优化

1. 利用 Nginx 限制请求访问
 	 利用$request_method 限制请求方法
2. Nginx 网站 CDN 加速优化
3. Nginx 程序架构优化
	  利用程序或反向代理实现架构访问分离解耦优化
4. 使用普通用户启动 Nginx(nginx监牢模式)
	利用 nginx –c 参数启动 nginx 多实例,使 master 进程让普通用户管理
	普通用户无法使用 1-1024 端口
5. 控制 nginx 并发连接数 利用 limit_conn_zone 参数和$binary_remote_addr 变量限
	制 nginx 单 IP 地址并发连接数
	利用 limit_conn_zone 参数和$server_name r 变量限制
6 .nginx 虚拟主机总连接数

fastcgi buffer 与 cache在这里插入图片描述

gzip 配置
 gzip on;
 gzip_min_length 1k; #设置大于 1K 才进行压缩
 gzip_buffers 4 16k; #设置压缩缓存
 #gzip_http_version 1.0;
 gzip_comp_level 2; #压缩级别 数字越大 压缩率(占用空间)越小 占用 CPU 越多
 gzip_types text/plain application/x-javascript text/css application/xml text/javascript
application/x-httpd-php ; #哪些类型的文件 需要进行压缩 这些类型需用 mime type 媒体类型.
#mime types #媒体类型(http) === 文件类型(linux)

故障案例:

	NSES WITH THE “TEXT/HTML” TYPE ARE ALWAYS COMPRESSED.
	GZIP 默认压缩 TEXT/HTML 类型,不用指定,指定会报错。
	nginx: [warn] duplicate MIME type "text/html" in /app/nginx-1.14.0//conf/nginx.conf:13
expires 客户端缓存图片或静态资源设置
	location ~* \.(gif|jpg|jpeg|png|bmp|ico)$ {
	 root /var/www/img/;
	 expires 30d;
	 }
配置 Nginx 服务相关日志操作
	[root@web01 nginx]# #定时任务+logrotate
	[root@web01 nginx]# cat /etc/cron.daily/
	logrotate man-db.cron
	[root@web01 nginx]# cat /etc/cron.daily/logrotate
	[root@web01 nginx]# cat /etc/cron.daily/logrotate
	#!/bin/sh
	/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
	EXITVALUE=$?
	if [ $EXITVALUE != 0 ]; then
	 /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
	fi
	exit 0
	[root@web01 nginx]# cat /etc/logrotate.d/
	bootlog mariadb php-fpm wpa_supplicant zabbix-agent
	httpd nginx syslog yum
	[root@web01 nginx]# cat /etc/logrotate.d/nginx
	[root@web01 nginx]# cat /etc/logrotate.d/nginx
	/var/log/nginx/*.log {
	 daily
	 missingok
	 rotate 52
	 compress
	 delaycompress
	 notifempty
	 create 640 nginx adm
	 sharedscripts
	 postrotate
	 if [ -f /var/run/nginx.pid ]; then
	 kill -USR1 `cat /var/run/nginx.pid`
	 fi
	 endscript
	}
	man logroate
	man lorotate
日志轮询配置案例
	[root@web01 nginx]# cat /etc/logrotate.d/syslog
	/var/log/cron
	/var/log/maillog
	/var/log/messages
	/var/log/secure
	/var/log/spooler
	{
	 missingok
	 sharedscripts
	 postrotate
	/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
	 endscript
	}
	[root@web01 nginx]# cat /etc/logrotate.d/nginx
	/var/log/nginx/*.log {
	 daily
	 missingok
	 rotate 52
	 compress
	 delaycompress
	 notifempty
	 create 640 nginx adm
	 sharedscripts
	 postrotate
	 if [ -f /var/run/nginx.pid ]; then
	 kill -USR1 `cat /var/run/nginx.pid`
	 fi
	 endscript
	}
Nginx 图片及目录防盗链解决方案
	1. 水印
	2. http_referer(用户从哪里跳转到网站)
	只要是跳转的给你禁止
	if ( $http_referer !~ "lidao.blog.51cto.com")
	{
	return 403;
	}
cookie 和 session 区别?
	共同: 
	存放用户信息
	存放的形式:key-value 格式
	区别: 
	cookie : 存放在客户端浏览器 , 每个域名对应一个 cookie,不能跨越域名访问其他 cookie ,用户可以查看或修改 cookie , http 响应报文里面给你浏览器设置 ,钥匙(用于打开服务器上锁头)
	session : 存放在服务器(文件,数据库,redis)  存放敏感信息  锁头
Nginx 防爬虫优化(spider|bot)
	把你网站一个页面一个页面下载下来 缓存下来.
	搜索引擎:爬取网站数据 加速用户查找速度 容易被用户搜索到.
	防止爬虫:
	1. 君子协议 robot 协议
	在网站站点目录下面放一个 robots.txt 文件
	2. 强制措施(只要是爬虫就禁止访问)
	if ($http_user_agent ~* "spider|bot" )
	{
	return 403;
	}
	location uri
	nginx
nginx 配置文件,nginx 内置变量,nginx location 规则
	$remote_addr
	[$time_local]
	"$request" 请求报文的起始行
	$request_uri 请求 uri 路径
	"$request_time" '
	'$status
	$body_bytes_sent
	"$http_referer" 从哪里跳转过来的
	'"$http_user_agent"
	"$http_x_forwarded_for"'; 负载均衡+web web 用来记录用户真实 ip 地址
	upstream lidao {
	server 10.0.0.7;
	server 10.0.0.8;
	}
	http {
	server {
	 location / {
	 proxy_pass http://lidao;
	 proxy_set_header Host $host; #负载均衡配置多个虚拟机
	 proxy_set_header X-Forwarded-For $remote_addr; #让后端服务器记录用户真实 ip
			}
		}
	}
	location 匹配 uri
	location / { #默认
	}
	location /image { # oldboyedu.com/image/index.html oldboyedu.com/lidao/image/index.jpg
	}
	location ~ "规则" { #匹配正则 区分大小写
	}
	location ~* "\.php$" { #匹配正则 不区分大小写
	}
	location = / {
	}
	location ^~ "oldboy$" { #不进行正则匹配 优先匹配
	}
HTTP 请求与响应
	[root@web01 ~]# curl -v www.baidu.com
	* About to connect() to www.baidu.com port 80 (#0)
	* Trying 112.34.112.41...
	* Connected to www.baidu.com (112.34.112.41) port 80 (#0)
	> GET / HTTP/1.1
	> User-Agent: curl/7.29.0
	> Host: www.baidu.com
	> Accept: */*
	> #请求头空行 下面如果是 GET(下载)下面没有内容 如果是 POST(上传)下
	面就是上传信息
	< HTTP/1.1 200 OK
	< Server: bfe/1.0.8.18
	< Date: Thu, 21 Mar 2019 03:07:14 GMT
	< Content-Type: text/html
	< Content-Length: 2381
	< Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
	< Connection: Keep-Alive
	< ETag: "588604c8-94d"
	< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
	< Pragma: no-cache
	< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
	< Accept-Ranges: bytes
	<
	<!DOCTYPE html>
	<!--STATUS OK--><html> <head><meta http-equiv=content-type
	content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta
	content=always name=referrer><link rel=stylesheet type=text/css
	href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title> 百度一下,你就知道
	</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper>
	<div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true
	src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f
	action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input
	type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden
	name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn
	value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255
	autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百
	度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com
	name=tj_trnews class=mnav> 新 闻 </a> <a href=http://www.hao123.com name=tj_trhao123
	class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地 图 </a> <a
	href=http://v.baidu.com name=tj_trvideo class=mnav> 视 频 </a> <a href=http://tieba.baidu.com
	name=tj_trtieba class=mnav> 贴 吧 </a> <noscript> <a
	href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.c
	om%2f%3fbdorz_come%3d1 name=tj_login class=lb> 登 录 </a> </noscript>
	<script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+
	encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+
	"bdorz_come=1")+ '" name="tj_login" class="lb"> 登 录 </a>');</script> <a
	href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div>
	</div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度
	</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a
	href=http://www.baidu.com/duty/> 使用百度前必读 </a>&nbsp; <a href=http://jianyi.baidu.com/
	class=cp-feedback> 意 见 反 馈 </a>&nbsp; 京 ICP 证 030173 号 &nbsp; <img
	src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
	* Connection #0 to host www.baidu.com left intact

CDN

	方法 1:全站缓存 CNAME
	方法 2:只有图片 CDN 加速 (CDN 域名)
	cdn.drift.com
	location ~* "\.jpg$" {
	return 302 $scheme://cdn.drift.com/$request_uri ;
	}
	$scheme === http 或 https 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值