nginx中IF语句的语法
我在nginx.conf文件中有两个不同的服务器名称:nginx中IF语句的语法,nginx,syntax,url-rewriting,reverse-proxy,Nginx,Syntax,Url Rewriting,Reverse Proxy,我在nginx.conf文件中有两个不同的服务器名称: 第一个是: server_name ~^(?<subdomain>.+)\.nithinveer\.com$; location / { proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/; access_log /
第一个是:
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;
}
server\u name~ ^(?.+)\.nithinveer\.com$;
位置/
{
代理通行证http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
访问\u log/var/log/nginx/true.log;
}
另一个是
server_name ~^(?<subdomain>.+)\.nithinveer\.com\.(?<extension>)$;
location /extension
{
proxy_pass http://192.168.6.190;
access_log /var/log/nginx/false.log;
}
server\u name~ ^(?.+)\.nithinveer\.com\.(?)$;
位置/扩展
{
代理通行证http://192.168.6.190;
访问\u log/var/log/nginx/false.log;
}
现在的问题是,我想在服务器名称中使用基于的服务器名称。如果没有服务器名称为的扩展,则应转到第一个位置。如果有分机,则应转到第二个位置。但是在运行nginx时,它并没有移动到第二个服务器名称中
任何人都能找到解决这个问题的方法吗?
我认为解决办法(可能是错误的)
server\u name~ ^(?.+)\.nithinveer\.com\.(?.+)$;
如果($==NULL)
{
位置/
{
代理通行证http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
访问\u log/var/log/nginx/true.log;
}
}
其他的
{位置/
{
代理通行证http://192.168.6.190;
访问\u log/var/log/nginx/false.log;
}
但是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;
}
}
}
服务器{
服务器名称~^(?.+).nithinveer\.com(?\..+);
地点/{
这将匹配以“.com”终止的主机
如果($extension=”“)
{
代理通行证http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
访问\u log/var/log/nginx/true.log;
}
这将使主机与“.com”后面的内容匹配,例如:“foo.nithinveer.com.me”,扩展名为“.me”
如果($extension!=“”)
{
代理通行证http://192.168.6.190;
访问\u log/var/log/nginx/false.log;
}
}
}
也考虑一下。 这是安全使用的版本:
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;
}
server_name~ ^(?.+).nithinveer\.com(?\..+);
地点/{
错误\u第418页=@带有\u扩展名;
#这将使主机与“.com”之后的内容匹配,
#例如:“foo.nithinveer.com.me”,扩展名为“.me”
如果($extension!=“”)
{
返回418;
}
#这将匹配以“.com”终止的主机
代理通行证http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
访问\u log/var/log/nginx/true.log;
}
位置@,扩展名为{
代理通行证http://192.168.6.190;
访问\u log/var/log/nginx/false.log;
}
在if条件下,您不需要“并在删除“\”后尝试”。Com之后,您可以将这两个服务器名称合并为一个,就像是邪恶的一样,具有与上述相同的功能哪两个服务器名称?我更新的答案只有一个。邪恶的部分是在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;
}