web服务器介绍
nginx 介绍
在线自动生成nginx配置文件
https://www.digitalocean.com/community/tools/nginx?global.app.lang=zhCN
可以自由选择所需的应用,生成nginx配置作为参考。
根据你的业务需求,自动生成复杂的nginx配置文件,提供你作为参考,非常好用
nginx企业用它干啥
1.提供静态页面展示,网页服务
2.提供多个网站、多个域名的网页服务
====================================================
3.提供反向代理服务(结合动态应用程序)
4.提供简单资源下载服务(密码认证) ftp服务
5.用户行为分析(日志功能)
响应状态码
1. 当你client 发出 get请求 获取一个图片信息 ,发出requests
2. server 响应结果,nginx找到这个图片,返回给用户, response
响应信息,是有对应的状态码的。。
学习nginx会遇见的内容
- 20x系列,表示请求,响应正常解析
- 30x系列,表示本次请求,被转发到另一个服务器上了。。
- 40x系列,表示client客户端访问的url有问题,该资源不存在
- 50x系列,表示网站的服务端出错了(php-fpm)没有运行
1.dns篇
用户访问域名www.yuchaoit.cn
↓
浏览器跳转
↓
浏览器缓存(disk cache)
↓
Hosts文件
↓
本地DNS服务器(递归查询 > 迭代查询)
↓
client 向 server发起查询(递归查询)
server 向 server发起查询(迭代查询)
2.TCP/IP协议篇(三次握手)
client > SYN报文,请求连接
server > SYN,ACK报文 响应client
client > ACK,建立连接
3.客户端发起http请求
- 请求方法是什么、get、post、delete
- 请求主机是什么、www.yuchaoit.cn
- 请求资源是什么 、 http://yuchaoit.cn:8090/upload/2022/05/Xnip2022-05-01_16-32-30-b82235c9b62c42af8ea25e0313ca42f7.jpg
- 请求端口是什么、默认http是80、https是443
- 请求参数是什么、请求头部信息(资源类型、是否压缩、cookie、浏览器客户端等)
- 请求信息最后的换行
4.服务端响应的内容
- server信息(web服务器软件类型)
- 响应文件类型
- 响应头部信息(是否压缩,语言编码,是否保持连接等)
5.客户端发起TCP四次挥手断开连接。
client > 发起断开请求 FIN=1
server > 响应断开 FIN、ACK
server > 发起断开请求 FIN=1
client > 确认断开连接 ACK
nginx的运行架构
nginx运行后,有多少个干活的工人,多进程,调用多个cpu去解析用户的请求
在linux中进行多进程开发,开辟多个进程,调用多个cpu,当然也会消耗更多的机器资源,内存,cpu资源,给服务器带来更大的压力
- 不是说进程越多,干活越快,合理的分配,才能达到最高效的处理效率
=====
有一个屋子要装修,请1个工人干活,还是请5个工人干活,请500个工人干活
哪一个效率是最高的呢? 最合适的?
===
关于nginx的优化设置,nginx默认应该启动多少个进程去工作呢?
默认就是根据cpu的核数去设置进程数即可。
=======
master进程
包工头进程,管理nginx的数据,创建worker工作进程。
1.启动时检查nginx.conf是否正确,语法错误;
2.根据配置文件的参数创建、且监控worker进程的数量和状态;
3.监听socket,接收client发起的请求,然后worker竞争抢夺链接,获胜的可以处理且响应请求。
4.接收运维超哥发送的管理nginx进程的信号,并且将信号通知到worker进程。
5.如果运维超哥发送了reload命令,则读取新配置文件,创建新的worker进程,结束旧的worker进程。
利用nginx -V 命令可以看到当前操作的这个命令二进制命令,详细的信息,包括支持了哪些模块
nginx的安装形式
- 源代码编译安装,优点
- 版本,可以获取官网最新的软件包,甚至最新测试版,都可以直接编译安装
- 还有稳定版本
- 自由定义,安装路径自由定义,
- 自由定义第三方插件
- 缺点,安装步骤繁琐,耗时太长,看你要装多少个模块,编译添加的模块多,安装的就更久
- rpm安装
- 得提前准备好nginx本身的rpm包,以及相关依赖的rpm包
- 用于离线安装nginx的环境
- yum安装,你会用哪些形式的仓库?
- 阿里云第三方仓库(centos-base.repo,epel.repo)
- 这个其实都不靠谱。
- 自建yum仓库(得提前准备好nginx本身的rpm包,以及相关依赖的rpm包)
- nginx官网仓库(获取官网最新稳定版的yum源仓库)
- yum一键安装,省心省事,版本也是有一定的保障的,rpm的安全性也是有保障的
官网yum仓库
源代码编译
离线的rpm安装(yum 自建仓库)
编译安装
留作作业
通过linux命令,去解决这些问题
作业1 : 部署两个版本的nginx
1.编译安装nginx最新版,安装到 /opt/ngx/
2. yum官网仓库也安装一个nginx
3. 你要学会如何管理这多个版本的nginx
4.(各自版本的nginx配置文件路径,PATH设置,数据目录)
5. 当前机器运行的是哪个安装形式的nginx
yum安装
1. 配置官网yum源,一键安装即可
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF
2.清空yum源,安装最新版nginx
[root@web-8 /etc/yum.repos.d]#yum clean all
[root@web-8 /etc/yum.repos.d]#yum install nginx -y
3.查看PATH变量
[root@web-8 /etc/yum.repos.d]#which nginx
/usr/sbin/nginx
[root@web-8 /etc/yum.repos.d]#ll /usr/sbin/nginx
-rwxr-xr-x 1 root root 1377720 Nov 16 2021 /usr/sbin/nginx
[root@web-8 /etc/yum.repos.d]#nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
nginx管理命令
nginx -t # 检测nginx.conf语法
nginx -s reload # 重新读取nginx.conf
nginx -s stop # 停止nginx kill -15 nginx
nginx # 默认是直接运行,前提是当前机器没运行nginx
#你通过yum安装的nginx请你用systemctl去管理
# 不能多次执行nginx二进制命令
[root@web-8 ~]#
[root@web-8 ~]#nginx
[root@web-8 ~]#nginx # 会报错
# nginx -s reload ,会发生什么
nginx -s reload是给master进程发信号,重新读取配置信息,导致worker重新生成,因此worker-pid发生了变化
但是master进程id不带变化的(包工头,一直没变,更换了手底下的干活的工人)
================================
配置文件变化,就好比 合同变化了(包工头还是他,但是工人更换了一批)
===========
只有你restart的时候,包工头,也会被更换
你只能先停止, 再重启
nginx -s stop # 不会报错的
==============================
如果出现如下错误,如何解决,其实是通过pid,管理nginx的进程
~]#nginx -s stop
nginx: [error] invalid PID number "" in "/var/run/nginx.pid"
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#cat /var/run/nginx.pid
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#ps -ef |grep nginx
root 3599 1 0 16:10 ? 00:00:00 nginx: master process nginx
nginx 3628 3599 0 16:12 ? 00:00:00 nginx: worker process
root 3677 3434 0 16:19 pts/0 00:00:00 grep --color=auto nginx
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#echo 3599 > /var/run/nginx.pid
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#nginx -s stop
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#!ps
ps -ef |grep nginx
root 3686 3434 0 16:19 pts/0 00:00:00 grep --color=auto nginx
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]## 看懂了扣 6 不懂7
[root@web-8 ~]#
# 明确,现在用systemctl去管理nginx了
[root@web-8 ~]#systemctl start nginx
查看状态,reload, restart nginx,查看进程id号
[root@web-8 ~]#systemctl status nginx
[root@web-8 ~]#systemctl reload nginx # worker变化,master不变
[root@web-8 ~]#systemctl restart nginx # 整个nginx进程变化
# 用什么命令启动的,就用什么方式去管理该进程
nginx配置文件详解
安装完了之后,后续nginx的所有功能,都是围绕着修改nginx配置文件生效了
看懂配置文件,运维来说,达到手写nginx配置文件,才是合格的。
通过官网yum仓库默认安装的nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
~
nginx虚拟主机,部署网站
单虚拟主机
只需要在http{}区域中,设置一个 server{}标签即可。
部署一个 huoying.linux0224.cc 看到 /www/huoying/index.html
配置文件如下
降低运行权限
[root@web-8 ~]#groupadd www -g 666
[root@web-8 ~]#
[root@web-8 ~]#useradd www -u 666 -g 666 -M -s /sbin/nologin
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#id www
uid=666(www) gid=666(www) groups=666(www)
修改nginx.conf
nginx.conf入口配置文件
user www; # 设置运行用户
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf; # include导入该目录下的*.conf配置文件
}
创建虚拟主机子配置文件
只需要写server{}标签即可。
# huoying.linux0224.cc
# 吧数据放在 /www/huoying/index.html
vim /etc/nginx/conf.d/huoying.linux0224.conf
# 写入如下信息
server {
listen 80;
# nginx会匹配 http://huoying.linux0224.cc:80
server_name huoying.linux0224.cc;
# 支持中文的参数
charset utf-8;
location / {
# 根据root参数,填写网页根目录信息
# 表示当你访问 http://huoying.linux0224.cc:80 ,自动来这个目录下找数据
root /www/huoying/;
# 默认找 /www/huoying/ 的名字叫做index.html的文件
index index.html;
}
}
# 创建网页静态文件,index.html 鸣人.jpg 鸣人与佐助的秘密.txt
# 你部署一个静态网站,最基本的提供,html,jpg,txt等静态数据
# nginx都可以帮你去返回,解析请求
#
mkdir -p /www/huoying
cat > /www/huoying/index.html <<EOF
<meta charset=utf-8>
我是火影页面,老六你好。
EOF
cd /www/huoying ; wget -O 鸣人.jpg https://pics0.baidu.com/feed/d62a6059252dd42a57f830e3671230b2c8eab8b1.jpeg?token=df950341a2fc3467a01012e87e868f08
cd /www/huoying ; echo '佐助其实打不过鸣人' > 鸣人与佐助的秘密.txt
# 修改静态文件的属主,属组
[root@web-8 /www/huoying]#chown -R www.www /www/
测试nginx配置文件语法,然后启动
[root@web-8 /www/huoying]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-8 /www/huoying]#systemctl restart nginx
根据域名访问该虚拟主机
你的本地添加好dns域名解析
分别添加二级域名,三级域名,hosts解析
10.0.0.8 huoying.linux0224.cc linux0224.cc
你可以先访问ip试试,通不通
[C:\~]$ ping huoying.linux0224.cc
正在 Ping huoying.linux0224.cc [10.0.0.8] 具有 32 字节的数据:
来自 10.0.0.8 的回复: 字节=32 时间<1ms TTL=64
来自 10.0.0.8 的回复: 字节=32 时间<1ms TTL=64
来自 10.0.0.8 的回复: 字节=32 时间<1ms TTL=64
来自 10.0.0.8 的回复: 字节=32 时间<1ms TTL=64
10.0.0.8 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
该测试域名是没问题,浏览器直接访问即可,
有时候如果出现故障,检查你本地是否设置了代理。关闭即可。。、
html文件资源
图片资源
普通txt文件资源
如果是其他类型的文件,nginx默认不解析,直接下载
直接生成静态数据,不用重启nginx,这就是磁盘上的一些静态数据
nginx的server{}虚拟主机,以及设置了,去这个目录下搜索资料
nginx默认不识别这个test.ttt格式的文件,因此直接下载了
nginx识别的文件类型都在这个文件里定义好了
[root@web-8 /www/huoying]#cat /etc/nginx/mime.types
只有这个文件中定义的文件类型,nginx默认可以识别处理。。
nginx的配置文件匹配
先看当前有几个nginx配置文件
[root@web-8 /etc/nginx/conf.d]#ls
default.conf huoying.linux0224.conf
先看第一个default.conf
[root@web-8 /etc/nginx/conf.d]#grep -Ev '#|^$' default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
再看第二个配置文件
[root@web-8 /etc/nginx/conf.d]#cat huoying.linux0224.conf
server {
listen 80;
# nginx会匹配 http://huoying.linux0224.cc:80
server_name huoying.linux0224.cc;
charset utf-8;
location / {
# 根据root参数,填写网页根目录信息
# 表示当你访问 http://huoying.linux0224.cc:80 ,自动来这个目录下找数据
root /www/huoying/;
# 默认找 /www/huoying/ 的名字叫做index.html的文件
index index.html;
}
}
规整,删除无用的配置文件
[root@web-8 /etc/nginx/conf.d]#ls
huoying.linux0224.conf
[root@web-8 /etc/nginx/conf.d]#
[root@web-8 /etc/nginx/conf.d]#systemctl restart nginx
IP多虚拟主机
给指定的网卡,绑定多个ip地址
# 这个命令是临时添加一个ip
ip addr add 10.0.0.88/24 dev eth0
不得有人在用
修改虚拟主机,绑定多个ip
vim /etc/nginx/conf.d/88.conf
# 指定绑定ip地址的配置文件
[root@web-8 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-8 /etc/nginx/conf.d]#
[root@web-8 /etc/nginx/conf.d]#
[root@web-8 /etc/nginx/conf.d]#
[root@web-8 /etc/nginx/conf.d]#ls
88.conf huoying.linux0224.conf
[root@web-8 /etc/nginx/conf.d]#cat 88.conf
server {
listen 10.0.0.88:80;
server_name _;
location / {
root /www/80/;
index index.html;
}
}
# 创建测试数据
[root@web-8 /etc/nginx/conf.d]#mkdir -p /www/80/
[root@web-8 /etc/nginx/conf.d]#
[root@web-8 /etc/nginx/conf.d]#echo 'I am 10.0.0.88 server. welcome my linux' > /www/80/index.html
[root@web-8 /etc/nginx/conf.d]#
[root@web-8 /etc/nginx/conf.d]#
[root@web-8 /etc/nginx/conf.d]#systemctl restart nginx
测试基于ip的虚拟主机
1. 当你访问 10.0.0.8 或者 huoying.linux2004.cc
看到的是/etc/nginx/conf.d/huoying.linux0224.conf这个虚拟主机的内容
2. 当你访问,基于指定ip访问,看到的/etc/nginx/conf.d/88.conf的内容
给这个指定的IP,绑定一个测试域名
修改本地hosts
10.0.0.88 88.linux0224.cc
访问该域名也是可以通的,因为依然是基于绑定的socket地址匹配的该虚拟主机文件
http://88.linux0224.cc/
nginx提供了那么多丰富的绑定机制,让你的各种请求,匹配各种虚拟主机
多域名虚拟主机
dnf.linux0224.cc /www/lol/index.html
lol.linux0224.cc /www/lol/index.html
俩域名,就得分俩配置文件更合适些,表示是2个站点
创建各自的配置文件
dnf.linux0224.conf
[root@web-8 /etc/nginx/conf.d]#touch dnf.linux0224.conf
分别写入配置 ,基于域名的虚拟主机,这样写
server {
listen 80;
server_name dnf.linux0224.cc; # 这里写的是域名
charset utf-8;
location / {
root /www/dnf/;
index index.html;
}
}
lol.linux0224.conf
server {
listen 80;
server_name lol.linux0224.cc;
charset utf-8;
location / {
root /www/lol/;
index index.html;
}
}
创建两个数据目录即可
听到这里,还跟得上的扣 6 ,晕了扣7
mkdir -p /www/{lol,dnf}
# 分别创建测试数据
echo '人在塔在,人在linux在' > /www/lol/index.html
echo '勇士,好好学习linux,你就是最帅的' > /www/dnf/index.html
[root@web-8 /etc/nginx/conf.d]#systemctl restart nginx
重启nginx,查看各自的网站
还差什么步骤吗?
还差客户端机器上的 域名解析
10.0.0.8 huoying.linux0224.cc linux0224.cc lol.linux0224.cc dnf.linux0224.cc
http://lol.linux0224.cc/
http://dnf.linux0224.cc/
没了兄弟们,辛苦了。。。明天继续嗨。。
休息吧,作业一会就来。。。
88
多端口虚拟主机
除了支持
- 绑定域名
- 绑定ip
- 绑定多个端口,的虚拟主机
配置文件如下
在一个配置文件中,定义多个虚拟主机
vim /etc/nginx/conf.d/port.conf
# 平级
server {
listen 10.0.0.8:81;
server_name _;
charset utf-8;
location / {
root /www/data81/;
index index.html;
}
}
# 平级
server {
listen 10.0.0.8:82;
server_name _;
charset utf-8;
location / {
root /www/data82/;
index index.html;
}
}
# 创建测试数据, 看懂扣6 不懂7
mkdir -p /www/{data81,data82}
cd /www/data81 ; echo "我是81,你是老六" > /www/data81/index.html
cd /www/data82 ; echo "我是82,你是秘制小汉堡" > /www/data82/index.html
验证整个配置文件,和数据目录
[root@web-8 /www/data82]#tree -N /www/
/www/
├── 80
│ └── index.html
├── data81
│ └── index.html
├── data82
│ └── index.html
└── huoying
├── index.html
├── test.ttt
├── 鸣人.jpg
└── 鸣人与佐助的秘密.txt
配置文件
[root@web-8 /www/data82]#tree -NF /etc/nginx/conf.d/
/etc/nginx/conf.d/
├── 88.conf
├── huoying.linux0224.conf
└── port.conf
重启服务,查看是否生效
[root@web-8 /www/data82]#systemctl restart nginx
[root@web-8 /www/data82]#netstat -tunlp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4563/nginx: master
tcp 0 0 10.0.0.8:81 0.0.0.0:* LISTEN 4563/nginx: master
tcp 0 0 10.0.0.8:82 0.0.0.0:* LISTEN 4563/nginx: master
测试访问81和82页面
反向代理,负载均衡,就有用了,可以基于虚拟主机,实现多台服务器运行的多个网站
指定端口号才行
http://10.0.0.8:82/
http://10.0.0.8:81/