如何使用NGINX proxy_pass
重定向到S3静态网站?
http://subdomain.mydomain.com
应该代理到http://subdomain.s3-website-us-east-1.amazonaws.com
一个重要的要求是subdomain
必须动态(这意味着需要使用变量)
下面的示例在没有变量的情况下工作:
server {
server_name sudomain.mydomain.com;
location / {
proxy_pass http://subdomain.s3-website-us-east-1.amazonaws.com
}
}
答案 0 :(得分:2)
我会尝试类似的东西:
server_name *.mydomain.com;
...
location / {
proxy_pass $scheme://$host.s3-website-us-east-1.amazonaws.com$request_uri
}
这里的人做了出色的工作,深入描述了所需的所有细节
答案 1 :(得分:0)
您可以使用access_by_lua
(或access_by_lua_file
)设置一个变量,然后可以在proxy_pass
指令中使用该变量。
这是我的PoC nginx.conf
的摘录:
# Default upstream variable; blank is the best option *in my case*.
set $upstream '';
# Run the script to figure out the required upstream.
access_by_lua_file upstream.lua;
# The script sets the $upstream variable for this request.
proxy_pass http://$upstream;
还有upstream.lua
中的相关位:
-- 'val' is set by business rules
-- (left as an exercise for the reader)
ngx.var.upstream = val