nginx 如何实现多个 web 应用共用一个端口一个域名? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
myyou
V2EX    NGINX

nginx 如何实现多个 web 应用共用一个端口一个域名?

  •  1
     
  •   myyou 2016-04-16 09:58:54 +08:00 16751 次点击
    这是一个创建于 3520 天前的主题,其中的信息可能已经有所发展或是发生改变。
    32 条回复    2018-01-11 11:49:41 +08:00
    manhere
        1
    manhere  
       2016-04-16 10:02:54 +08:00 via iPhone   1
    用不同入口 /目录啊
    penjianfeng
        2
    penjianfeng  
       2016-04-16 10:03:04 +08:00   2
    你这问题让我也想问一个问题,如何让两个妹子同时爱上我....
    myyou
        3
    myyou  
    OP
       2016-04-16 10:08:56 +08:00
    @manhere 能具体点吗?
    junnplus
        4
    junnplus  
       2016-04-16 10:08:57 +08:00   1
    myyou
        5
    myyou  
    OP
       2016-04-16 10:09:55 +08:00
    @penjianfeng 同时戳瞎两个妹子的眼\(^o^)/~
    ss098
        6
    ss098  
       2016-04-16 10:10:20 +08:00   1
    可以设置 Cookie 或者不同应用之间的特征码以区别应用,比如有 A 和 B 两个应用,就可以设置 Cookie application=a ,然后将所有请求转发到应用中。
    myyou
        7
    myyou  
    OP
       2016-04-16 10:22:17 +08:00
    @junnplus 请问这在 nginx 要怎么设置啊?
    wyssurvivor
        8
    wyssurvivor  
       2016-04-16 10:22:46 +08:00   1
    在域名后面加路径,就像 4 楼说的那样
    qgy18
        9
    qgy18  
       2016-04-16 10:29:52 +08:00 via iPhone   1
    @myyou 不同路径 proxy pass 到不同 backend 。
    junnplus
        10
    junnplus  
       2016-04-16 10:47:36 +08:00   2
    location /app1 {
    ....rewrite /app1/(.*) /$1 break;
    ....proxy_pass http://localhost:5000;
    ....proxy_redirect off;
    ....proxy_set_header Host $proxy_host;
    ....proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    大概这样子,具体还请自行 google

    ps :为什么不用二级域名反向代理呢?
    myyou
        11
    myyou  
    OP
       2016-04-16 10:53:16 +08:00
    @junnplus 谢谢
    bdbai
        12
    bdbai  
       2016-04-16 11:17:59 +08:00 via Android
    @ss098 浏览器同时开两个应用会发生什么?
    ss098 /td>
        13
    ss098  
       2016-04-16 11:58:37 +08:00
    @bdbai 同一个 Cookie 只能保持一个值啊,所以不会发生同时打开两个的情况,除非代码实现有问题。
    lux182
        14
    lux182  
       2016-04-16 12:03:15 +08:00
    域名:目录:端口
    bdbai
        15
    bdbai  
       2016-04-16 12:32:43 +08:00 via Android
    @ss098 这个最佳实践我给满分。
    RqPS6rhmP3Nyn3Tm
        16
    RqPS6rhmP3Nyn3Tm  
       2016-04-16 13:02:36 +08:00 via Android
    上子域啊
    tobacco
        17
    tobacco  
       2016-04-16 13:07:22 +08:00   3
    可以用请求 url 区分不同的 web 服务器,配置文件类似于:

    http {
    server {
    server_name example.com;

    location /mail/ {
    proxy_pass http://example.com:protmail/;
    }

    location /com/ {
    proxy_pass http://example.com:portcom/main/;
    }

    location / {
    proxy_pass http://example.com:portdefault;
    }
    }
    }

    可以参考这篇文章: http://blog.lwons.com/archieve/nginx_reverse_proxy.html
    kitalphaj
        18
    kitalphaj  
       2016-04-16 13:47:58 +08:00
    如果你说的域名可以是 aaa.me.combbb.me.com 那就很简单。
    JerningChan
        19
    JerningChan  
       2016-04-16 14:41:14 +08:00
    同域名,同端口,那就要不同路径了..
    JerningChan
        20
    JerningChan  
       2016-04-16 14:41:44 +08:00
    你要把 3 个不同站点放到同一个 web 根目录入边
    yangxin0
        21
    yangxin0  
       2016-04-16 14:48:21 +08:00
    http 里面有种东西叫 Host
    beginor
        22
    beginor  
       2016-04-16 15:15:33 +08:00 via Android
    楼上正解, ng 可以配置多个 host
    Fedor
        23
    Fedor  
       2016-04-16 15:27:14 +08:00   3
    server_name
    julyclyde
        24
    julyclyde  
       2016-04-17 10:07:43 +08:00
    @yangxin0 lz 问的是同一个域名
    @beginor
    kaneg
        25
    kaneg  
       2016-04-17 11:11:38 +08:00
    反向代理就是干这个活的
    beginor
        26
    beginor  
       2016-04-17 13:23:29 +08:00 via Android
    同一个端口,同一个域名,又不想用 URL 或子域名,难道是要弄 load balance ?
    rupert
        27
    rupert  
       2016-04-17 15:06:29 +08:00 via iPhon
    @beginor 也不是,你猜 哈哈哈
    myyou
        28
    myyou  
    OP
       2016-04-18 17:48:15 +08:00
    @tobacco 谢谢
    sunnogo
        29
    sunnogo  
       2018-01-10 16:56:10 +08:00
    @junnplus 我也是想搞个这样的:

    http://example.com/app1
    http://example.com/app2
    http://example.com/app3

    你在 10 楼的回复,意思是不是这样:

    server app1, example.com:port1
    server app2, example.com:port2
    server app3, example.com:port3

    server example.com
    location app1, 反向代理到 example.com:port1,
    location app2, 反向代理到 example.com:port2,
    ...
    junnplus
        30
    junnplus  
       2018-01-10 19:48:39 +08:00
    @sunnogo 楼主说的是同一个域名同一个端口
    sunnogo
        31
    sunnogo  
       2018-01-11 09:51:24 +08:00
    @junnplus 谢谢这么快回复哈~。

    我的目标和楼主的也是一样,入口是同一个入口,通过 xxx.com/app1 xxx.com/app2 来区分业务。所以想问下我对你在 10 楼回复的理解是不是对的,通过 xxx.com/app1 访问,通过 location 反向代理到 xxx.com:port1。
    junnplus
        32
    junnplus  
       2018-01-11 11:49:41 +08:00
    @sunnogo 对的
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5451 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 02:57 PVG 10:57 LAX 18:57 JFK 21:57
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86