nginx的负载均衡配置和重定向

upstream_check模块

配置文件详情

复制代码
upstream cluster1{
 server 10.0.0.4:80  weight=1 max_fails=3 fail_timeout=30s;
 server 10.0.0.5:80  weight=1 max_fsils=3 fsil_tomeout;
 check interval=3000 rise=2 fall=5 timeout=1000 type=http;

 check interval=3000 rise=2 fall=5 timeout=1000 type=http;
 check_http_send "HEAD /HTTP/1.0/r/n/r/n";
 check_http_expect_alive http_2xx http_3xx;
} 

server {
    listen 80;
    server_name Lb.oldboylinux.cn;
    error_log /var/log/nginx/lb-error.log notice;
    access_log /var/log/nginx/lb-access.log main;

    location / {
        proxy_pass http://lb_pools;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}
 
 

location /1{
      proxy_pass http://cluster1;
}

location /2{

      proxy_pass http://cluster2;
}

location /status{
      check_status;
      access_log  off;
      allow 10.0.0.1;
      allow 10.0.0.0/24;
      deny all;
}

}

最后访问Lb.oldboylinux.cn页面出来的是测试模块

web集群 nginx rewrite功能

nginx重定向

url重定向又叫做url改写

rewrite模块

return指令

我们在配置文件中加入reutrn 模块

具体如下

复制代码
server {
    listen 80;
    server_name rewrite.xm cn;  # 修正 server_name
    root /app/code/rewrite;

    location / {
        index index.html;
        try_files $uri $uri/ =404;  # 处理文件不存在的情况
    }

    location /admin/ {
        return 403;  # 禁止访问
    }
}

当我们做好域名解析以后 访问网站/admin/模块就会出现以下情形

域名间跳转

复制代码
server {
    listen 80;
    server_name rewrite.xm cn;  # 修正 server_name
    root /app/code/rewrite;

    # 对根路径的请求
    location / {
        index index.html;
        try_files $uri $uri/ =404;  # 处理文件不存在的情况
    }

    # 对/admin/路径的请求
    location /admin/ {
        return 403;  # 禁止访问
    }

    # 如果希望重定向某个具体路径,可以添加相应的 location 块
    location /redirect {
        return 301 http://www.baidu.com;  # 重定向到百度
    }
}

nginx if判断

一般放在 server,location

if(条件){

满足条件执行的内容

}

set 自己创建或者修改nginx变量

set $变量 值

复制代码
http {
    server {
        listen 80;
        server_name example.com;

        # 设置一个变量
        set $is_mobile "no";

        # 检查请求头中的User-Agent是否包含"Mobile"
        if ($http_user_agent ~* "Mobile") {
            set $is_mobile "yes";
        }

        location / {
            # 如果变量$is_mobile为"yes",则返回状态码403
            if ($is_mobile = "yes") {
                return 403;
            }

            # 否则,返回状态码200
            return 200;
        }
    }
}

rewrite模块

域名跳转

复制代码
server {
    listen 80;
    server_name rewrite.oldboylinux.cn;



    # 这一行是实际执行的重写规则
    rewrite ^([^/]*)$ http://www.baidu.com$1;
}
相关推荐
EnigmaCoder4 分钟前
Java多线程:核心技术与实战指南
java·开发语言
攀小黑7 分钟前
阿里云 使用TST Token发送模板短信
java·阿里云
麦兜*13 分钟前
Spring Boot秒级冷启动方案:阿里云FC落地实战(含成本对比)
java·spring boot·后端·spring·spring cloud·系统架构·maven
自由鬼40 分钟前
正向代理服务器Squid:功能、架构、部署与应用深度解析
java·运维·服务器·程序人生·安全·架构·代理
fouryears_234171 小时前
深入拆解Spring核心思想之一:IoC
java·后端·spring
codervibe2 小时前
使用 Spring Boot + JWT 实现多角色登录认证(附完整流程图)
java·后端
坚持学习永不言弃2 小时前
Ehcache、Caffeine、Memcached和Redis缓存
java
阿劲2 小时前
从业务卡顿到数据库连接池耗尽:Spring Boot项目HikariCP超时问题实战排查
java·后端·面试
亮1112 小时前
Maven 编译过程中发生了 Java Heap Space 内存溢出(OutOfMemoryError)
java·开发语言·maven
添乱2 小时前
「Java案例」求PI的值
java