› NGINX
› NGINX Trac
› 3rd Party Modules
› Security Advisories
› CHANGES
› OpenResty
› ngx_lua
› Tengine
在线学习资源
› NGINX 开发从入门到精通
NGINX Modules
› ngx_echo
myyou
V2EX  ›  NGINX

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

  •  1
     
  •   myyou · Apr 16, 2016 · 17400 views
    This topic created in 3853 days ago, the information mentioned may be changed or developed.
    32 replies  •  2018-01-11 11:49:41 +08:00
    manhere
    manhere
    Apr 16, 2016 via iPhone❤️1
    1
    用不同入口 /目录啊
    penjianfeng
    penjianfeng
    Apr 16, 2016❤️2
    2
    你这问题让我也想问一个问题,如何让两个妹子同时爱上我....
    myyou
    myyou
    OP
    Apr 16, 2016
    3
    @manhere 能具体点吗?
    myyou
    myyou
    OP
    Apr 16, 2016
    5
    @penjianfeng 同时戳瞎两个妹子的眼\(^o^)/~
    ss098
    ss098
    Apr 16, 2016❤️1
    6
    可以设置 Cookie 或者不同应用之间的特征码以区别应用,比如有 A 和 B 两个应用,就可以设置 Cookie application=a ,然后将所有请求转发到应用中。
    myyou
    myyou
    OP
    Apr 16, 2016
    7
    @junnplus 请问这在 nginx 要怎么设置啊?
    wyssurvivor
    wyssurvivor
    Apr 16, 2016❤️1
    8
    在域名后面加路径,就像 4 楼说的那样
    qgy18
    qgy18
    Apr 16, 2016 via iPhone❤️1
    9
    @myyou 不同路径 proxy pass 到不同 backend 。
    junnplus
    junnplus
    Apr 16, 2016❤️2
    10
    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
    myyou
    OP
    Apr 16, 2016
    11
    @junnplus 谢谢
    bdbai
    bdbai
    Apr 16, 2016 via Android
    12
    @ss098 浏览器同时开两个应用会发生什么?
    ss098
    ss098
    Apr 16, 2016
    13
    @bdbai 同一个 Cookie 只能保持一个值啊,所以不会发生同时打开两个的情况,除非代码实现有问题。
    lux182
    lux182
    Apr 16, 2016
    14
    域名:目录:端口
    bdbai
    bdbai
    Apr 16, 2016 via Android
    15
    @ss098 这个最佳实践我给满分。
    RqPS6rhmP3Nyn3Tm
    RqPS6rhmP3Nyn3Tm
    Apr 16, 2016 via Android
    16
    上子域啊
    tobacco
    tobacco
    Apr 16, 2016❤️3
    17
    可以用请求 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
    kitalphaj
    Apr 16, 2016
    18
    如果你说的域名可以是 aaa.me.com 和 bbb.me.com 那就很简单。
    JerningChan
    JerningChan
    Apr 16, 2016
    19
    同域名,同端口,那就要不同路径了..
    JerningChan
    JerningChan
    Apr 16, 2016
    20
    你要把 3 个不同站点放到同一个 web 根目录入边
    yangxin0
    yangxin0
    Apr 16, 2016
    21
    http 里面有种东西叫 Host
    beginor
    beginor
    Apr 16, 2016 via Android
    22
    楼上正解, ng 可以配置多个 host
    Fedor
    Fedor
    Apr 16, 2016❤️3
    23
    server_name
    julyclyde
    julyclyde
    Apr 17, 2016
    24
    @yangxin0 lz 问的是同一个域名
    @beginor
    kaneg
    kaneg
    Apr 17, 2016
    25
    反向代理就是干这个活的
    beginor
    beginor
    Apr 17, 2016 via Android
    26
    同一个端口,同一个域名,又不想用 URL 或子域名,难道是要弄 load balance ?
    rupert
    rupert
    Apr 17, 2016 via iPhone
    27
    @beginor 也不是,你猜 哈哈哈
    myyou
    myyou
    OP
    Apr 18, 2016
    28
    @tobacco 谢谢
    sunnogo
    sunnogo
    Jan 10, 2018
    29
    @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
    junnplus
    Jan 10, 2018
    30
    @sunnogo 楼主说的是同一个域名同一个端口
    sunnogo
    sunnogo
    Jan 11, 2018
    31
    @junnplus 谢谢这么快回复哈~。

    我的目标和楼主的也是一样,入口是同一个入口,通过 xxx.com/app1 xxx.com/app2 来区分业务。所以想问下我对你在 10 楼回复的理解是不是对的,通过 xxx.com/app1 访问,通过 location 反向代理到 xxx.com:port1。
    junnplus
    junnplus
    Jan 11, 2018
    32
    @sunnogo 对的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3063 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 11:16 · PVG 19:16 · LAX 03:16 · JFK 06:16
    ♥ Do have faith in what you're doing.