g0thic
V2EX  ›  Node.js

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

  •  
  •   g0thic · Oct 21, 2015 · 5119 views
    This topic created in 4033 days ago, the information mentioned may be changed or developed.

    题目可能说不清!
    我这里说一下,我们的项目都是前后分离,几个项目都用的同一后端服务器,本地开发测试的时候是通过配置 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 replies  •  2015-10-22 13:04:06 +08:00
    AlloVince
    AlloVince
    Oct 21, 2015
    1
    为什么不对不同的资源绑定不同的域名
    lhbc
    lhbc
    Oct 21, 2015 via Android
    2
    多个 host 。
    zzlyzq
    zzlyzq
    Oct 21, 2015
    3
    git checkout XXX
    g0thic
    g0thic
    OP
    Oct 21, 2015
    4
    @AlloVince 😀没想到可以从域名入手,多谢
    @lhbc
    g0thic
    g0thic
    OP
    Oct 21, 2015
    5
    @zzlyzq 不同的项目之间怎么 checkout ,这个还真不知道
    ryd994
    ryd994
    Oct 21, 2015
    6
    文件有冲突么?
    如果没有的话可以 try_files 所有的
    Showfom
    Showfom
    PRO
    Oct 21, 2015
    7
    绑定不同域名或者不同端口啊
    cxbig
    cxbig
    Oct 21, 2015
    8
    为每个项目文件夹做 vhost 文件,切换哪个就链接到 sites-enabled 。
    zzlyzq
    zzlyzq
    Oct 22, 2015
    9
    @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
    randyzhao
    Oct 22, 2015
    10
    恩...
    .../conf.d/project1.conf
    .../conf.d/project2.conf
    .../conf.d/project3.conf
    .../conf.d/project4.conf
    ...
    5thcat
    5thcat
    Oct 22, 2015
    11
    可以用 ejs, 把 root 那一行 做成 <%= root_dir %>
    mouer
    mouer
    Oct 22, 2015❤️1
    12
    nginx conf 里面可以写 if 的, if 里面判断某个文件是否存在,比如(伪代码)
    if 存在 /tmp/a
    proxy http://a
    if 存在 /tmp/b
    proxy http://b
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2271 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 11:40 · PVG 19:40 · LAX 03:40 · JFK 06:40
    ♥ Do have faith in what you're doing.