[以前,我在DNS staging.example.com/
后面可以访问一个临时环境。该地址的后面是具有以下配置的Nginx代理。请注意,我的代理可以重定向
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_i
,staging1.example.com
创建了一些带有staging2.example.com
的其他DNS名称。因此,我的Nginx代理将接收带有主机标头的请求,该标头类似于staging#{index_i}.example.com
我正在考虑做的事情:
[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块?
这里是使用具有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;
}