Nginx 核心配置-单节点实现多域名访问
Nginx核心配置-单节点实现多域名访问
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.试验环境说明
1>.虚拟机环境说明
[root@node101.yinzhengjie.org.cn ~]# uname -r
3.10.-.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# uname -m
x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release
CentOS Linux release 7.6. (Core)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# grep 172.30.1.101 /etc/hosts
172.30.1.101 pc.yinzhengjie.org.cn mobile.yinzhengjie.org.cn mail.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~]#
2>.试验环境说明
在同一台web服务器上基于不同的主机名访问不同的网页内容,具体要求如下:
访问pc.yinzhengjie.org.cn会得到一个网页内容:
网页内容自定义。
访问mobile.yinzhengjie.org.cn和mail.yinzhengjie.org.cn内容相同:
网页内容自定义即可。
3>.Nginx源码方式安装步骤
博主推荐阅读:
https://www.cnblogs.com/yinzhengjie/p/12031651.html
二.编辑配置文件并重启服务
1>.编辑nginx的主配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes ;
worker_cpu_affinity ; events {
worker_connections ;
use epoll;
accept_mutex on;
multi_accept on;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
charset utf-;
keepalive_timeout ;
server {
listen ;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
}
} #导入其他路径的配置文件
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
2>.编辑nginx的子配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/pc.conf
server {
listen ;
server_name pc.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/html/pc;
index index.html;
} }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/mobile.conf
server {
listen ;
server_name mobile.yinzhengjie.org.cn mail.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/html/mobile;
index index.html;
} }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
3>.创建测试数据
[root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/nginx/html/{pc,mobile}
mkdir: created directory ‘/yinzhengjie/data/web/nginx/html/pc’
mkdir: created directory ‘/yinzhengjie/data/web/nginx/html/mobile’
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(255,0,0)'>尹正杰到此一游</h1>" > /yinzhengjie/data/web/nginx/html/pc/index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(0,0,255)'>Jason's mobile email:y1053419035</h1>" > /yinzhengjie/data/web/nginx/html/mobile/index.html
[root@node101.yinzhengjie.org.cn ~]#
4>.启动nginx服务
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN *: *:*
LISTEN ::: :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN *: *:*
LISTEN *: *:*
LISTEN ::: :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
三.测试
1>.浏览器访问“http://pc.yinzhengjie.org.cn/”,如下图所示。

2>.浏览器访问"http://mobile.yinzhengjie.org.cn/",如下图所示。

3>.浏览器访问"http://mail.yinzhengjie.org.cn/",如下图所示。

Nginx 核心配置-单节点实现多域名访问的更多相关文章
- Nginx 核心配置-作为上传服务器配置
Nginx 核心配置-作为上传服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关键参数说明 client_max_body_size 1m: 设置允许客户端上传单 ...
- Nginx 核心配置-可优化配置参数
Nginx 核心配置-可优化配置参数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.nginx的官网查看指令帮助信息方法 1>.打开nginx的官网(https://ng ...
- Nginx 核心配置-作为下载服务器配置
Nginx 核心配置-作为下载服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.无限速版本的下载服务器 1>.查看主配置文件 [root@node101.yinz ...
- Nginx 核心配置-长连接配置
Nginx 核心配置-长连接配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.长连接配置参数说明 keepalive_timeout number; 设定保持连接超时时长,0 ...
- Nginx 核心配置-location的登录账户认证实战篇
Nginx 核心配置-location的登录账户认证实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用ab命令模拟网站攻击 1>.安装httpd-tools工具 ...
- Nginx 核心配置-location的匹配案例实战篇
Nginx 核心配置-location的匹配案例实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.location语法规则介绍 在没有使用正则表达式的时候,nginx会先在 ...
- Nginx 核心配置-检测文件是否存在
Nginx 核心配置-检测文件是否存在 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件 ...
- Nginx 核心配置-自定义日志路径及清空日志注意事项
Nginx 核心配置-自定义日志路径及清空日志注意事项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于日志清空注意事项 1>.nginx服务写访问日志是基于acces ...
- Nginx 核心配置-自定义错误页面
Nginx 核心配置-自定义错误页面 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 生产环境中错误页面一般都是UI或开发工程师提供的,他们已经在软件中定义好了,我们这里就简单写个h ...
随机推荐
- 【java】[null]的问题
这么写逻辑就有问题,改成下面的
- python 面试真题
0.Python是什么? Python是一种解释型语言.但是跟C和C的衍生语言不同,Python代码在运行之前不需要编译.其他解释型语言还包括PHP和Ruby. Python是动态类型语言,指的是在声 ...
- SQL Server 迁移数据库 (二)分离和附加
分离和附加其实比导入和导出,步骤要少一些,但是数据量大的话,跨服务器拷贝数据文件可能要慢一些 1. 分离数据库 这里最好选择断开链接,断开之前要确保你记得数据库的路径,一般默认都是C:\Program ...
- 初赛Part1
计算机硬件 CPU CPU(中央处理器) = 运算器(ALC) + 控制器 + 寄存器, 主要任务是执行数据运算和程序控制. CPU的主要性能指标包括主频, 字长 ,高速缓存容量, 指令集等. 主频 ...
- 前端Vue项目——课程详情页面实现
一.详情页面路由跳转 应用 Vue Router 编程式导航通过 this.$router.push() 来实现路由跳转. 1.绑定查看详情事件 修改 src/components/Course/Co ...
- 日常笔记4关于cin、cin.get()、cin.getline()、getline()使用区别
1.关于PAT中段错误 使用字符数组出现错误: char str[256]; 报错段错误,然后改用C++中的string 改成: string str; 同char数组一样,也可以使用下标来取单个字符 ...
- C# HTTP系列1 HttpWebRequest类
系列目录 [已更新最新开发文章,点击查看详细] .NET Framework 中 System.Net 命名空间下提供了 HttpWebRequest 和 HttpWebResponse 2个 ...
- Nodejs操作MySQL数据库
https://github.com/mysqljs/mysql 如何用nodejs操作MySql数据呢,其实写法还是简单的, 1.开始在你的node项目中 npm install mysql - ...
- torch_06_卷积神经网络
1.概述 卷积神经网络的参数,由一些可学习的滤波器集合构成的,每个滤波器在空间上都计较小,但是深度和输入数据的深度保持一致.在前向传播中,让每个滤波器都在输入数据的宽度和高度上滑动(卷积),然后计算整 ...
- Luogu P2727 【01串 Stringsobits】
看到题解里好像都是用$DP$解决的,本着禁止DP的原则,我来提供一发纯数学其实和DP本质相同的题解,前两天刚反演题,脑子炸了,本来说换换脑子,结果还是数学 首先受进制思想启发,我们不妨按位考虑,考虑这 ...