登录  /  注册
首页 > 运维 > Nginx > 正文

Nginx主机域名配置如何实现

PHPz
发布: 2023-06-03 13:13:04
转载
1845人浏览过

一、配置多个端口访问不同文件

相同域名,不同端口,不同文件

#两个不同文件夹,分别存放不同文件
[root@<a style='color:#f60; text-decoration:underline;' href="//m.sbmmt.com/zt/16000.html" target="_blank">nginx</a> ~]# mkdir /www/work_01 -p
[root@nginx ~]# mkdir /www/work_02
[root@nginx ~]# vim /www/work_01/index.html 
this is work_01!
[root@nginx ~]# vim /www/work_02/index.html
this is work_02!
登录后复制

#编辑其中server模块,把端口80的站点指向一个文件夹,再复制这个server到下面,修改端口

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
#80端口,指向work_01的文件夹
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /www/work_01;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
#8080端口,指向work_02的文件夹
    server {
    listen 8080;
    server_name localhost;
    location / {
    root /www/work_02;
    index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}
}
登录后复制

#浏览器访问

Nginx主机域名配置如何实现

二、配置不同域名访问不同文件

相同端口,不同域名,不同文件

#四个文件夹,分别对应不同文件内容

[root@nginx ~]# cd /www/
[root@nginx www]# mkdir work_03
[root@nginx www]# mkdir work_04
[root@nginx www]# echo "This is work_03" > work_03/index.html
[root@nginx www]# echo "This is work_04" > work_04/index.html
[root@nginx www]# ls
work_01  work_02  work_03  work_04
登录后复制

#修改配置文件

[root@nginx www]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    sendfile        on;
    keepalive_timeout  65;
#通配符在后的域名
    server {
        listen       80;
        server_name  www.haha.*;
        location / {
            root   /www/work_01;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
#精确域名
    server {
    listen 80;
    server_name www.haha.com;
    location / {
    root /www/work_02;
    index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}
#通配符在前的域名
    server {
        listen 80;
        server_name *.haha.com;
    location / {
        root /www/work_03;
        index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}
#正则表达式域名
    server {
        listen 80;
        server_name ~\w+.com;
    location / {
        root /www/work_04;
        index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}
}
[root@nginx www]# systemctl restart nginx
登录后复制

#配置宿主机host文件,在"C:\Windows\System32\drivers\etc\hosts"

Nginx主机域名配置如何实现

#访问结果

Nginx主机域名配置如何实现

sever_name匹配顺序:

  • 精准匹配

  • 通配符开头,比如*.example.com

  • 通配符结尾,比如www.example.*

  • 正则表达式

  • 默认值

三、配置不同域名访问同个文件

相同端口,不同域名 ,同个文件

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
#只需要在server_name再添加一个域名,不需要在复制一个server_name
    server {
        listen       80;
        server_name  www.xixi.com www.qiqi.com;
        location / {
            root   /www/work_01;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx ~]# systemctl restart nginx
登录后复制

#该宿主机的host文件

Nginx主机域名配置如何实现

#访问结果如下:

Nginx主机域名配置如何实现

以上就是Nginx主机域名配置如何实现的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:亿速云网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号