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

本文介绍如何通过将静态资源从微服务迁移到nginx实现动静分离,以此提高网站性能。具体步骤包括配置nginx以直接提供静态资源,并调整微服务以专注于处理动态内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的动静分离 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我才是真的封不觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值