【问题标题】:Separate website with same domain name by adding /organizationx path at the url通过在 url 添加 /organizationx 路径来分隔具有相同域名的网站
【发布时间】:2020-06-23 15:44:37
【问题描述】:

我有 2 个容器网站,分别监听 app1.com/ 和 app2.com/,它们可以有相同的端点。

我想为不同的组织创建一个 Nginx 代理:

  • http://<proxy-ip>/organisation1 从这里收听 app1.com/
  • http://<proxy-ip>/organisation2 从这里收听 app2.com/

用户应该看到http://<proxy-ip>/organisation1/movies 和所有css/js 被转发到http://<proxy-ip>/organisation1/css/a.css

应用中的位置是app1.com/movies和app1.com/css/a.css等。

问题是重写方法没有转发/organisationX。

如果该位置在实际应用中不存在,如何添加并将其转发到 URL?

server {
   listen 80;
   location /organisation1 {
       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_set_header   X-Forwarded-Proto $scheme;
       proxy_set_header   X-Forwarded-Host $host/organisation1;
       proxy_pass         http:app1.com

   }
   location /organisation2 {
       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_set_header   X-Forwarded-Proto $scheme;
       proxy_set_header   X-Forwarded-Host $host/organisation2;
       proxy_pass         http:app2.com
   }
}

【问题讨论】:

  • 所以最终的容器应该这样调用http://app.com/movies和http://app.com/css/a.css?现在发生了什么?你的问题我不清楚

标签: docker nginx kubernetes nginx-location nginx-reverse-proxy


【解决方案1】:

ngx_http_rewrite_module会帮你

见手册:

如果指定的正则表达式与请求 URI 匹配,则 URI 将按照 _replacement_ 字符串中的指定进行更改。

试试这个代码:

server {
   listen 80;
   location /organisation1 {
       rewrite ^/organisation1/(.*) /$1 break;       
       ...
       proxy_pass         http:app1.com

   }
   location /organisation2 {
       rewrite ^/organisation2/(.*) /$1 break;       
       ...
       proxy_pass         http:app2.com
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    相关资源
    最近更新 更多