nginx网站架构优化

1)网站架构优化体系说明(面试)
面试时怎么说:
先说体系,再说具体
2)网站架构的优化具体操作部分(nginx)
网站安全的维度
网站性能的维度
网站管理有关的维度
用户体验的维度
3)动态解析服务具体优化部分(PHP)
4)数据库的优化(mysql)

02.网站架构优化体系说明

用户访问网站架构的过程

客户端浏览器 -----> DNS服务器 ----->防火墙设备 ----->负载均衡服务器 ----->nginx网站服务器 ----> PHP服务器 ------>数据库服务器

网络小技巧:加一个稳定器小设备。

优化方面:
①DNS服务器处做一个智能解析,让用户使用较近的DNS服务器,加快域名解析;如果是云主机服务器的情况,使用CDN缓存加速技术
②防火墙设备优化 数据包处理 做好数据包的映射
③负载均衡设备优化 性能优化 缓存优化 当用户访问网站时,如果是热点设备,就不联系后端的web服务器或者数据库服务器,直接在负载服务器上处理用户的请求,减少后端服务器的压力。(但是个人认为,nginx负载均衡数据包的进出都是经过nginx,nginx服务的压力本来就不算小,容易成为架构的短板点)
④PHP服务性能优化缓存,减轻后端web以及数据库服务器的压力
⑤数据库服务器的优化 数据信息的优化(索引的使用)数据库服务器搭建集群

网站架构示意图:
在这里插入图片描述

总结:
01.对前端DNS服务做好相应的优化
02.对负载均衡服务做好相应优化
03.对网站服务进行相应的优化
04.对动态信息做优化(缓存配置优化)
05.对数据库服务进行相应优化

03.网站架构优化具体操作部分
前期环境准备:
编译安装好nginx服务
因为需要加载一个特殊的模块,所以将编译的命令拿来

./configure --prefix=/application/nginx-1.16.1/ --sbin-path=/usr/bin/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --add-module=../ngx_cache_purge-2.3

网站安全角度
1)隐藏网站服务版本号信息
作用说明:尽可能不要泄露网站资源信息
参考资料:http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens
实现方式:

Syntax:		server_tokens on | off;
Default: 	server_tokens on;
Context:	http, server, 

举例配置:

配置前,访问时会出现版本号

[root@rsync nginx-1.16.1]# curl  -I  10.0.0.41
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Sat, 28 Mar 2020 12:17:01 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 28 Mar 2020 12:16:21 GMT
Connection: keep-alive
ETag: "5e7f4015-264"
Accept-Ranges: bytes

配置信息:

  server {
        listen       80;
        server_tokens off;
        server_name  localhost;

配置完重启后再操作查看效果:

[root@rsync conf]# curl  -I  10.0.0.41
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 28 Mar 2020 12:51:29 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 28 Mar 2020 12:16:21 GMT
Connection: keep-alive
ETag: "5e7f4015-264"
Accept-Ranges: bytes

2)隐藏网站服务名称信息
作用说明:尽可能不要泄露网站资源/情况信息
实现方式:需要利用编译安装隐藏服务程序名称
第一个历程:修改源码包文件信息

nginx-x.x.x/src/core/nginx.h   
#define NGINX_VERSION      "1.6.3" 						#<- 定义Nginx版本函数
#define NGINX_VER          "OWS/" NGINX_VERSION			#<- 定义Nginx名称加版本函数
#define NGINX_VAR          "OW
nginx-x.x.x/src/http/ngx_http_header_filter_module.c
49:static char ngx_http_server_string[] = "Server:nginx " CRLF;  #<- 修改本行结尾的nginx。

nginx-x.x.x/src/http/ngx_http_special_response.c
static u_char ngx_http_error_full_tail[] = 
"<hr><center>"NGINX_VER"(http://oldboy.blog.51cto.com)</center>"CRLF
 #<- 错误提示信息说明
