nginx ServerName匹配规则

1、同一个主机配置不同端口,访问不同资源

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;     
        server_name  localhost;    #localhosts:80端,访问/www/www/index.com
        location / {
            root   /www/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       88;
        server_name  localhost;
        location / {
            root   /www/vod;  
            index  index.html index.htm;     # localhost:88,访问/www/vod/index.html
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

 

 2、同一个端口,不同server_name

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.example.com;
        location / {
            root   /www/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  test.example.com;
# server_name ~^(0-9)+\.example.com; location
/ { root /www/vod; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

注意: 这里的www.example.com 和 test.example.com两个域名可以解析到同一个IP,只要listen端口+server_name唯一即可实现资源路由。

           一个server块也可以配置多个值,指向同一个资源,即可以使用通配符和正则,前提是域名有做dns解析

           server块匹配规则遵循从上到下的规则,上面匹配到了,下面就不匹配了

 

 

posted @ 2023-01-03 16:21  高佳丰  阅读(486)  评论(0编辑  收藏  举报