【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文件

#访问结果如下:

相关内容

热门资讯

安卓系统怎么调日期显示,安卓系... 手机里的日期显示不对了?别急,我来教你一招,让你的安卓手机日期显示变得刚刚好!?一、打开设置,找到日...
苹果手机安卓系统功能 你有没有发现,最近身边的朋友都在讨论手机呢?尤其是苹果手机和安卓系统,简直是两大热门话题。今天,我就...
安卓12系统好用不好 最近手机圈可是热闹非凡呢!安卓12系统终于发布了,各大品牌纷纷宣布自家新机将搭载这个新系统。那么,安...
如何安装安卓车机系统,安卓车机... 你有没有想过,你的爱车也能拥有智能的“大脑”?没错,就是那种可以导航、听歌、看视频的安卓车机系统!想...
鸿蒙系统来自安卓架构吗,源自安... 你有没有想过,我们平时使用的手机操作系统,其实背后有着复杂的故事呢?就拿华为的鸿蒙系统来说吧,它是不...
升级安卓12系统耗电快,揭秘原... 最近是不是发现你的安卓手机升级到12系统后,电量消耗得特别快?别急,这可不是个例哦!今天就来聊聊这个...
安卓系统ROM精简版,极致性能... 你有没有发现,手机用久了,系统越来越卡,有时候甚至有点儿像老牛拉破车,慢吞吞的。别急,今天就来给你揭...
kindle系统与安卓下载,无... 亲爱的读者们,你是否也有过这样的经历:手捧一本厚重的纸质书,却因为旅途的颠簸而不得不放下,或者是在夜...
安卓手机系统改169 你有没有发现,你的安卓手机系统突然变得有点“慢吞吞”的?别急,别急,今天就来给你揭秘如何让你的安卓手...
安卓未来系统是什么 你知道吗?最近科技圈可是炸开了锅,都在热议安卓未来的系统会是什么样子。想象未来的手机操作系统,是不是...
安卓操作系统命名规则,从“糖果... 你知道吗?安卓操作系统的命名规则可是隐藏着不少趣味和奥秘呢!每次新版本的发布,都会给我们带来惊喜,同...
安卓原生系统电视盒子,探索安卓... 亲爱的读者们,你是否曾想过,家里的电视盒子也能像智能手机一样,拥有流畅的操作体验和丰富的应用生态?今...
安卓手机系统好评率,用户满意度... 你有没有发现,现在安卓手机简直成了街机啊!大街小巷,无论是年轻人还是老年人,几乎人手一部安卓手机。那...
安卓系统有没有手绘软件,手绘软... 你有没有想过,在安卓系统上,是不是也能像在苹果手机上那样,轻松地画出那些五彩斑斓的画作呢?今天,就让...
安卓自己刷系统的软件,揭秘热门... 你有没有想过,你的安卓手机其实可以像换衣服一样,随时换上新的系统?没错,就是那种完全改变手机面貌的感...
cf分不分苹果系统和安卓系统,... 你有没有发现,现在手机市场上,苹果系统和安卓系统就像是一对“欢喜冤家”,总是被拿来比较。这不,最近就...
安卓最新系统版本壁纸,壁纸背后... 亲爱的手机控们,你是否已经迫不及待想要一睹安卓最新系统版本的芳容了呢?没错,今天我要带你一起探索这个...
怎样让安卓系统变成个s系统,轻... 你有没有想过,你的安卓手机能不能也来点个性,变成那个酷炫的S系统呢?别急,今天就来手把手教你,怎样让...
安卓平板如何双系统,打造个性化... 你有没有想过,你的安卓平板可以同时运行两个操作系统呢?没错,就是那种一个平板,两个世界的感觉!今天,...
忠诚卫士安装安卓系统 你有没有想过,你的忠诚卫士手机,是不是也能像电脑一样,装上各种有趣的安卓系统呢?没错,今天就要来给你...