"<hr><center>"NGINX_VER"<a href=http://oldboy.blog.51cto.com>(http://oldboy.blog.51cto.com)</a>
</center>"CRLF
#<- 错误提示超链接说明

3)修改nginx服务默认管理用户
作用说明:不要将root用户作为映射用户和管理员用户
实现方式:

 Syntax:		user user [group];
    Default:	user nobody nobody;
    Context:	main

网站性能角度
1)优化服务进程数量
作用说明:提升网站服务处理并发能力
实现方式:
worker_processes 1; --指定nginx要开启的进程数,结尾数字就是进程的个数
说明:添加worker进程数量建议 = 总的CPU核心数 提升2CPU核心数
2)优化服务单个进程最大连接数
作用说明:提升网站服务处理并发能力
实现方式:
worker_connections 1024 #<- 指定单个进程并发连接数量
说明:添加连接数量建议 总的并发连接数=worker进程数量
每个进程连接数据< 系统文件打开数量
3)优化指定nginx服务文件打开数量
作用说明:直接设置nginx程序文件打开数量
实现方式:
worker_rlimit_nofile 1024;
4)优化绑定不同的nginx进程到不同的CPU上
作用说明:将nginx进程信息尽可能平均到多颗CPU进行处理,有效平均硬件资源
实现方式
方式一:CPU4个核心时,分配方式
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
方式二:CPU8核心时,分配方式
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000;
方式三:CPU多核心时,分配方式
worker_processes 2;
worker_cpu_affinity 0101(0001 0100) 1010(1000 0010);

5)开启高效传输数据模式 流媒体网站 视频 直播 图片 电商
作用说明:利用高效传输模式,可以调整不同的传输方式
方式一配置:尽可能快传输:

syntax:sendfile on | off;							<- 参数语法
default:sendfile off;							 	<- 参数默认大小
context:http,server,location,if in location    	<- 可以放置的标签段
syntax: tcp_nopush on | off;			   #<==参数语法
    default: tcp_nopush off;				   #<==参数默认大小
    context: http,server,location		   #<==可以放置的标签段

建议大文件进行快速传输
方式二配置:尽可能减少传输包数量:默认

syntax:sendfile on | off;							<- 参数语法
    default:sendfile off;							 	<- 参数默认大小
    context:http,server,location,if in location    	<- 可以放置的标签段
Syntax:     tcp_nodelay on | off; 			#<==参数语法
    Default: 	tcp_nodelay on; 				#<==参数默认大小
    Context: 	http, server, location          #<==可以放置的标签段

建议小文件进行汇总传输

6)设置连接超时时间信息
作用说明:
1节省网站资源信息
2避免nginx服务出现僵死情况,尽快给用户返回结果

实现方式:

keepalive_timeout 60        <--  链路上没有数据传输时,设定的链路保活时间
	client_header_timeout 15  	<--  读取客户端请求报文(头部)超时时间,如果客户端发送请求有延迟,只等待指定时间
	client_body_timeout 15      <--  读取客户端请求报文(主体)超时时间,如果客户端发送请求有延迟,只等待指定时间
	send_timeout 25             <--  服务端响应报文间隔超时间隔(nginx给客户端响应)

总结:以上参数都是优化网络客户端到服务端连接资源 连接资源=进程数*每个进程连接数

建议设置方式:

keepalive_timeout 60
client_header_timeout 10
client_body_timeout 10
send_timeout 20

缓存的架构图
在这里插入图片描述

代理缓存反向代理的原理
在这里插入图片描述
7)nginx代理缓存配置说明
作用说明:减缓后端服务访问压力,将访问过的数据缓存在代理服务器本地
实现方式:
说明:开启缓存服务功能

Syntax:		proxy_cache zone | off;     开启缓存服务的功能  
    Default:	proxy_cache off;   
    Context:	http, server, location

说明:设置缓存数据路径信息

