[nginx反向代理基于主机名中的索引到不同的应用程序

问题描述 投票:-1回答:1

[以前,我在DNS staging.example.com/后面可以访问一个临时环境。该地址的后面是具有以下配置的Nginx代理。请注意,我的代理可以重定向

  • 到(在s3之后的)cloudfront发行版(app1)
  • 通过转发主机名到负载平衡器(让我们考虑我的ALB能够根据主机名选择合适的应用程序)(app2)
server {
  listen 80;
  listen 443 ssl;
  server_name
      staging.example.com
  ;

  location / {
    try_files /maintenance.html @app1;
  }

  location ~ /(faq|about_us|terms|press|...) {
      try_files /maintenance.html @app2;
  }

  [...] # Lots of similar config than redirects either to app1 or app2

  # Application hosted on s3 + CloudFront
  location @app1 {
    proxy_set_header Host app1-staging.example.com;
    proxy_pass http://d2c72vkj8qy1kv.cloudfront.net;
  }

  # Application hosted behind a load balancer
  location @app2 {
    proxy_set_header Host app2-staging.example.internal;
    proxy_set_header X-ALB-Host $http_host;
    proxy_pass https://staging.example.internal;
  }
}

现在,我的团队需要更多的临时环境。我们尚未准备好过渡到docker部署(鉴于我们的团队规模,能够生成需要测试的完整分支基础设施的最终目标...有点过头了),而我正尝试提出一些建议而是使用技巧,以便我们可以轻松地获得更多的暂存环境使用大致相同的nginx配置

假设我使用index_istaging1.example.com创建了一些带有staging2.example.com的其他DNS名称。因此,我的Nginx代理将接收带有主机标头的请求,该标头类似于staging#{index_i}.example.com

我正在考虑做的事情:

  • 对于我的s3 + Cloudfront应用程序,我正在考虑将文件嵌套在[bucket_id]/#{index_i}/[app1_files]下(以前它们直接位于根文件夹[bucket_id]/[app1_files]中]
  • 对于我的负载平衡器应用程序,让我们假设我的负载平衡器知道在哪里分派https://staging#{iindex_i}.example.com请求。

我正在尝试拉类似的东西

# incoming host : staging{index_i}.example.com`
server {
  listen 80;
  listen 443 ssl;
  server_name
      staging.example.com
      staging1.example.com 
      staging2.example.com # I can list them manually, but is it possible to have something like `staging*.example.com` ?
  ;
[...]
location @app1 {
    proxy_set_header Host app1-staging$index_i.example.com; # Note the extra index_i here
    proxy_pass http://d2c72vkj8qy1kv.cloudfront.net/$index_i; # Here proxy_passing to a subfolder named index_i
  }

  location @app2 {
    proxy_set_header Host app2-staging$index_i.example.internal; # Note the extra index_i here
    proxy_set_header X-ALB-Host $http_host;
    proxy_pass http://staging$index_i.example.internal; # Here I am just forwarding the host header basically
  }

所以最终我的问题是-当我的Nginx服务器收到连接时,我可以从请求主机头中提取index_i变量(也许使用一些正则表达式吗?)-如果是,如何使用index_i有效地实现app1和app2块?

nginx reverse-proxy nginx-reverse-proxy
1个回答
0
投票
不利的一面是,对于我的静态单页应用程序,要使其与S3一起使用,我必须为每个“登台索引”创建一个存储桶(因为S3上的静态托管与网站托管一起工作的方式/一个index.html (用于404)。这又使得在我的(以前是单个)s3之前无法使用单个Cloudfront发行版。

这里是使用具有create-react-app前端和ALB后面的服务器端呈现的代理的示例

server { listen 80; listen 443 ssl; server_name ~^staging(?<staging_index>\d*).myjobglasses.com$ location @create-react-app-frontend { proxy_pass http://staging$staging_index.example.com.s3-website.eu-central-1.amazonaws.com; } location @server-side-rendering-app { # Now Amazon Application Load Balancer can redirect traffic based on ANY HTTP header proxy_set_header EXAMPLE-APP old-frontend; proxy_pass https://staging$staging_index.myjobglasses.com; }

© www.soinside.com 2019 - 2024. All rights reserved.