nginx基于主机名中的索引向不同应用程序反向代理
以前,我在DNSnginx基于主机名中的索引向不同应用程序反向代理,nginx,reverse-proxy,nginx-reverse-proxy,Nginx,Reverse Proxy,Nginx Reverse Proxy,以前,我在DNSstaging.example.com/后面有一个可以访问的登台环境。在这个地址后面是一个nginx代理,配置如下。请注意,我的代理或者重定向 到(s3后面)cloudfront分发(app1) 通过加载主机名(并考虑我的ALB能够根据主机名选择合适的应用程序)来实现负载均衡器(APP2) 现在,我的团队需要更多的暂存环境。我们还没有准备好过渡到docker部署(我们需要测试的每个分支能够生成一个完整的infra的最终目标…考虑到我们的团队规模,这有点过分了)我正在尝试使用一
staging.example.com/
后面有一个可以访问的登台环境。在这个地址后面是一个nginx代理,配置如下。请注意,我的代理或者重定向
- 到(s3后面)cloudfront分发(app1)
- 通过加载主机名(并考虑我的ALB能够根据主机名选择合适的应用程序)来实现负载均衡器(APP2)
索引的DNS名称,比如staging1.example.com
,staging2.example.com
。因此,我的nginx代理将接收带有主机头的请求,主机头看起来像staging{index_i}.example.com
我想做的是:
- 对于我的s3+Cloudfront应用程序,我正在考虑将我的文件嵌套在
[bucket\u id]/{index\u I}/[app1\u files]
下(以前它们直接位于根文件夹[bucket\u id]/[app1\u 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
变量(可能使用一些正则表达式?)
-如果是,如何使用索引i
有效地实现app1和app2块?在研究了其他几个问题后,我能够想出一个完美的配置:可以使用主机名中的正则表达式提取所述变量
另一方面,对于我的静态单页应用程序,为了使其与S3一起工作,我必须为每个“暂存索引”创建一个bucket(因为S3上的静态托管与website hosting/single index.html的工作方式是在404上使用)。这又使得在我(以前的单一)s3之前无法使用单一的Cloudfront发行版
下面是使用代理的示例,该代理具有create react应用程序前端和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;
}
服务器{
听80;
听443ssl;
服务器名称~^staging(?\d*).myjobglasses.com$
位置@create react应用程序前端{
代理通行证http://staging$staging_index.example.com.s3-website.eu-central-1.amazonaws.com;
}
位置@服务器端渲染应用程序{
#现在Amazon应用程序负载均衡器可以基于任何HTTP头重定向流量
代理集头示例-APP旧前端;
代理通行证https://staging$staging_index.myjobglass.com;
}
你能解释一下否决票吗?
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;
}