Syntax:		proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
    Default:	—
    Context:	http

说明:缓存数据有效期限设置

Syntax:		proxy_cache_valid [code ...] time;
    Default:	—
	Context:	http, server, location

说明:指定需要进行缓存数据信息

Syntax:		proxy_cache_key string;
Default:	proxy_cache_key $scheme$proxy_host$request_uri;
Context:	http, server, location

缓存配置实践过程
规划:
10.0.0.41 后端web服务
10.0.0.71 nginx负载缓存服务
环境:
web端的环境准备(可以是yum方式下载的nginx)

root@rsync ~]# for  i in {1..3} ;do echo test01_cache$i >/html/cache01/test$i.html ; done
[root@rsync ~]# for  i in {1..3} ;do echo test02_cache$i >/html/cache02/test$i.html ; done
[root@rsync ~]# for  i in {1..3} ;do echo test03_cache$i >/html/cache03/test$i.html ; done

[root@rsync cache01]# tree  /html
/html
├── cache01
│   ├── test1.html
│   ├── test2.html
│   └── test3.html
├── cache02
│   ├── test1.html
│   ├── test2.html
│   └── test3.html
└── cache03
    ├── test1.html
    ├── test2.html
    └── test3.html

3 directories, 9 files
[root@rsync cache01]# 

第二个历程:编写nginx扩展的配置文件

vim  /etc/nginx/conf.d/web_node.conf
	server {                -->  cache01 --> 8080 端口
	    listen   8080;
		root     /html/cache01;
		index    index.html;
	}
    server {                -->  cache02 --> 8081 端口
	    listen   8081;
		root     /html/cache02;
		index    index.html;
	}
    server {                -->  cache02 --> 8082 端口
	    listen   8082;
		root     /html/cache03;
		index    index.html;
	}

访问测试:
注意:此处这里的区别是test01 02 03 这些数字对应端口号

[root@zabbix nginx-1.16.1]# curl    10.0.0.41:8080/test3.html
test01_cache3
[root@zabbix nginx-1.16.1]# curl    10.0.0.41:8081/test3.html
test02_cache3
[root@zabbix nginx-1.16.1]# curl    10.0.0.41:8082/test3.html
test03_cache3
[root@zabbix nginx-1.16.1]# 

lb01的环境准备
编写nginx缓存的配置文件

upstream web {
       server 10.0.0.7:8080;
       server 10.0.0.7:8081;
       server 10.0.0.7.8082;
    }

	proxy_cache_path  /html/cache  levels=1:2 keys_zone=oldboy_cache:10m inactive=60m max_size=10g use_temp_path=off;
	path           -- 指定缓存数据主目录
	levels=x:x     -- 定义目录层级信息  定义1层目录名称信息和2层目录名称信息
	keys_zone      -- 开辟一个缓存数据空间,设置空间名称(oldboy_cache),开辟缓存空间大小
	inactive       -- 访问缓存数据间隔时间,超过间隔时间的数据会被清理
	max_size=size  -- 设置最大缓存空间大小,超过后nginx会启用淘汰机制
	use_temp_path=off  --- 临时文件访问效率提升,开启会影响访问性能
	
	server {
       listen        80;
       server_name   10.0.0.71;
       location / {
           proxy_pass    http://web;
           proxy_set_header host $host;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_next_upstream  error timeout invalid_header  http_403;
           proxy_cache oldboy_cache;
           proxy_cache_valid 200 304 12h;
           proxy_cache_valid any 10m;
           add_header Nginx-Cache "$upstream_cache_status";                     
            }
         }
     }

完整的配置负载均衡配置文件

