栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

【浅】nginx.conf 配置之listen、server

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【浅】nginx.conf 配置之listen、server

关于虚拟主机server 的 listen 、server_name 配置:
  • listen 对应nginx虚拟主机对外提供服务的端口
  • server_name 可以理解成虚拟主机的名字,这个值会和http请求里的Host header做匹配

    所以
    1、在同一个nginx.conf配置文件里,只要 listen 、server_name组合起来唯一就没有问题;
#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;    
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;        
        location / {
            root   html;
            index  index.html index.htm;
        }        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }        
    }

    server {
        listen       80;
        server_name  www.nginxo.com;      
        location / {
            root   html;
            index  index2.html index2.htm;
        }
       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

2、server_name 这一行可以配置多个,中间用空格隔开,例如:

server {
        listen       80;
        server_name  www.nginxo.com www.nginxp.com;        

        location / {
            root   html;
            index  index1.html
        }
       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }       
    }

3、server_name 也可以配置通配符

server {
        listen       80;
        server_name  *.nginxo.com;        

        location / {
            root   html;
            index  index1.html
        }
       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }       
    }

4、server_name 也可以配置正则
用 ~ 符号表示正则匹配

server {
        listen       80;
        server_name  ~^[0-9]+.nginxo.com$; 

        location / {
            root   html;
            index  index1.html
        }
       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

server_name 匹配规则

从上到下,匹配到后不再继续匹配

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/881915.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号