当前位置: 首页 > article >正文

Nginx泛域名 解析的匹配前缀绑定或转发到子目录

网站的目录结构为:

# tree /home/wwwroot/landui.com

/home/wwwroot/landui.com

├── bbs

│   └── index.html

└── www

    └── index.html

2 directories, 2 files

/home/wwwroot/landui.com为nginx的安装目录下默认的存放源代码的路径。

bbs为论坛程序源代码路径;www为主页程序源代码路径;把相应程序放入上面的路径通过;http://www.landui.com 访问的就是主页http://www.landui.com 访问的就是论坛,其它二级域名类推。

有2种方法,推荐方法一

方法一:

server {

listen 80;

server_name ~^(?<subdomain>.+).landui.com$;

access_log /data/wwwlogs/landui.com_nginx.log combined;

index index.html index.htm index.php;

root /home/wwwroot/landui/$subdomain/;

location ~ .php$ {

    fastcgi_pass unix:/dev/shm/php-cgi.sock;

    fastcgi_index index.php;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    include fastcgi_params;

    }

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {

    expires 30d;

    }

location ~ .*\.(js|css)?$ {

    expires 7d;

    }

}

方法二:

server {

listen 80;

server_name *.landui.com;

access_log /home/wwwlogs/landui.com_nginx.log combined;

index index.html index.htm index.php;

if ($host ~* ^([^\.]+)\.([^\.]+\.[^\.]+)$) {

    set $subdomain $1;

    set $domain $2;

}

location / {

    root /home/wwwroot/landui.com/$subdomain/;

    index index.php index.html index.htm;

}

location ~ .php$ {

    fastcgi_pass unix:/dev/shm/php-cgi.sock;

    fastcgi_index index.php;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    include fastcgi_params;

    }

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {

    expires 30d;

    }

location ~ .*\.(js|css)?$ {

    expires 7d;

    }

}

另外: 如果是根据二级名称 分发到不同服务上

若是通配: *.xxxx.com 则需要配置 二级域名的名称获取:

 set $subdomain '';
  if ($host ~* ^(.*?)\.mydomain.net) {
    set $subdomain $1;
  }

然后根据名称 方发到不同的服务:

# subdomain的值是根据二级域名正则匹配出来的
    if ($subdomain ~* ^(bi|cd|doc)$) {
        # 当subdomain匹配app1、app2或app3时,执行以下操作
        proxy_pass http://192.1.4.6:8080;
    }
    if ($subdomain = git) {
        proxy_pass http://192.1.4.7:8080;
    }


http://www.kler.cn/news/309429.html

相关文章:

  • APP测试基本流程与APP测试要点总结
  • 什么是单元测试?怎么做?
  • C++系列-匿名对象
  • linux网络命令:使用最多最广泛的网络抓包工具tcpdump详细介绍
  • MATLAB入门教程
  • 检查一个复数C的实部a和虚部b是否都是有限数值即a和b都不是无限数值、空值cmath.isfinite(x)
  • MES管理系统在智能制造中的重要应用
  • CMU 10423 Generative AI:lec5(Encoder-only Transformers + 阅读材料Bert, ViT)
  • 如何理解BCEWithLogitsLoss()
  • 什么是期权日内交易?怎么做日内期权策略?
  • MyBatis 源码解析:Mapper 文件加载与解析
  • 导弹追踪问题:蒙特卡罗模拟+matlab代码
  • Linux7-su,exit,sudo
  • Java 中的 sleep、wait、join 怎么理解
  • linux中的kill、pkill和killall
  • C++速通LeetCode简单第3题-相交链表
  • RTMP协议在无人机巡检中的应用场景
  • 【深度学习】【OnnxRuntime】【Python】模型转化、环境搭建以及模型部署的详细教程
  • Java学习线路(2024版)
  • 简单了解微服务--黑马(在更)
  • 安全运维教程(非常详细)从零基础入门到精通,看完这一篇就够了
  • 【Pycharm使用技巧记录手册】批量检索与替换功能——辅助Yolo训练标签label配置文件构建
  • Mac笔记本上查看/user/目录下的文件的几种方法
  • mysql配置优化和分组报错问题解决
  • 信号与线性系统综合实验
  • 87-java 可轮询锁和定时锁
  • 网络安全宣传周的时间,举办活动的方式和意义
  • 计算机毕业设计公交站点线路查询网站登录注册搜索站点线路车次/springboot/javaWEB/J2EE/MYSQL数据库/vue前后分离小程序
  • 场外个股期权通道商是什么业务?个人投资者可以参与场外期权吗?
  • JavaScript ES6特性(var let const、function=>、增强表达赋值、类与对象)