[root@zabbix conf]# cat nginx.conf
user  nginx;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
upstream web {
server 10.0.0.41:8080;
server 10.0.0.41:8081;
server 10.0.0.41:8082;
}
proxy_cache_path /html/cache levels=1:2 keys_zone=oldboy_cache:10m inactive=60m max_size=10g use_temp_path=off;
server {
       listen        80;
       server_name  10.0.0.71;
       location / {
           proxy_pass    http://web;
           proxy_set_header host $host;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_next_upstream  error timeout invalid_header  http_403;
           proxy_cache oldboy_cache;
           proxy_cache_valid 200 304 12h;
           proxy_cache_valid any 10m;
           add_header Nginx-Cache "$upstream_cache_status";                     
            }
     }
}

注意:这一个指令不要使用localhost默认的,会提示一些其他的信息,导致无法将请求负载到后端web。
server_name 10.0.0.71;
否则会出现这个情况

[root@zabbix conf]# nginx -t
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: the configuration file /application/nginx-1.16.1//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.1//conf/nginx.conf test is successful

这个我不是很清楚是怎么回事儿,但是访问localhost是不成的,即使解析为127.0.0.1也不行.

现象
在这里插入图片描述
此时查看我们设置的/html/cache的缓存目录。(这个缓存目录是需要我们手动创建的)

[root@zabbix conf]# cd /html/cache/
[root@zabbix cache]# ll
total 0
drwx------ 3 nginx nginx 16 Mar 29 04:47 e
[root@zabbix cache]# 

会发现有一条缓存的信息,如果再次访问test02(刚才访问的test03)
会发现第一次是Miss
第二次就是hit,并且该目录下会多出一条缓存信息
在这里插入图片描述

此时还会有一个问题就是:你会发现原本负载均衡设置的是轮询的方式反向代理用户的请求,但是现在第一次访问后,再次刷新页面,也还是那个信息,没有转发。
这是因为直接读取的nginx缓存的信息,没有将用户请求转发给后端web,所以没有轮询的现象。

关于缓存目录/html/cache中缓存信息的命名规则:

[root@zabbix cache]# tree 
.
├── 3
│   └── f7
│       └── 9bd9e5398c9da875890da933307f5f73
├── 4
│   └── 0a
│       └── 11337e0503f5890bcb26a79f6b9fe0a4
└── e
    └── 51
        └── 88e1365876dd446161a99ef3b40c751e

6 directories, 3 files
[root@zabbix cache]# 

在这里插入图片描述

缓存清除的操作:
方法一:直接删除缓存目录里面的数据信息
由于方式简单,我就不写了,其实就删除缓存目录下的数据信息

方法二(需要进行编译安装的nginx才能实现,因为有一个模块使用这个功能):有针对性的清除指定缓存信息
利用nginx_cache_purge 扩展模块进行缓存的清除操作
第一个历程:获取nginx源码包和扩展模块数据信息
nginx源码压缩包:http://nginx.org/download/nginx-1.16.1.tar.gz
nginx模块压缩包:http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
第二个历程:编译安装nginx程序

yum  install -y  openssl-devel   pcre-devel
tar  xf  nginx-1.16.1.tar.gz
tar  xf  ngx_cache_purge-2.3.tar.gz
cd nginx-1.16.1
./configure --prefix=/application/nginx-1.16.1/ --sbin-path=/usr/bin/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --add-module=../ngx_cache_purge-2.3
     make && make install

查看已经安装的模块信息nginx -V

[root@zabbix conf]# nginx  -V
nginx version: IIS/1.1.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.16.1/ --sbin-path=/usr/bin/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --add-module=../ngx_cache_purge-2.3
[root@zabbix conf]

第三个历程:编写nginx.conf配置文件

