Nit*_*nti 5 syntax reverse-proxy nginx url-rewriting
我在 nginx.conf 文件中有两个不同的 server_name:
第一个为:
server_name ~^(?<subdomain>.+)\.nithinveer\.com$;
location /
{
proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
access_log /var/log/nginx/true.log;
}
Run Code Online (Sandbox Code Playgroud)
另一个作为
server_name ~^(?<subdomain>.+)\.nithinveer\.com\.(?<extension>)$;
location /extension
{
proxy_pass http://192.168.6.190;
access_log /var/log/nginx/false.log;
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是我想同时使用基于 server_name 中的 server_name。如果 server_name 没有扩展名,它应该转到第一个位置。如果有分机,它应该转到第二个位置。
但是在运行 nginx 时,它并没有进入第二个 server_name
任何人都可以为此找到一些解决方案......?
我认为一个解决方案(可能是错误的)。
server_name ~^(?<subdomain>.+)\.nithinveer\.com\.(?<extension>.+)$;
if($<extension> == NULL)
{
location /
{
proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
access_log /var/log/nginx/true.log;
}
}
else
{ location /
{
proxy_pass http://192.168.6.190;
access_log /var/log/nginx/false.log;
}
Run Code Online (Sandbox Code Playgroud)
但是带有 if 语句的语法会引发错误。
nginx 中没有else指令。此外,您也不需要复制该位置。
尝试这个:
server {
server_name ~^(?<subdomain>.+).nithinveer\.com(?<extension>\..+)$;
location / {
This will match hosts terminating with ".com"
if ($extension = "")
{
proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
access_log /var/log/nginx/true.log;
}
This will match hosts with something after ".com", e.g: "foo.nithinveer.com.me", the $extension will be ".me"
if ($extension != "")
{
proxy_pass http://192.168.6.190;
access_log /var/log/nginx/false.log;
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外,请考虑if 是邪恶的。
这是可以安全使用的 版本:
server_name ~^(?<subdomain>.+).nithinveer\.com(?<extension>\..+)$;
location / {
error_page 418 = @with_extension;
#This will match hosts with something after ".com",
# e.g: "foo.nithinveer.com.me", the $extension will be ".me"
if ($extension != "")
{
return 418;
}
# This will match hosts terminating with ".com"
proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
access_log /var/log/nginx/true.log;
}
location @with_extension {
proxy_pass http://192.168.6.190;
access_log /var/log/nginx/false.log;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33564 次 |
| 最近记录: |