Nginx开篇

Nginx开篇
[TOC]
😍实现目标

Nginx介绍
Nginx是一个开源且高性能、可靠的Http Web服务、代理服务1.开源: 直接获取源代码2.高性能: 支持海量并发3.可靠: 服务稳定
Nginx优点Nginx非常轻量'学习简单,易读,'功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)代码模块化 (易读,便于二次开发,对于开发人员非常友好)
互联网公司都选择Nginx1.Nginx技术成熟,具备的功能是企业最常使用而且最需要的2.适合当前主流架构趋势, 微服务、云架构、中间层3.统一技术栈, 降低维护成本,降低技术更新成本。
Nginx采用Epool网络模型(异步非阻塞模型)Apache采用Select模型Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。'处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效'
用一个比喻来解释Apache采用的select模型和Nginx采用的epoll模型进行之间的区别:假设你在大学读书,住的宿舍楼有很多间房间,你的朋友要来找你。select版宿管大妈就会带着你的朋友挨个房间去找,直到找到你为止。而epoll版宿管大妈会先记下每位同学的房间号,你的朋友来时,只需告诉你的朋友你住在哪个房间即可,不用亲自带着你的朋友满大楼找人。如果来了10000个人,都要找自己住这栋楼的同学时,select版和epoll版宿管大妈,谁的效率更高,不言自明。同理,在高并发服务器中,轮询I/O是最耗时间的操作之一,select和epoll的性能谁的性能更高,同样十分明了。Nginx和Apache区别
Nginx1.轻量级,采用 C 进行编写,同样的 web 服务,会占用更少的内存及资源2.高并发,nginx 以 epoll and kqueue 作为开发模型,处理请求是异步非阻塞的,负载能力比 apache 高很多,而 apache 则是阻塞型的。在高并发下 nginx 能保持低资源,低消耗,高性能,而 apache 在 PHP 处理慢或者前端压力很大的情况下,很容易出现进程数飙升,从而拒绝服务的现象。
nginx 处理静态文件好,静态处理性能比 apache 高三倍以上
nginx 的设计高度模块化,编写模块相对简单
nginx 配置简洁,正则配置让很多事情变得简单,而且改完配置能使用 -t 测试配置有没有问题apache 配置复杂,重启的时候发现配置出错了,会很崩溃
nginx 作为负载均衡服务器,支持 7 层负载均衡
nginx 本身就是一个反向代理服务器,而且可以作为非常优秀的邮件代理服务器
启动特别容易, 并且几乎可以做到 7*24 不间断运行,即使运行数个月也不需要重新启动,还能够不间断服务的情况下进行软件版本的升级
社区活跃,各种高性能模块出品迅速'因为在不断地进行更新,所以有很多模块的bug并没有测出来,bug多'---------------------------------Apacheapache 的 rewrite 比 nginx 强大,在 rewrite 频繁的情况下,用 apache
apache 发展到现在,模块超多,基本想到的都可以找到
apache 更为成熟,少 bug ,nginx 的 bug 相对较多
apache 超稳定
apache 对 PHP 支持比较简单,nginx 需要配合其他后端才能运行PHP
apache 在处理动态请求有优势,nginx 在这方面是鸡肋,一般动态请求要 apache 去做,nginx 适合静态和反向代理。--------------------------------------面试题: 你们公司为什么用nginx不用apache呢?nginx适合高并发场景、我们的业务属于并发类型的,所以使用nginx'高并发:说不准的业务,不稳定,今天没人访问,明天一下子来了很多人'apache适合稳定的业务场景、我们的业务属于稳定类型的,则使用apache# 公司的内部业务,都可以使用apache,它稳定'稳定的业务场景:用户数量固定,浮动不是很大,业务量不会突增'Nginx部署
三种安装方式:1.直接安装(默认仓库安装、版本低、不易读)[root@web01 ~]#yum -y install nginx[root@web01 ~]#nginx -vnginx version: nginx/1.21.5# 查看版本号'配置比较乱'[root@web01 ~]#cat /etc/nginx/nginx.conf# 默认配置文件.......user nginx;worker_processes auto;error_log /var/log/nginx/error.log;......
2.通过官网仓库安装(配置官网的仓库)1)配置官方仓库https://nginx.org/# nginx官网'documentation(文档)---> Installing nginx(下载Nginx)--->packages(在Linux安装的包)---->RHEL and..(选红帽的)'
# 创建一个repo文件,也就是设置一个仓库[root@web01 ~]#vim /etc/yum.repos.d/nginx.repo[nginx-stable]name=nginx stable repobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1
'配置说明'[nginx-stable] # 仓库的名称name=nginx stable repo # 仓库的名称baseurl=http://nginx.org/packages/centos/7/$basearch/ # 下载地址# /centos/$releasever/$basearch/# 因为我们是kylin所以centos后面必须7不然解析不出来gpgcheck=0 # 通过MD5校验数据包的完整性enabled=1 # 1表示开启,0表示关闭gpgkey=https://nginx.org/keys/nginx_signing.key# 官网上的校验值、如果gpgchek为0,可以删除此行
2)建立元数据连接[root@web01 ~]#yum makecache.....nginx stable repo 9.4 kB/s | 113 kB 00:12Metadata cache created.# 如果不配置makecache,通常也能安装(在首次运行yum时,自动下载元数据)
3)安装nginx[root@web01 ~]#yum -y install nginxInstalling: nginx x86_64 1:1.26.1-2.el7.ngx nginx-stable# nginx是通过nginx-stable仓库下载的'当我们配置了官方仓库,yum会优先选择官方仓库'Installing dependencies: compat-openssl10 x86_64 1:1.0.2o-8.ky10 ks10-adv-os# 依赖是默认仓库下载的[root@web01 ~]#nginx -vnginx version: nginx/1.26.1# 检查版本
4)配置
3.编译安装# 我们后面再详细介绍这个Nginx配置详解
[root@web01 ~]#cat /etc/nginx/nginx.conf1)user nginx; # 启动nginx的用户 安装后系统会自动创建此用户[root@web01 ~]#id nginxuid=665(nginx) gid=665(nginx) groups=665(nginx)
2)worker_processes auto; # 进程数量# 父进程派生多个子进程,如果是auto,则子进程的数量和CPU核心数有关# 我当前的web01和核心数为1[root@web01 ~]#lscpu.......CPU(s): 1.......# 那我们再来看一下它的进程是怎么样子的?[root@web01 ~]#systemctl start nginx[root@web01 ~]#ps aux | grep nginxroot 5344 0.0 0.0 22504 844 ? Ss 21:26 0:00nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf'master process;主进程'nginx 5345 0.0 0.3 33776 3736 ? S 21:26 0:00nginx: worker process'worker process:工作的进程'# 这样看并不直观,我们自己再进行调整一下CPU的核心数[root@web01 ~]#lscpuCPU(s): 4# 我们再来过滤一下进程[root@web01 ~]#ps aux | grep nginxroot 1287 0.0 0.0 22504 844 ? Ss 21:31 0:00nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.confnginx 1288 0.0 0.3 33776 3780 ? S 21:31 0:00 nginx: worker processnginx 1289 0.0 0.3 33776 3784 ? S 21:31 0:00 nginx: worker processnginx 1290 0.0 0.3 33776 3788 ? S 21:31 0:00 nginx: worker processnginx 1291 0.0 0.3 33776 3788 ? S 21:31 0:00 nginx: worker process'一个master,四个worker process'
3)error_log /var/log/nginx/error.log notice;# 错误日志的位置# notice(默认)是错误日志的等级,也就是日志的详细度不一样,默认的就够用了# 等级越高,越详细,服务器的压力越大,因为它要输出好多东西出来
4)pid /var/run/nginx.pid;# PID的位置# 这个文件里面存放的到底是什么呢?[root@web01 ~]#cat /var/run/nginx.pidcat: /var/run/nginx.pid: No such file or directory# 这个时候,我并没有启动nginx,压根没有这个文件[root@web01 ~]#systemctl start nginx[root@web01 ~]#cat /var/run/nginx.pid1323# 服务的进程ID,只有启动了这个服务,才会有相应的进程ID'在启动nginx服务的时候,它会判断这个文件里面是否有东西,有,则说明nginx启动了'# systemctl stop nginx == kill 1323[root@web01 ~]#kill `cat /var/run/nginx.pid`[root@web01 ~]#cat /var/run/nginx.pidcat: /var/run/nginx.pid: No such file or directory
5)events { worker_connections 32768;}# 每个 worker 进程(工作进程)可以同时处理的最大连接数# 默认是1024个,通常使用 1024 的倍数;32768------------------------------------------Nginx 能处理的总并发连接数 = worker_processes × worker_connectionsworker_processes:通常设为 CPU 核心数(或 auto)worker_connections:每个 worker 能处理的连接数------------------------------------------~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~上面是核心区块,下面是http区块专门用来响应用户的请求~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~http { 1)include /etc/nginx/mime.types;# 我服务器支持的资源类型都在这个文件中存放这呢[root@web01 ~]#cat /etc/nginx/mime.typestypes { text/html html htm shtml; text/css css; text/xml xml;# 资源类型 mp3、txt、html.................
2)default_type application/octet-stream;# 如果我们访问的资源类型,服务器不支持,也就是无法确认文件类型时;服务器会让浏览器进行下载,而不是尝试显示或者执行------------------------------------------举个例子假设你的服务器上有一个 .exe 文件,比如 setup.exe:1.如果 Nginx 的/etc/nginx/mime.types文件里, 没有定义 .exe 对应的 MIME 类型,那么 Nginx 就不知道该用什么 Content-Type(相应的页面类型,响应头)2.此时就会使用 default_type 指定的值,也就是 application/octet-stream3.浏览器收到响应头:Content-Type: application/octet-stream4.浏览器就会弹出“下载文件”对话框,而不是尝试打开或执行它'所以说“不支持访问,就会让你下载下来”'------------------------------------------ 3)log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';# 日志格式#日志保存在/var/log/nginx/access.log$remote_addr # 客户端的IP地址(远端的IP地址)$remote_user # 客户端登录用户名# 我们后面配置完用户认证模块后,日志会显示客户端登录的用户[$time_local] # 本地时间$request # 请求我们服务器上具体的资源 www.oldboy.com www.oldboy.com/pc/lx.png# 有的用户直接访问主页,有的是直接访问链接$status # 状态码$body_bytes_sent # 资源的大小$http_referer # 来源的网站页面,从哪个页面来的# 直接访问,没有跳转,不会进行记录$http_user_agent # 记录客户端的浏览器以及操作系统的信息$http_x_forwarded_for # 在有负载情况下记录客户端真实的IP地址# 如果没有这一条,记录的都是前面负载的IP地址而不是真实的客户端IP地址
4)access_log /var/log/nginx/access.log main;# 日志保存在这个文件中,以main这种格式保存# 当让我们也可以自定义日志格式为test(log_format)(随机取名字)
5)sendfile on; # 文件高效传输 #tcp_nopush on;
6)keepalive_timeout 65; # 长连接的超时时间# 我只等你65秒,如果65秒没有新的请求过来,我网站给你4次挥手,主动断开连接# 如果改为0,就是短连接,请求一次断开一次
7)gzip on; # 是否压缩资源传给用户# 我们服务器在返回资源给用户的时候,是否要把这个资源给提前压缩一下# 比如说,原来1M的资源压缩为200K,再传给用户,用户再进行解压缩,我们用户浏览器就能够看到这个资源了------------------------------------------是否压缩要看情况!'压缩后,传输速度会变快,但是每一个都进行压缩,会占用我们服务器的cpu的资源,而且cpu飙升老猛了'如果你想要传输的快,你cpu压力就大,不压缩,cpu快,网络传输就会变慢我们通常会有一个折中的解决方案,既要压缩,但是不能压缩得太狠!给cpu留一些资源来Nginx后面有压缩的等级1-9;9压缩的最狠,我们一般用到5就够用了# 通常一个问题解决了,会有另一个新的问题出现,那我们需要去锻炼平衡这些问题能力------------------------------------------
8)include /etc/nginx/conf.d/*.conf;# 将conf.d目录下所有的,以.conf结尾的配置文件,引入到当前配置# 也就是说我们在conf.d目录下创建的配置文件,都属于我们/etc/nginx/nginx.conf主配置文件里面的配置}
假如我们在/etc/nginx/conf.d/test.conf配置文件
中写了个脚本,脚本中,有touch a.txt
因为它被包含在/etc/nginx/nginx.conf主配置文件中
所以这个a.txt的绝对路径是/etc/nginx/a.txt
server配置文件
-
上面是主配置文件,下面是子配置文件
主配置文件会根据要求做些优化,未来我们主要改的就是 子配置文件 -
子配置文件一般以server{ }开头,server部分就是网站部分
[root@web01 ~]#cd /etc/nginx/conf.d/[root@web01 conf.d]#lltotal 4-rw-r--r-- 1 root root 1072 May 30 2024 default.conf# 它里面默认有一个配置文件,同时是被包含在/etc/nginx/nginx.conf主配置文件中# 我们只要在/etc/nginx/conf.d/目录下写任意.conf结尾的配置文件都会被包含[root@web01 conf.d]#rm -f default.conf# 我们把默认的给干掉,自己写一个配置文件
1)server配置文件[root@web01 conf.d]#cat test.confserver { listen 80; #监听的端口号 server_name www.oldboy.com; #主机名或者域名'浏览器访问www.oldboy.com:80/' # 省略了端口80,和网站根目录/# server_name可以指定多个域名,用空格分隔 location / { # 匹配规则,用户访问什么我就给他什么# /:网站根目录,如果有人访问我这个/根目录,去指定代码的位置里面去找 root /code/; # root不是用户,是指定代码的位置 index index.html index.htm; # index默认给用户返回的页面是什么'www.oldboy.com:80/index.html' #还省略了默认页面 }}------------------------------------------1.结束标志是分号;2.在 Nginx 的配置文件中,缩进(包括空格或 Tab)没有任何语法要求# 它通过 { } 大括号来界定作用域(比如 server {}、location {}),而不是靠缩进'避免使用 Tab,不同编辑器对 Tab 宽度解释不同,容易造成视觉混乱'# 推荐统一风格,4个空格------------------------------------------
2)检测nginx配置语法[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful# 主配置文件没有问题,我们刚才写的server配置文件包含在主配置文件里面# ok,successful# 这里我们还没有进行创建/code目录,我们后面再进行配置
3)启动nginx[root@web01 conf.d]#systemctl enable nginx[root@web01 conf.d]#systemctl restart nginx
4)检查端口[root@web01 conf.d]#netstat -lntup.......tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2210/nginx: master'访问页面,目前还没有创建/code目录'# 三种常见的状态码1)代码目录不存在 404[root@web01 conf.d]#ll /codels: cannot access '/code': No such file or directory[root@web01 conf.d]#curl localhost<title>404 Not Found</title>
2)代码目录存在、首页文件不存在 或者 权限拒绝 403[root@web01 conf.d]#mkdir /code[root@web01 conf.d]#curl localhost<title>403 Forbidden</title>'必会问题:如果删除首页文件,进行访问(不指定文件)会发生什么'# 403,首页不存在
3)访问正常 200[root@web01 conf.d]#echo '<h1>hhhh<h1>' > /code/index.html[root@web01 conf.d]#cat /code/index.html<h1>hhhh<h1>[root@web01 conf.d]#curl localhost<h1>hhhh<h1># 这里实际访问的是localhsot/index.html[root@web01 conf.d]#curl localhost/index.html<h1>hhhh<h1>'这个根目录/被指定到了具体的目录/code; /====/code'# 也就是表面访问的是/index.html,实际访问的是/code/index.html[root@web01 conf.d]#echo aaaaa > /code/test.html[root@web01 conf.d]#ll /code/total 8-rw-r--r-- 1 root root 13 Feb 3 11:48 index.html-rw-r--r-- 1 root root 6 Feb 3 14:38 test.html[root@web01 conf.d]#curl localhost/test.htmlaaaaa
#一定要知道用户访问的资源具体在我们服务器上的位置!!!
主配置VS子配置
Nginx 在实际生产环境中典型的 配置组织方式
两个关键路径:
-
主配置文件:
/etc/nginx/nginx.conf -
子配置目录:
/etc/nginx/conf.d/它是一个
目录(通常通过include /etc/nginx/conf.d/*.conf;被主配置文件包含)
并以一个具体示例(如 /etc/nginx/conf.d/test.conf)来讲解 http → server → location 的层级关系 ,以及 “包含”是如何跨文件实现的
🍔典型目录结构与包含关系
/etc/nginx/├── nginx.conf ← 主配置文件(核心配置 + http区块)└── conf.d/ └── test.conf ← 虚拟主机配置(含 server + location)主配置文件 /etc/nginx/nginx.conf(节选):
'专注于全局的设置'user nginx;..........
events { worker_connections 32768;}
# HTTP 核心上下文(最外层的 HTTP 配置)http { ....... # 👇 关键:包含 conf.d 目录下所有 .conf 文件 include /etc/nginx/conf.d/*.conf;}✅ 此时,
http块在nginx.conf中定义,并主动包含了conf.d/下的所有.conf文件
子配置文件 /etc/nginx/conf.d/test.conf
# 这个文件的内容会被“插入”到 nginx.conf 的 http 块中server { listen 80; server_name kpyun.com;
root /var/www/html; index index.html;# root和index也可以写在location外面,但它依旧属于server块 # location 是 server 的子块 location / { ....... }}⚠️ 注意:虽然
test.conf是一个独立文件,但因为被include到http { ... }块内部,它的内容逻辑上就处于http上下文中✅ 所以,尽管
这些模块写在不同的物理文件中,但在 Nginx 解析时,它们仍然严格遵循http → server → location的嵌套关系
- 主配置(
nginx.conf)专注全局设置(日志、gzip、连接数等) - 每个站点或服务单独一个
.conf文件(如shop.conf,blog.conf)
实践案例
Nginx配置小霸王
# 第一步: 配置server[root@web01 conf.d]#vim xbw.kpyun.confserver { listen 80; server_name xbw.kpyun.com;
location / { root /code/xbw; index index.html index.htm; }}# 这里指定的/网站根目录是/code/xbw,后面要去创建这个目录[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 conf.d]#systemctl restart nginx
#第二步 创建游戏代码目录[root@web01 conf.d]#mkdir /code/xbw
#第三步 上传游戏代码到/code/xbw[root@web01 conf.d]#cd /code/xbw/[root@web01 xbw]#lltotal 0[root@web01 xbw]#rz -Erz waiting to receive.# 开始上传# 关于rz的命令具体请看《有道云笔记》软件安装&文件详细信息# 因为我虚拟机里面安装了rz相关的软件包,所以我可以直接从Windows中拖拽的Xshell中[root@web01 xbw]#lltotal 7708-rw-r--r-- 1 root root 7892256 Dec 3 09:02 xbw.zip[root@web01 xbw]#unzip xbw.zip# unzip命令用于解压以zip结尾的压缩包[root@web01 xbw]#lltotal 7756-rw-r--r-- 1 root root 28032 May 24 2021 bgm.mp3drwxr-xr-x 2 root root 23 May 24 2021 cssdrwxr-xr-x 2 root root 23 May 24 2021 images-rw-r--r-- 1 root root 8956 May 24 2021 index.html# 在这里面有默认的首页drwxr-xr-x 2 root root 213 May 24 2021 jsdrwxr-xr-x 2 root root 4096 May 24 2021 roms-rw-r--r-- 1 root root 811 May 24 2021 shuoming.html-rw-r--r-- 1 root root 7892256 Dec 6 21:47 xbw.zip
# 最后一步客户端访问:# 我们在windows中修改本地的hosts文件,做域名解析,解析到我们的虚拟机的IP地址C:\Windows\System32\drivers\etc # windows路径10.0.0.7 xbw.kpyun.comC:\Users\LENOVO>ping xbw.kpyun.com# 用我们Windows物理机ping一下这个域名,测试是否正确解析到我们虚拟机中正在 Ping xbw.kpyun.com [10.0.0.7] 具有 32 字节的数据:来自 10.0.0.7 的回复: 字节=32 时间<1ms TTL=64来自 10.0.0.7 的回复: 字节=32 时间<1ms TTL=64.......
# 上面是我们在Windows中访问域名# 那么我们Linux虚拟机能够访问这个域名吗?[root@web01 conf.d]#curl xbw.kpyun.comcurl: (6) Could not resolve host: xbw.kpyun.com[root@web01 conf.d]#ping xbw.kpyun.comping: xbw.kpyun.com: Name or service not known'拉取不到页面,我们也ping不通'# Linux和Windows他们访问域名的流程是一样的,都要做DNS解析,先找本地缓存,再找本地的hosts表,再找我们配置的DNS服务器(8.8.8.8)....# 我们配置了Windows中的hosts表,那么我们用Windows的浏览器自然是能够访问的到的# 但是我们没有配置Linux的hosts表,在Linux当中,自然是拉取不到的[root@web01 conf.d]#vim /etc/hosts127.0.0.1 localhost10.0.0.7 xbw.kpyun.com# 配置Linux本地的hosts解析[root@web01 conf.d]#ping xbw.kpyun.comPING xbw.kpyun.com (10.0.0.7) 56(84) bytes of data.64 bytes from xbw.kpyun.com (10.0.0.7): icmp_seq=1 ttl=6464 bytes from xbw.kpyun.com (10.0.0.7): icmp_seq=2 ttl=64.......# 这样就能够ping的通了[root@web01 conf.d]#curl xbw.kpyun.com<script type="text/javascript" charset="utf-8"> 'ui': $('#emulator').JSNESUI({ "经典": [ ['魂斗罗', 'roms/Contra1(U)30.nes'], ['围棋大战', 'roms/wqdzz.nes'], ['四川麻将-制服篇', 'roms/scmj.nes'],...........Nginx虚拟主机
基于多域名的虚拟主机
'基于多域名的虚拟主机'# 不同域名访问不同的站点,生产环境中最常用业务一: 小霸王游戏机[root@web01 conf.d]#cat xbw.kpyun.confserver { listen 80; server_name xbw.kpyun.com;
location / { root /code/xbw; index index.html index.htm; }}[root@web01 conf.d]#cp xbw.kpyun.conf zh.kpyun.conf# 把小霸王的配置文件复制一份给植物大战僵尸
业务二: 植物大战僵尸#第一步骤 创建server[root@web01 conf.d]#vim zh.kpyun.confserver { listen 80; server_name zh.kpyun.com;
location / { root /code/zh; index index.html index.htm; }}
#第二步 测试配置文件[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
#第三步 重启生效[root@web01 conf.d]#systemctl restart nginx
#第四步 创建代码目录、上传代码[root@web01 conf.d]#mkdir /code/zh[root@web01 conf.d]#cd /code/zh[root@web01 zh]#rz -Erz waiting to receive.# 上传[root@web01 zh]#lltotal 3904-rw-r--r-- 1 root root 3995312 Dec 3 11:46 zh.zip[root@web01 zh]#unzip zh.zip# 解压[root@web01 zh]#lltotal 3948drwxr-xr-x 7 root root 4096 Apr 22 2023 images-rw-r--r-- 1 root root 38314 Dec 7 2023 index.html# 可以把zip压缩包删除了# 修改Windows中的hosts文件,做域名解析10.0.0.7 xbw.kpyun.com zh.kpyun.com# 用空格分隔多个域名,一个IP地址可以对应多个域名C:\Users\LENOVO>ping xbw.kpyun.com正在 Ping xbw.kpyun.com [10.0.0.7] 具有 32 字节的数据:来自 10.0.0.7 的回复: 字节=32 时间<1ms TTL=64来自 10.0.0.7 的回复: 字节=32 时间<1ms TTL=64........C:\Users\LENOVO>ping zh.kpyun.com# 测试是否能够ping通这个域名正在 Ping xbw.kpyun.com [10.0.0.7] 具有 32 字节的数据:来自 10.0.0.7 的回复: 字节=32 时间<1ms TTL=64来自 10.0.0.7 的回复: 字节=32 时间<1ms TTL=64.........浏览器测试访问:http://xbw.kpyun.com/https://zh.kpyun.com/ #https是访问不到的http://zh.kpyun.com/ #http才行'不安全'
基于多端口的虚拟主机
'基于多端口虚拟主机\企业网站或者业务的后台经常使用'# 不同端口访问不同的站点,一种保护,设置特殊端口配置两个业务81--->小霸王82--->植物
1)修改server配置文件的名称[root@web01 conf.d]#lltotal 8-rw-r--r-- 1 root root 133 Feb 3 15:02 xbw.kpyun.conf-rw-r--r-- 1 root root 131 Feb 3 16:00 zh.kpyun.conf[root@web01 conf.d]#mv xbw.kpyun.conf 81.conf[root@web01 conf.d]#mv zh.kpyun.conf 82.conf[root@web01 conf.d]#lltotal 8-rw-r--r-- 1 root root 133 Feb 3 15:02 81.conf-rw-r--r-- 1 root root 131 Feb 3 16:00 82.conf
2)修改内部配置文件[root@web01 conf.d]#vim 81.confserver { listen 81; server_name _;# server_name _; 作为“默认 server 块”的占位符# 用于捕获那些没有匹配到其他 server_name 的请求 location / { root /code/xbw; index index.html index.htm; }}[root@web01 conf.d]#vim 82.confserver { listen 82; server_name _;
location / { root /code/zh; index index.html index.htm; }}
3)测试配置文件[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
4)重新启动服务[root@web01 conf.d]#systemctl restart nginx
5)查看端口号[root@web01 conf.d]#netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 1984/nginx: mastertcp 0 0 0.0.0.0:82 0.0.0.0:* LISTEN 1984/nginx: master
6)测试访问[root@web01 conf.d]#curl localhostcurl: (7) Failed to connect to localhost port 80: Connection refused# 直接拉取页面,访问不到# 因为默认访问的是80端口,我们80端口压根就没有业务浏览器测试访问:10.0.0.7:8110.0.0.7:82'一定得是http://'# 这里用IP地址,是因为我们把server_name _;设置为默认的server块了
基于多IP的虚拟主机
'基于多IP的虚拟主机(几乎不用)'# 不同ip访问不同的站点10.0.0.7--->小霸王10.0.0.100-->植物
#第一步 配置临时的IP地址[root@web01 ~]#ip address add 10.0.0.100/24 dev eth0# 临时生效,重启后失效[root@web01 ~]#ip addr show eth02: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:93:c8:6a brd ff:ff:ff:ff:ff:ff inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 10.0.0.100/24 scope global secondary eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe93:c86a/64 scope link valid_lft forever preferred_lft forever
#第二步 基于多IP配置业务[root@web01 ~]#cd /etc/nginx/conf.d/[root@web01 conf.d]#mv 81.conf 10.0.0.7.conf[root@web01 conf.d]#mv 82.conf 10.0.0.100.conf[root@web01 conf.d]#lltotal 8-rw-r--r-- 1 root root 120 Feb 3 16:24 10.0.0.100.conf-rw-r--r-- 1 root root 121 Feb 3 16:24 10.0.0.7.conf# 修改里面的配置文件[root@web01 conf.d]#vim 10.0.0.7.confserver { listen 10.0.0.7; # 默认访问的依旧是80端口 # 当让我们也可以指定端口来进行访问:8888 # 访问8888端口 server_name _;
location / { root /code/xbw; index index.html index.htm; }}[root@web01 conf.d]#vim 10.0.0.100.confserver { listen 10.0.0.100; server_name _;
location / { root /code/zh; index index.html index.htm; }}[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 conf.d]#systemctl restart nginx# 查看端口监听[root@web01 conf.d]#netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 10.0.0.7:80 0.0.0.0:* LISTEN 2116/nginx: mastertcp 0 0 10.0.0.100:80 0.0.0.0:* LISTEN 2116/nginx: master# 两个都是80端口# 浏览器测试访问10.0.0.710.0.0.100文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!



