zoukankan      html  css  js  c++  java
  • nginx动静分离与网关

    当我们请求一个网页的时候,可能会加载很多css,js,img等静态文件;一般这些文件是很久都不会变化的,所以我们为了提高页面响应速度,完全可以将这些文件缓存到浏览器中(可以理解为cookie信息),这就叫动静分离。

    1.  vim nginx.conf

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        
        log_format  wuleiformat  '$remote_addr - $remote_user [$time_local]';
    
        sendfile        on;
        keepalive_timeout  65;
    
        server {
            listen       80;
            server_name  localhost;
            
            location /baby_ting{
                alias '/usr/local/wulei/nginx-1.16.1/html/LOVE/';
            }
    
            # 输入ip默认访问html文件夹下的index.html文件
            location / {
                root html;
                index index.html index.htm;
            }
            # 所有.png  .css ...结尾的文件都直接去/html/images里面找
            location ~ .(png|jpg|js|css|gif)$ {
              root html/images;
              expires 10m;  # 缓存10分钟
               # s秒   m分   h时   d天
            }
        }
    }

    2.  修改首页index.html  让它去加载一个静态图片。

     3.  重启可以看到效果(图片第一次加载花了7ms,后面再次刷新就是0ms,查看详细信息看到34分第一次缓存的,直到44分失效)。

    nginx网关配置

    1. 比如现在有两个项目,url前缀分别是 /a  /b

      

    2. nginx.conf 配置网关

    #user  nobody;
    worker_processes  1;
    
    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;
    
        server {
            listen       80;
            server_name  localhost;
    
            location /a {
                proxy_pass http://127.0.0.1:8080/a/;
            }
            
            location /b {
                proxy_pass http://127.0.0.1:8081/b/;
            }
        }
    }

    此时: 访问localhost/a/index 就相当于访问 127.0.0.1:8080/a/index了。

  • 相关阅读:
    HTTP 协议中的并发限制及队首阻塞问题
    聊聊JMM
    聊聊CacheLine
    git解决本地建立git仓库 连接远程git仓库出现拒绝合并问题
    django 本地项目部署uwsgi 以及云服务器部署 uwsgi+Nginx+Docker+MySQL主从
    我的第一篇播客
    大爷的超市管理系统——冲刺第一天
    能混绝不C——凡事预则立
    2020软件工程团队作业——05
    2020软件工程作业——04
  • 原文地址:https://www.cnblogs.com/wlwl/p/9855724.html
Copyright © 2011-2022 走看看