[root@zabbix cache]# cat /application/nginx-1.16.1/conf/nginx.conf
user  nginx;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
upstream web {
server 10.0.0.41:8080;
server 10.0.0.41:8081;
server 10.0.0.41:8082;
}
proxy_cache_path /html/cache levels=1:2 keys_zone=oldboy_cache:10m inactive=60m max_size=10g use_temp_path=off;
server {
       listen        80;
       server_name  10.0.0.71;
       location / {
           proxy_pass    http://web;
           proxy_set_header host $host;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_next_upstream  error timeout invalid_header  http_403;
           proxy_cache oldboy_cache;
           proxy_cache_key $host$uri$is_args$args;
           proxy_cache_valid 200 304 12h;
           proxy_cache_valid any 10m;
           add_header Nginx-Cache "$upstream_cache_status";                     
            }
        location ~ /purge(/.*)  {
        allow   127.0.0.1;
        allow   10.0.0.1;
        deny    all;
        proxy_cache_purge oldboy_cache $host$1$is_args$args;
      }
     }
}
[root@zabbix cache]# 

测试访问:
在这里插入图片描述

清除缓存前

[root@zabbix cache]# tree  /html/cache/
/html/cache/
├── 8
│   └── 8b
│       └── 26647d500baba13f7f014bb6c3c458b8
├── a
│   └── a6
│       └── c9b8e7f832c240ef57c41d292e210a6a
└── f
    └── c4
        └── b204ba8402fa1ab9717b3f0879090c4f

6 directories, 3 files

清除缓存
在这里插入图片描述

清除缓存后

[root@zabbix cache]# tree  /html/cache/
/html/cache/
├── 8
│   └── 8b
├── a
│   └── a6
└── f
    └── c4

6 directories, 0 files
[root@zabbix cache]# 

指定部分信息进行不要缓存:
特殊图片 密码信息 注册信息 登录页面 评论页面
举例访问test1.html 不要缓存 test2-3.html 需要缓存

编写nginx配置文件

 server {
        listen       80;
        server_name  localhost;
        if ($request_uri ~ ^/(test1|login|register|password)) {    定义不做缓存uri信息
             set $cookie_nocache 1;                                设置一个变量
        }
        location / {
           proxy_pass    http://web;
           proxy_set_header host $host;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_next_upstream  error timeout invalid_header  http_403;
           proxy_cache oldboy_cache;
           proxy_cache_key $host$uri$is_args$args;
           proxy_cache_valid 200 304 12h;
           proxy_cache_valid any 10m;
           add_header Nginx-Cache "$upstream_cache_status";                     
           proxy_no_cache $cookie_nocache $arg_nocahe $arg_comment;   加载不做缓存变量信息,实现将指定uri内容不做缓存处理
       }
      }

完整的配置文件

[root@zabbix cache]# cat /application/nginx-1.16.1/conf/nginx.conf
user  nginx;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
upstream web {
server 10.0.0.41:8080;
server 10.0.0.41:8081;
server 10.0.0.41:8082;
}
proxy_cache_path /html/cache levels=1:2 keys_zone=oldboy_cache:10m inactive=60m max_size=10g use_temp_path=off;
server {
       listen        80;
       server_name  10.0.0.71;
       if  ($request_uri ~ ^/(test1|lgin|register|password)) {
        set  $cookie_nocache 1;
     }
       location / {
           proxy_pass    http://web;
           proxy_set_header host $host;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_next_upstream  error timeout invalid_header  http_403;
           proxy_cache oldboy_cache;
           proxy_cache_key $host$uri$is_args$args;
           proxy_cache_valid 200 304 12h;
           proxy_cache_valid any 10m;
           add_header Nginx-Cache "$upstream_cache_status";                     
           proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;           
 }
        location ~ /purge(/.*)  {
        allow   127.0.0.1;
        allow   10.0.0.1;
        deny    all;
        proxy_cache_purge oldboy_cache $host$1$is_args$args;
      }
     }
}

在这里插入图片描述
服务器目录查看缓存,也是没有的

[root@zabbix cache]# tree 
.
├── 8
│?? └── 8b
├── a
│?? └── a6
└── f
    └── c4

6 directories, 0 files
[root@zabbix cache]# 
发布了224 篇原创文章 · 获赞 19 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_42506599/article/details/105166619