Nginx server name 规则

Nginx About 1,689 words

测试域名

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

127.0.0.1       test1.com
127.0.0.1       test2.com
127.0.0.1       test3.com
127.0.0.1       test4.com

server_name 测试配置

请求test1.com/test2.com会定位到第一/第二条server规则。

请求test3.com/test4.com会匹配添加了default_server的规则。

如果都没有设置default_server指令,则第一条server规则就会是默认规则。

http {
    server {
        listen 80;
        server_name test1.com;
        location / {
            default_type text/html;
            return 200 'test1.com';
        }
    }

    server {
        listen 80;
        server_name test2.com;
        location / {
            default_type text/html;
            return 200 'test2.com';
        }
    }

    server {
        listen 80;
        server_name "";
        location / {
            default_type text/html;
            return 200 'empty server name';
        }
    }

    server {
        listen 80;
        server_name _;
        location / {
            default_type text/html;
            return 200 'underscore server name';
        }
    }

    server {
        listen 80 default_server;
        location / {
            default_type text/html;
            return 200 'default_server';
        }
    }
}

设置多个 default_server

多个default_server指令会报错

nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /opt/homebrew/etc/nginx/nginx.conf:62
nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test failed

默认 server_name

如果server_name设置为"",或者不设置server_name,则匹配所有可能的域名到这条location

如果""和不设置server_name,会得到以下警告,但不影响启动。

nginx: [emerg] directive "server_name" is not terminated by ";" in /opt/homebrew/etc/nginx/nginx.conf:74

参考

https://nginx.org/en/docs/http/server_names.html

Views: 545 · Posted: 2024-01-29

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh