【Nginx】Nginx主机域名配置
创始人
2024-06-02 19:43:19
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文件

#访问结果如下:

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...