V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
g0thic
V2EX  ›  Node.js

有什么工具或者方法实现'一键' 切换 nginx 配置

  •  
  •   g0thic · 2015-10-21 22:25:18 +08:00 · 3792 次点击
    这是一个创建于 3115 天前的主题,其中的信息可能已经有所发展或是发生改变。

    题目可能说不清!
    我这里说一下,我们的项目都是前后分离,几个项目都用的同一后端服务器,本地开发测试的时候是通过配置 nginx 来调用测试服务器的 Api 。(具体的这里也不细说了)
    所以问题就来了,随着负责项目的增加,我们的 nginx 配置会出现类似这么这个情况:

    location ~* ^.+\.(js|css|less|sass|map|gif)$ {
    #PC 静态资源
       #项目 1
        #root /Users/Go7hic/work/项目 1
       #项目 2
        root /Users/Go7hic/work/项目 2
       #项目 3
       #root /Users/Go7hic/work/项目 3
       # 项目 4
       #root /Users/Go7hic/work/项目 4
       #项目 5
      #root /Users/Go7hic/work/项目 5
      expires 2h;
    }
    #后端服务
    location ~ /{
      proxy_pass http://xx.xxx.xxx:3000;
    
    }
    

    每次我要开发测试项目 1 的时候,我又要找到 nginx.conf 来把项目 2 注释掉,而当我要修改测试项目 3 的一个 bug 时,又得把项目 1 的路径给注释掉。

    觉得每次都查找修改这个文件很麻烦,所以我现在的做法就是经常把 ngnix.conf 这个文件用编辑器打开不关,觉得这个做法一点都不优雅。

    现在想可不可以用 Node.js 或者其他语言来写个小工具来操作这个文件,希望各位给点建议或者分享你们的解决办法

    12 条回复    2015-10-22 13:04:06 +08:00
    AlloVince
        1
    AlloVince  
       2015-10-21 22:30:10 +08:00
    为什么不对不同的资源绑定不同的域名
    lhbc
        2
    lhbc  
       2015-10-21 22:30:32 +08:00 via Android
    多个 host 。
    zzlyzq
        3
    zzlyzq  
       2015-10-21 22:34:50 +08:00
    git checkout XXX
    g0thic
        4
    g0thic  
    OP
       2015-10-21 22:42:02 +08:00
    @AlloVince 😀没想到可以从域名入手,多谢
    @lhbc
    g0thic
        5
    g0thic  
    OP
       2015-10-21 22:44:38 +08:00
    @zzlyzq 不同的项目之间怎么 checkout ,这个还真不知道
    ryd994
        6
    ryd994  
       2015-10-21 23:29:22 +08:00
    文件有冲突么?
    如果没有的话可以 try_files 所有的
    Showfom
        7
    Showfom  
       2015-10-21 23:31:18 +08:00
    绑定不同域名或者不同端口啊
    cxbig
        8
    cxbig  
       2015-10-21 23:38:19 +08:00
    为每个项目文件夹做 vhost 文件,切换哪个就链接到 sites-enabled 。
    zzlyzq
        9
    zzlyzq  
       2015-10-22 00:05:51 +08:00
    @g0thic heihei, just use domain name . Last reply I just see you have different project at the different period, so you can use git checkout, aha~~
    randyzhao
        10
    randyzhao  
       2015-10-22 01:49:14 +08:00
    恩...
    .../conf.d/project1.conf
    .../conf.d/project2.conf
    .../conf.d/project3.conf
    .../conf.d/project4.conf
    ...
    5thcat
        11
    5thcat  
       2015-10-22 11:33:07 +08:00
    可以用 ejs, 把 root 那一行 做成 <%= root_dir %>
    mouer
        12
    mouer  
       2015-10-22 13:04:06 +08:00   ❤️ 1
    nginx conf 里面可以写 if 的, if 里面判断某个文件是否存在,比如(伪代码)
    if 存在 /tmp/a
    proxy http://a
    if 存在 /tmp/b
    proxy http://b
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   971 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:16 · PVG 07:16 · LAX 16:16 · JFK 19:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.