day94-性能压测-优化-nginx动静分离

1.为什么要动静分离

从前面压测结果了解到,首页全量数据的获取主要慢在静态资源的加载

原因是静态资源跟动态的请求一样也放在微服务中,也要通过向tomcat发送请求获取,这样光静态资源的请求就占用tomcat很大一部分线程资源,这样就会导致吞吐量急剧的下降

那么我们要使得静态资源快速获取返回,要做动静分离

2.概念图

之前的动态静态请求都通过nginx到网关再到微服务通过tomcat获取资源,现在变为如下,静态资源我们从微服务中搬到nginx,访问nginx时静态资源直接返回,

这样tomcat得以空出手有足够多的线程资源来处理动态资源,静态资源的获取也不需要再经过网关与微服务中的tomcat,直接从nginx中返回

3.动手

(1)以后将所有静态资源全部搬到nginx中

在nginx文件夹中创建static文件夹

[root@10 ~]# cd /
[root@10 /]# ls
bin  boot  dev  -e  etc  home  lib  lib64  media  mnt  mydata  opt  proc  root  run  sbin  srv  swapfile  sys  tmp  usr  -v  vagrant  var
[root@10 /]# cd mydata/nginx/
[root@10 nginx]# ls
conf  html  logs
[root@10 nginx]# cd html/
[root@10 html]# ls
es  index.html
[root@10 html]# mkdir static
[root@10 html]# ls
es  index.html  static
[root@10 html]# 

项目内的index文件夹复制到static文件夹中,然后删除

此时访问还是这样,原因是nginx内还没添加配置

(2)定义路径映射规则:/static/**  所有请求都由nginx直接返回

修改conf.d文件夹下的 gulimall.conf 添加

完整命令如下

[root@10 conf.d]# ls
default.conf  gulimall.conf
[root@10 conf.d]# vi default.conf 

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
"default.conf" 45L, 1097C written
[root@10 conf.d]# vi gulimall.conf


        root   /usr/share/nginx/html;
server {
    listen       80;
    server_name  gulimall.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location /static/ {
        root   /usr/share/nginx/html;
    }

    location / {
         proxy_set_header Host $host;
         proxy_pass http://gulimall;
    }

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
"gulimall.conf" 49L, 1162C written
[root@10 conf.d]# docker restart nginx
nginx

再次访问首页,显示成功

至此实现了nginx的动静分离 

猜你喜欢

转载自blog.csdn.net/JavaCoder_juejue/article/details/113617391
今日推荐