nginx

26 阅读1分钟
http {
    upstream api2Service { // 默认情况下负载均衡使用轮训的方式,轮训其实是一种特殊的加权策略。即每个服务器的weight都为1
        least_conn; // 最少连接。nginx 将尽量不给过于繁忙的应用服务器负载过多的请求,而是将新的请求分发到不太忙的服务器。
        ip_hash; // 使用 IP 哈希,客户端的 IP 地址用作为哈希键,以确定应用为客户端请求选择服务器组中的哪个服务器。此方法确保了来自同一个客户端的请求始终被定向到同一台服务器,除非该服务器不可用。
        server request1.com weight=3; // weight加权,指定了该服务器可以被分配请求的比重。
        server request2.com;
    }
    server {
        error_log  logs/error.log  notice; // 错误日志目录及级别
        server_name: www.helloworld.com; // 域名
        listen: 8089; // 端口
        location /static { // 匹配路径
            root: /path/to/root; // 静态资源根目录; /static/1.img映射到/path/to/root/static/1.img
            alias: /path/to/alias; // /static/1.img映射到/path/to/alias/1.img
        }
        
        location ^~ /api/ {
            proxy_pass: http://apiserver.com/; // /api/getList映射到http://apiserver.com/getList
        }

        location ^~ /api2/ {
            proxy_pass: http://api2Service/; // api2Service对应upstream name
        }
    }

}