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

2016 年 4 月 16 日
 myyou
16971 次点击
所在节点    NGINX
32 条回复
manhere
2016 年 4 月 16 日
用不同入口 /目录啊
penjianfeng
2016 年 4 月 16 日
你这问题让我也想问一个问题,如何让两个妹子同时爱上我....
myyou
2016 年 4 月 16 日
@manhere 能具体点吗?
junnplus
2016 年 4 月 16 日
myyou
2016 年 4 月 16 日
@penjianfeng 同时戳瞎两个妹子的眼\(^o^)/~
ss098
2016 年 4 月 16 日
可以设置 Cookie 或者不同应用之间的特征码以区别应用,比如有 A 和 B 两个应用,就可以设置 Cookie application=a ,然后将所有请求转发到应用中。
myyou
2016 年 4 月 16 日
@junnplus 请问这在 nginx 要怎么设置啊?
wyssurvivor
2016 年 4 月 16 日
在域名后面加路径,就像 4 楼说的那样
qgy18
2016 年 4 月 16 日
@myyou 不同路径 proxy pass 到不同 backend 。
junnplus
2016 年 4 月 16 日
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
2016 年 4 月 16 日
@junnplus 谢谢
bdbai
2016 年 4 月 16 日
@ss098 浏览器同时开两个应用会发生什么?
ss098
2016 年 4 月 16 日
@bdbai 同一个 Cookie 只能保持一个值啊,所以不会发生同时打开两个的情况,除非代码实现有问题。
lux182
2016 年 4 月 16 日
域名:目录:端口
bdbai
2016 年 4 月 16 日
@ss098 这个最佳实践我给满分。
RqPS6rhmP3Nyn3Tm
2016 年 4 月 16 日
上子域啊
tobacco
2016 年 4 月 16 日
可以用请求 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
2016 年 4 月 16 日
如果你说的域名可以是 aaa.me.combbb.me.com 那就很简单。
JerningChan
2016 年 4 月 16 日
同域名,同端口,那就要不同路径了..
JerningChan
2016 年 4 月 16 日
你要把 3 个不同站点放到同一个 web 根目录入边

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://study.congcong.us/t/271492

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX