Nginx 缓存及HTTPS

2022-10-04  本文已影响0人  Tinyspot

1. Nginx 缓存

1.1 缓存配置

1.2 缓存命令

Syntax: proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [min_free=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
Default:    —
Context:    http

proxy_cache_path 可选参数

Syntax: proxy_cache zone | off;
Default:    
proxy_cache off;
Context:    http, server, location

Syntax: proxy_cache_key string;
Default:    
proxy_cache_key $scheme$proxy_host$request_uri;
Context:    http, server, location

Syntax: proxy_cache_valid [code ...] time;
Default:    —
Context:    http, server, location

2. 动静分离

server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://localhost:8020;
        }

        location ~ .*\.(html|htm|jpg|jpeg|bmp|png|ico|js|css)$ {
            expires 2h; # 失效时间
            # 可以是本机目录也可以是其他服务器,proxy_pass
            root  static/doc;
        }
    }

目录:..\nginx\static\doc\demo.png
请求:http://localhost/nginx/index
图片:http://localhost/demo.png

3. 数据压缩

3.1 gzip 压缩

http {
    gzip  on;
    gzip_comp_level 3;
    gzip_types text/plain application/json image/jpeg image/png;
    server {
    }
}

测试下读取文件内容,返回数据

4. 跨域

是否同域
http://www.demo.com vs https://www.demo.com 不同域,协议不同
www.demo.com vs www.demo.net 不同域,域名不同
www.demo.com vs log.demo.com 不同域,子域名不同
www.demo.com:8080 vs www.demo.com:8090 不同域,端口不同
www.demo.com/index vs www.demo.com/query 同域

4.1 同源策略

前置准备:

  1. IDEA 启动两个服务, localhost:8020, localhost:8022
  2. 请求 http://localhost:8020/nginx/index
  3. localhost:8020 去请求 localhost:8022,跨域会报错
    <!--访问静态资源-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $.ajax({
            url: "http://localhost:8022/nginx/query",
            type: "GET",
            success: function (data) {
                alert("successful");
            },
            error: function () {
                alert("fail");
            }
        });
    });
</script>
cors-error.png

4.2 方式一:Nginx代理两个服务器

ajax 里的 URL 改为访问 Nginx

$.ajax({
    url: "http://localhost:8080/nginx/query"
});

两个服务都用 Nginx 代理,8020 <--- Nginx ----> 8022

server {
    listen       8080;
    server_name  localhost;

    location / {
        proxy_pass http://localhost:8020;
    }
    location /nginx/ {
        proxy_pass http://localhost:8022;
    }
}

请求 Nginx 地址 http://localhost:8080/index

@Controller
public class NginxController {
    @RequestMapping("/nginx/query")
    @ResponseBody
    public String query(HttpServletRequest request) {
        return "success";
    }
    @RequestMapping("/index")
    public String index() {
        return "index";
    }
}

4.2 方式二:Nginx 只代理一个服务器

server {
    listen       8080;
    server_name  localhost;

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, Accept, Origin, User-Agent, DNT, Cache-Control, X-Mx-ReqToken, X-Data-Type, X-Requested-With, X-Data-Type, X-Auth-Token';
        proxy_pass http://localhost:8022;
    }
}
$.ajax({
    url: "http://localhost:8080/nginx/query"
});

直接请求服务器地址 http://localhost:8020/index

上一篇下一篇

猜你喜欢

热点阅读