随笔 - 2  文章 - 0  评论 - 0  阅读 - 5178 

同一台nginx服务器通过配置多个server块实现在同一端口号下监听多个域名。

需要注意的是:端口号(listen)+主机名(server_name) 需要在多个server中唯一,否则会报错。

实现效果:分别访问one.lyj.com 和 two.lyj.com获取不同的资源 

复制代码
#user  nobody;
# 工作进程数量
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


# 每个worker创建连接数
events {
    worker_connections  1024;
}


http {
    # 引入文件   mime.types里配置的是文件会以何种方式返回给客户端
    include       mime.types;
    # 默认的返回方式
    default_type  application/octet-stream;

    # 数据0拷贝
    sendfile        on;
    #tcp_nopush     on;

    # 保持长链接时间
    keepalive_timeout  65;


    # 虚拟主机 vhost  一台nginx可以配置多个server
    server {
        # 监听端口
        listen       80;
        # 配置域名、主机名  域名需要备案和配置dns解析
        server_name  one.lyj.com;

        # location用来匹配uri(资源)  eg: url为: http://liyijun.com/learnnginx/index.html ; uri就是:/learnnginx/index.html
        # 一个server可以配置多个location
        location / {
            # 资源的相对路径 eg:html就是在nginx主目录下的html文件夹下
            root   /www/one;
            # 默认展示页面
            index  index.html index.htm;
        }

        # 遇到错误页面码转到 /50x.html下 下面的location会将/50x.html转到html文件夹下去寻找50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


        # 监听多个端口
    server {
        # 监听端口
        listen       80;
        # 配置域名、主机名 多个域名之间用空格间隔开
        server_name  two.lyj.com three.lyj.com;

        # location用来匹配uri(资源)  eg: url为: http://liyijun.com/learnnginx/index.html ; uri就是:/learnnginx/index.html
        # 一个server可以配置多个location
        location / {
            # 资源的相对路径 eg:html就是在nginx主目录下的html文件夹下
            root   /www/two;
            # 默认展示页面
            index  index.html index.htm;
        }

        # 遇到错误页面码转到 /50x.html下 下面的location会将/50x.html转到html文件夹下去寻找50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}
复制代码

 参考:16-基本使用-Nginx虚拟主机域名配置_哔哩哔哩_bilibili

posted on   悟已往之不谏  阅读(5022)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示