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块匹配规则遵循从上到下的规则,上面匹配到了,下面就不匹配了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了