【Nginx】Nginx主机域名配置
创始人
2024-06-02 19:26:44
0

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

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

#两个不同文件夹,分别存放不同文件
[root@nginx ~]# 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;}
}
}

#浏览器访问

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

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

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

[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"

#访问结果

sever_name匹配顺序:

  1. 精准匹配

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

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

  1. 正则表达式

  1. 默认值

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

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

[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_nameserver {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文件

#访问结果如下:

相关内容

热门资讯

恋夜视频安卓系统Uc,恋夜视频... 亲爱的读者,你是否曾在深夜时分,被手机屏幕上跳动的视频吸引?今天,就让我带你一探究竟,揭开恋夜视频安...
鸿蒙套娃安卓系统视频,融合与创... 你知道吗?最近科技圈可是炸开了锅,因为华为的新操作系统鸿蒙OS又有了新动作。这不,他们竟然把鸿蒙套娃...
xp系统连接安卓手机问题,实用... 你有没有遇到过这样的情况:你的电脑上还运行着那个经典的XP系统,而你的安卓手机却时不时地想要和你亲密...
压缩安卓系统储存空间,高效管理... 手机里的照片越来越多,游戏也越玩越上瘾,可这安卓系统的储存空间却越来越紧张,是不是感觉像是在挤牙膏?...
安卓手游转苹果系统教程,轻松实... 你是不是也和我一样,手头有一堆安卓手游,突然之间想换换口味,体验一下苹果系统的魅力呢?别急,今天就来...
安卓原生系统锁屏暗,安卓系统锁... 亲爱的手机控们,你是否曾为安卓手机锁屏时的暗模式而感到好奇?那种在夜晚或光线不足的环境中,屏幕自动调...
安卓系统表情包下载地址,安卓系... 你是不是也和我一样,对安卓系统的表情包爱不释手?那些搞笑的、可爱的、甚至是有点小调皮的表情,总能让我...
原生安卓系统声音bug,揭秘那... 你有没有遇到过这种情况?手机里突然传来一阵奇怪的声音,让你瞬间从美梦中惊醒,或者正在专心工作时被打扰...
水果收银机安卓系统,便捷高效的... 你有没有想过,在繁忙的超市里,那些摆满新鲜水果的摊位,背后竟然隐藏着一个小小的科技秘密?没错,就是那...
安卓系统变苹果界面了吗,苹果界... 最近手机界可是炸开了锅,不少安卓用户都在议论纷纷:“安卓系统变苹果界面了吗?”这事儿可真不简单,得好...
miui操作系统与安卓系统吗,... 亲爱的读者,你是否曾在手机上看到过MIUI操作系统和安卓系统这两个名字,好奇它们之间有什么区别?今天...
安卓系统怎么卡道具界面,探究原... 手机用久了,是不是感觉安卓系统越来越卡?尤其是那个道具界面,点开就慢吞吞的,真是让人头疼。别急,今天...
安卓系统红包加速器,畅享无阻新... 你有没有发现,现在用手机抢红包简直是一场速度与激情的较量?别急,别急,让我来给你揭秘一款神器——安卓...
安卓经典版系统更新时间,从首次... 你有没有发现,最近你的安卓手机又悄悄地变了个样?没错,就是那个陪伴我们多年的经典版系统,它又来更新啦...
安卓系统开发要多久,约需1-2... 你有没有想过,自己动手开发一个安卓应用,究竟需要多长时间呢?这可是个让人好奇的问题,毕竟安卓系统开发...
原生安卓系统手机壁纸图片,探索... 亲爱的手机控们,你是否曾为寻找一款独特的壁纸而烦恼?今天,就让我带你走进原生安卓系统手机壁纸的奇幻世...
bmw安卓互联系统,智能驾驶新... 你有没有发现,现在开车已经不仅仅是驾驶那么简单了?一辆好车,还得有个好“大脑”,这样才能让你的驾驶体...
安卓手机升级系统卡吗,安卓手机... 你有没有遇到过这种情况:安卓手机升级系统后,突然感觉手机像蜗牛一样慢吞吞的,心里那个急啊!今天,就让...
无线麦克风安卓系统,轻松实现无... 你有没有想过,在一场热闹的K歌派对或者重要的演讲场合,无线麦克风简直就是救星啊!想象你手握麦克风,自...
怎么重新定制安卓系统,打造专属... 你有没有想过,你的安卓手机其实可以变得独一无二,就像是你自己的小宇宙一样?没错,就是重新定制安卓系统...