5.Nginx 动静分离实例

1、动静分离

  通过location指定不同的后缀名实现不同的请求转发。通过expires参数设置,可以使浏览器缓存过期时间,减少与服务器之前的请求和流量。具体Expires定义: 是给一个资源

  设定一个过期时间,也就是说无需去服务端验证,直接通过浏览器自身确认是否过期即可,所以不会产生额外的流量。此种方法非常适合不经常变动的资源。(如果经常更新的文件,不建议使用Expires来缓存),如果设置3d, 表示在这3天之内访问这个URL, 发送一个请求,比对服务器该文件最后更新时间没有变化,则不会从服务器抓取,返回状态码304,
如果有修改,则直接从服务器重新下载,返回状态码200
在这里插入图片描述
准备工作:

  在liunx系统中准备静态资源,用于进行访问/date/images 图片文件夹,/date/www html文件夹(工具:FileZilla)
在这里插入图片描述
  具体配置:

server {
    listen       80;
    listen  [::]:80;
    server_name  192.168.3.3;

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

    #location / {
    #    root   /usr/share/nginx/html;
    #    proxy_pass  http://192.168.3.3:8080;
    #    proxy_pass  http://myserver;
    #    index  index.html index.htm;
    #}
    
   # location ~ /yee/ {
   #      proxy_pass  http://192.168.3.3:8081;    
   # }    
    
   # location ~ /yew/ {
   #      proxy_pass  http://192.168.3.3:8082;
   # }
   #添加静态资源路径,访问html
     location ~ /www/ {
          root    /date/;
     }
   #添加静态资源路径,访问图片
     location ~ /images/ {
          root    /date/;
          #列出访问目录
          autoindex on; 
     }

    #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;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

  由于使用的是docker容器进行操作,直接将文件放在linux的文件夹中是访问不到的

[root@localhost /]# ls
bin  boot  date  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@localhost /]# cd /date/
[root@localhost date]# ls
images  www

  所以需要将文件拷贝到nginx容器中:

docker cp date 802047bd7530:/

  查看,发现拷贝成功

root@802047bd7530:/# ls
bin  boot  date  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@802047bd7530:/# cd date/
root@802047bd7530:/date# ls
images  www

  访问测试:http://192.168.3.3/www/,http://192.168.3.3/images/,http://192.168.3.3/images/mzq.jpg
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值