OpenResty 通过二级域名做跳转
if ( $host ~* (\b(?!www\b)\w+)\.\w+\.\w+ ) {
#获取nba.test.com域名中的nba
set $subdomain $1;
}
location / {
rewrite_by_lua '
--只有二级域名不是www或空才做跳转
if ngx.var.subdomain ~= "www" and ngx.var.subdomain ~= "" then
--可以根据二级域名来传不同的参数
local id = ""
if ngx.var.subdomain == "a" then
id = "1"
else
id = "2"
end
--302为ngx.redirect的参数,ngx.redirect可以发出一个http 301或302的重定向
return ngx.redirect("/c/index.html?id="..id,302)
end
';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend;
}
location ~.*\.(html|htm)$
{
root html;
index index.html index.htm;
}