Nginx主机域名配置实现
目录
- 一、配置多个端口访问不同文件
- 二、配置不同域名访问不同文件
- 三、配置不同域名访问同个文件
一、配置多个端口访问不同文件
相同域名,不同端口,不同文件
#两个不同文件夹,分别存放不同文件 [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匹配顺序:
- 精准匹配
- 通配符开头,比如*.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文件

#访问结果如下:

您可能感兴趣的文章
- 12-20Kubernetes中使用临时容器进行故障排查的方法
- 12-20Nginx设置HTTPS的方法步骤
- 12-20二进制方式安装 Kubernetes1.18.3版本实现脚本
- 12-20Nginx工作模式及代理配置的使用细节
- 12-20ZooKeeper分布式协调服务设计核心概念及安装配置
- 12-20Kubernetes部署可视化地图的十个步骤
- 12-20关于docker清理Overlay2占用磁盘空间的问题(亲测有效)
- 12-20Docker compose配置文件写法及命令使用示例
- 12-20openwrt安装docker并启动的操作方法
- 12-20云原生Kubernetes初始化容器Init使用教程


阅读排行
推荐教程
- 12-07一文教你怎么选择Tomcat对应的JDK版本
- 12-07新版Eclipse集成Tomcat时找不到server选项的解决方法
- 12-06IIS7 应用程序池自动回收关闭的解决方案
- 12-05Windows Server 2019安装VMware
- 12-05Windows服务器默认IE浏览器无法下载文件的解决方法
- 12-05Docker安装Jenkins全过程
- 12-19Zabbix SAML SSO 登录绕过漏洞的操作流程
- 12-15Docker-Compose搭建Spark集群的实现方法
- 12-14Docker Desktop无法正常启动解决(failed to start...)
- 12-14k8s 与docker空间使用分析与清理方法





