【愚公系列】2022年04月 .NET架构班 033-分布式中间件 Nginx多场景使用问题分析之动静分离
前言
在Web开发中,通常来说,动态资源其实就是指那些后台资源,而静态资源就是指HTML,JavaScript,CSS,img等文件。
一般来说,都需要将动态资源和静态资源分开,将静态资源部署在Nginx上,当一个请求来的时候,如果是静态资源的请求,就直接到nginx配置的静态资源目录下面获取资源,如果是动态资源的请求,nginx利用反向代理的原理,把请求转发给后台应用去处理,从而实现动静分离。
在使用前后端分离之后,可以很大程度的提升静态资源的访问速度,同时在开过程中也可以让前后端开发并行可以有效的提高开发时间,也可以有些的减少联调时间 。
一、Nginx多场景使用问题分析之动静分离
1.同一虚拟主机使用
缺陷:如果商品是在电商Web网站中,这个时候从电商网站当中查询商品的时候,会同时加载两类数据,静态数据和动态数据,静态数据:js css 图片,视频,音频等等,动态资源:商品数据。如果访问js css 并发量比较大,会出现cpu,内存等资源全部被耗尽,导致动态数据加载没有资源可用,所以,导致动态资源请求导致性能下降问题?如何提升性能?
方案:动静分离
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
proxy_cache_path /cache/nginx/ levels=1:2 keys_zone=mycache:64m;
limit_conn_zone $server_name zone=perserver:10m;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
limit_conn perserver 1;
proxy_cache mycache;
proxy_pass http://YDT.EBusiness;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_methods GET HEAD;
proxy_cache_revalidate on;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1m;
proxy_cache_min_uses 1;
proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
}
#静态资源
location ~ \.(ico|js|css|png|jpg|mp4)$ {
root D:/work/net-project/分布式中间件专题/网关中间件Nginx/XT.EBusiness.StaticResource/wwwroot;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# https 虚拟主机
server {
listen 4435 ssl;
server_name localhost;
ssl_certificate D:/work/net/grpc-cluster/server-cert.pem;
ssl_certificate_key D:/work/net/grpc-cluster/server-key.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
location / {
proxy_pass http://XT.EBusiness;
}
}
}
#动态负载均衡配置
upstream XT.EBusiness{
server localhost:5001 max_fails=2 fail_timeout=10s;
server localhost:5002 max_fails=2 fail_timeout=10s;
server localhost:5003 backup;
}
}
#数据库代理
stream {
server {
listen 13306;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass localhost:3306;
}
upstream mysql {
server localhost:3306;
}
}
2.不同虚拟主机使用
缺陷:如果动静资源在一个虚拟主机中,那么静态资源和动态资源共享同一个资源,如果静态或者动态资源访问量比较大,把资源消耗殆尽,动态和静态资源互相会进行影响,导致系统整体上性能下降,如何提升性能?
方案:动静不同虚拟主机
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
proxy_cache_path /cache/nginx/ levels=1:2 keys_zone=mycache:64m;
limit_conn_zone $server_name zone=perserver:10m;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
limit_conn perserver 1;
proxy_cache mycache;
proxy_pass http://YDT.EBusiness;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_methods GET HEAD;
proxy_cache_revalidate on;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1m;
proxy_cache_min_uses 1;
proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
}
#静态资源
#location ~ \.(ico|js|css|png|jpg|mp4)$ {
# root D:/work/net-project/分布式中间件专题/网关中间件Nginx/YDT.EBusiness.StaticResource/wwwroot;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server {
listen 8089;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
proxy_pass http://localhost:5007;
}
#静态资源
#location ~ \.(ico|js|css|png|jpg|mp4)$ {
# root D:/work/net-project/分布式中间件专题/网关中间件Nginx/YDT.EBusiness.StaticResource/wwwroot;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8090;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
#location / {
# proxy_pass http://localhost:5007;
#}
#静态资源
location ~ \.(ico|js|css|png|jpg|mp4)$ {
root D:/work/net-project/分布式中间件专题/网关中间件Nginx/XT.EBusiness.StaticResource/wwwroot;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
proxy_cache_path cache/nginx/ levels=1:2 keys_zone=mycache:64m;
server {
listen 8091;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
proxy_cache mycache;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_methods GET HEAD;
proxy_cache_revalidate on;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1m;
proxy_cache_min_uses 1;
proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_pass http://localhost:8089;
}
#静态资源
location ~ \.(ico|js|css|png|jpg|mp4)$ {
proxy_pass http://localhost:8090;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# https 虚拟主机
server {
listen 4435 ssl;
server_name localhost;
ssl_certificate D:/work/net/grpc-cluster/server-cert.pem;
ssl_certificate_key D:/work/net/grpc-cluster/server-key.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
location / {
proxy_pass http://XT.EBusiness;
}
}
}
#动态负载均衡配置
upstream XT.EBusiness{
server localhost:5001 max_fails=2 fail_timeout=10s;
server localhost:5002 max_fails=2 fail_timeout=10s;
server localhost:5003 backup;
}
}
#数据库代理
stream {
server {
listen 13306;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass localhost:3306;
}
upstream mysql {
server localhost:3306;
}
}
3.动静分离共享资源
缺陷:如果动静资源在一个虚拟主机中,那么静态资源和动态资源共享同一个资源,如果静态或者动态资源访问量比较大,把资源消耗殆尽,动态和静态资源互相会进行影响,导致系统整体上性能下降,如何提升性能?
方案:Include
Include nginx.https.conf
- 点赞
- 收藏
- 关注作者
评论(0)