upstream bakend {
# 哈希算法,自动定位到该服务器 保证唯一ip定位到同一部机器 用于解决session登录态的问题
ip_hash;
server 127.0.0.1:9090 down; #(down 表示单前的server暂时不参与负载)
server 127.0.0.1:8080 weight=2; #(weight 默认为1.weight越大,负载的权重就越大)
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup; # (其它所有的非backup机器down或者忙的时候,请求backup机器)
}
# html设置history模式
location / {
index index.html index.htm;
proxy_set_header Host $host;
try_files $uri $uri/ /index.html; # history模式最重要就是这里
# index.html文件不可以设置强缓存 设置协商缓存即可
add_header Cache-Control 'no-cache, must-revalidate, proxy-revalidate, max-age=0';
proxy_pass http://bakend; #代理到负载均衡的机器
}
# 接口反向代理
location ^~ /api/ {
# 跨域处理 设置头部域名
add_header Access-Control-Allow-Origin *;
# 跨域处理 设置头部方法
add_header Access-Control-Allow-Methods 'GET,POST,DELETE,OPTIONS,HEAD';
# 改写路径
rewrite ^/api/(.*)$ /$1 break;
# 反向代理
proxy_pass http://static_env;
proxy_set_header Host $http_host;
}
location ~* .(?:css(.map)?|js(.map)?|gif|svg|jfif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
# 静态资源设置七天强缓存
expires 7d;
access_log off;
}