2 Star 1 Fork 0

enilu / book

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Nginx根据host信息转发到不同的站点.md 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
zhangtao 提交于 2020-12-08 18:51 . Add init

Nginx根据host信息转发到不同的站点

upstream xxxx{
        server host1:port1;//或者配置为域名wwww.xxx.com
}
upstream mmmm{
        server host2:port2;//或者配置为域名wwww.mmmm.com
}
server {
    listen       80;
    server_name  www.xxx.com;

    access_log  /data/app/runtime/log/nginx/apijn.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        try_files $uri @apijn;
    }
    location @www{
                proxy_ignore_client_abort on;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_read_timeout 600s;
                //$http_${host信息参数名}
                if ($http_xxx = "www.xxx.com") {
                        proxy_pass http://xxxx;
                }
                proxy_pass http://mmmm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
  • 调用方设置头信息
Map header = Maps.newHashMap("host_name", "www.xxx.com")
HttpClientUtil.post(url, params, header);
Map header2 = Maps.newHashMap("host_name", "www.mmm.com")
HttpClientUtil.post(url, params, header2);
  • 注意上面的host_name值要和nginx配置中$http_后面的值一致
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/enilu/book.git
git@gitee.com:enilu/book.git
enilu
book
book
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891