Nginx系列:配置跳转的常用方式
  TEZNKK3IfmPf 2023年11月13日 18 0

阅读文本大概需要3分钟。

       随着应用服务的增多,服务可能部署在不同的服务器上。这些服务有可能存在IP端口Port、请求的ContextPath等一样的情况,怎么合理的配置他们的跳转呢?下面介绍三种常见的跳转方式。

0x01:根据不同域名判断跳转不同服务

就是根据在nginx.conf配置的server_name与域名或者(或者IP)匹配跳转不同的服务。

#当客户端访问www.domain.com,监听端口号为80,直接跳转到data/www目录下文件
server {
    listen       80;
       server_name  www.domain.com;
       location / {
          root   data/www;
          index  index.html index.htm;
    }
 }
 #当客户端访问bbs.domain.com,监听端口号为80,直接跳转到data/bbs目录下文件
server {
     listen       80;
     server_name  bbs.domain.com;
     location / {
         root   data/bbs;
         index  index.html index.htm;
     }
}

0x02:根据不同端口判断跳转不同服务

就是根据在nginx.conf配置的listen指令匹配跳转不同的服务。

#当客户端访问www.domain.com,监听端口号为8081,直接跳转到data/www目录下文件
server {
      listen       8081;
      server_name  www.domain.com;
      location / {
          root   data/www;
          index  index.html index.htm;
      }
}

#当客户端访问www.domain.com,监听端口号为8082,直接跳转到data/bbs目录下文件
server {
      listen       8082;
      server_name  www.domain.com;
      location / {
          root   data/bbs;
          index  index.html index.htm;
      }
}

0x03:根据链接的ContextPath不同跳转不同的服务器

主要根据每个应用服务器的ContextPath的普通,匹配跳转到不同的服务器。

#服务创建监听的端口号
server {
    #监听的端口号
    listen       80;
    #服务名称
    server_name  www.domain.com;
   # 匹配项目名称为bbs开头
   location /bbs/ {
       #  配置反向代理
       proxy_pass http://192.168.1.188:8081/;
       index  index.html index.htm;
   }
   # 匹配项目名称为blog开头
   location /blog/ {
        # 配置反向代理
        proxy_pass http://192.168.1.188:8082/;
       index  index.html index.htm;
   }
}

往期精彩

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   28   0   0 nginxpasswd
  TEZNKK3IfmPf   2024年04月26日   39   0   0 linuxnginxcentos
  TEZNKK3IfmPf   2024年05月31日   34   0   0 nginxpasswd
  TEZNKK3IfmPf   2024年04月19日   29   0   0 nginxTCP
  TEZNKK3IfmPf   2023年11月15日   28   0   0 nginx
  TEZNKK3IfmPf   2023年11月15日   35   0   0 nginxphp
  TEZNKK3IfmPf   2023年11月15日   32   0   0 nginxphp
TEZNKK3IfmPf