Nginx代理服务器基于域名到S3静态网站

时间:2018-12-28 15:57:46

标签: nginx

如何使用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
   }
}

2 个答案:

答案 0 :(得分:2)

我会尝试类似的东西:

 server_name *.mydomain.com;

 ...
 location / {
   proxy_pass $scheme://$host.s3-website-us-east-1.amazonaws.com$request_uri
 }

这里的人做了出色的工作,深入描述了所需的所有细节

https://serverfault.com/questions/706438/what-is-the-difference-between-nginx-variables-host-http-host-and-server-na

答案 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