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

#访问结果如下:

相关内容

热门资讯

安卓系统车机互联软件 你有没有发现,现在开车的时候,手机和车机之间的互动越来越频繁了呢?想象你正驾驶着爱车,一边享受着音乐...
荣耀play4安卓系统,智能生... 你有没有发现,最近手机圈里又掀起了一股热潮?没错,就是荣耀Play4这款新机!它不仅外观时尚,性能强...
安卓模拟苹果多开系统,打造跨平... 你有没有想过,在安卓手机上也能享受到苹果系统的多开功能呢?没错,这就是今天我要跟你分享的神奇话题——...
广汽埃安是安卓系统吗,搭载安卓... 你有没有想过,你的爱车广汽埃安,它的操作系统是不是安卓呢?这个问题,估计不少车友都在心里默默琢磨过。...
安卓系统是google的吗,谷... 你有没有想过,安卓系统是不是谷歌的亲生孩子呢?这个问题,就像是在问苹果是不是苹果树的宝宝一样,让人好...
安卓系统的设置程序下载,轻松掌... 你有没有发现,手机里的安卓系统设置程序真是五花八门,让人眼花缭乱?今天,就让我带你一起探索这个神秘的...
安卓系统的手机家园,打造个性化... 你有没有发现,现在手机的世界里,安卓系统的手机家园可是越来越热闹了呢!想象每天打开手机,就像走进了一...
安卓系统哪些相机可以调,支持实... 你有没有发现,手机拍照已经成为我们生活中不可或缺的一部分呢?无论是记录生活的点滴,还是捕捉美丽的瞬间...
安卓手机系统英文变中文,And... 你有没有发现,你的安卓手机上那些英文字母的系统提示,突然间变成了熟悉的中文呢?是不是觉得瞬间亲切感倍...
系统语言可以删除吗安卓,基于系... 你有没有想过,手机里的那些系统语言,是不是有时候觉得有点多余呢?比如,你是个地道的英语使用者,却总是...
安卓系统收取专利吗,揭秘科技巨... 你有没有想过,那个陪伴你每天刷手机、玩游戏、办公的安卓系统,它背后是不是也有自己的“小金库”呢?没错...
安卓屏幕怎么更新系统,轻松掌握... 亲爱的安卓手机用户们,你是不是也和我一样,时不时地想给手机来个“大变身”,让它焕然一新呢?没错,说的...
安卓系统有没有类似carpla... 你有没有想过,在安卓系统上,能不能也像苹果用户那样,轻松连接汽车音响系统呢?没错,我说的就是那个神奇...
安卓系统哪里升级包,生成与更新... 亲爱的安卓用户们,你是不是也和我一样,时不时地想给手机来个“大变身”,让它焕然一新呢?那就得提到那个...
安卓手机怎么恢复老系统,轻松恢... 手机用久了,是不是觉得系统越来越卡,功能也越来越不跟得上潮流了呢?别急,今天就来教你怎么把安卓手机恢...
安卓系统如何安装windows... 亲爱的安卓用户们,你是否曾幻想过在手机上体验Windows 7的韵味?别再羡慕那些拥有Windows...
旧的安卓系统怎么备份,轻松保存... 亲爱的安卓用户们,你是否曾经因为系统升级或者手机意外重启而担心丢失了珍贵的照片、联系人或者应用数据呢...
安卓手机系统文件被破坏,全面解... 手机突然间罢工了,是不是很崩溃?别急,今天就来聊聊安卓手机系统文件被破坏的那些事儿。相信我,掌握了这...
街头霸王四安卓系统,畅享格斗盛... 你知道吗?最近在安卓系统上,有一款游戏可是火得一塌糊涂,那就是《街头霸王四》!这款经典格斗游戏在安卓...
阿里tv安卓系统刷机,畅享智能... 你有没有发现,家里的阿里TV用久了,系统有点卡卡的呢?别急,今天就来教你怎么给它来个焕然一新的刷机大...