NGINX 不尊重 server_name 正则表达式

时间:2020-12-26 10:30:04

标签: nginx-config

我有这个 nginx 配置..我希望它接受所有包含“竞争”一词并以 .com.au 结尾的域。 .. server_name 是否因为我使用代理而被忽略?

  server {
    listen 80 default_server;
    server_name ~^(.+)competitions?(.+)\.com\.au;
    access_log /var/log/nginx/$host.access.log;
    error_log /var/log/nginx/error.log;
    if ($host !~* ^www){
            rewrite ^/(.*)$ https://www.$host/$1 permanent;
    }
  location / {
            proxy_no_cache 1;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8888;
            try_files $uri $uri/ @proxy;
    }

    location @proxy {
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8888;
            proxy_intercept_errors on;
            recursive_error_pages on;
            error_page 404 = @rewrite_proxy;
    }

    location @rewrite_proxy {
            rewrite /(.*) /index.cfm?path=$1;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8888;
    }

}

1 个答案:

答案 0 :(得分:2)

您必须从那里删除 default_server,因为这是一个包罗万象的指令。
您仍然可以使用 server 设置另一个 default_server指令,如果需要。

参见How nginx processes a request以获得更详细的解释:

<块引用>

如果它的值与任何服务器名称都不匹配,或者请求根本不包含此头字段,则 nginx 会将请求路由到此端口的默认服务器。