一、nginx的基础
- nginx是一个web服务器。(一个master 多个worker)
- nginx的优点:
- 高性能(响应快,近万个链接请求时,他的响应要比其他的web快)
- 高扩展性(nginx功能模块化。官方提供了很多的优秀模块)
- 高可靠性(9999 99999)
- 热部署(不停止服务的情况下进行升级)
- 互联网公司都选择nginx (代理、缓存、负载均衡、静态资源处理、动静分离)
统一技术债
…
-
nginx的基本组件
nginx二进制文件 (nginx二进制可执行文件) niginx.conf配置文件 (配置文件主要用来控制软件的行为) nginx access日志 (access.log访问日志,分析) nginx erroor日志 (error.log错误日志,黑匣子,根本性的问题)
二、nginx的安装
进入nginx官网nginx下载
选择自己的需要的版本(这里安装的是CentOS版本)
1.配置/etc/yum.repos.d/nginx.repo
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[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
module_hotfixes=true
2.下载nginx
[root@web01 ~]# yum install nginx -y
3.检查版本并启动
[root@web01 ~]# nginx -v
nginx version: nginx/1.18.0
[root@web01 ~]# systemctl start nginx
完成后在浏览器输入net网段,即可看到"Welcome to nginx!"
三、nginx目录结构
[root@web01 ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf #主配置文件 ( 驾驶员 )
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx #编译出来的二进制文件 ( 汽车 )
/usr/sbin/nginx-debug
/var/cache/nginx
/var/log/nginx #访问日志和错误日志
四、nginx配置文件
[root@web01 ~]# cat /etc/nginx/nginx.conf
#全局配置( 作用于整个Nginx )
user www; # nginx进程是哪个用户来运行
worker_processes 2; # 启动多少个worker进程
error_log /var/log/nginx/error.log warn; # 错误日志存在哪里( 黑匣子 )
pid /var/run/nginx.pid; # 每个进程运行起来都会存在一个pid文件,里面存放的是该进程的 ID
# 事件驱动模型
events {
worker_connections 1024; #worker接收的连接数 1024 * worker_process = connections
}
# http模型
http {
include /etc/nginx/mime.types;
default_type application/octet-stream; #当nginx无法识别你这个文件时,则默认一访问就直接下载了。
#定义日志的格式
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; #长连接 ( 默认长连接保持 65s )
#gzip on; #压缩
include /etc/nginx/conf.d/*.conf; #包含/etc/nginx/conf.d/目录下的所有以 .conf 结尾的文件。
}
www.game.com.conf
server { #server表示要配置网站,每个Server{} 代表一个网站,简称虚拟主机
listen 80; #这个网站监听80端口
server_name oldxu.com; #这个网站的域名是什么
location / { #控制网站访问的路径
root /usr/share/nginx/html; #定义网站源代码存放的路径
index index.html index.htm; #默认返回哪个文件作为默认返回页
}
}
- nginx中的http、server、location之间的关系是?
http:主要用来解决用户的请求和响应。 www test tt
server:主要用来响应具体的某一个网站。
location:用于匹配网站的uri路径。
http{} 下面可以有多个 Server{} 每个server{} 又可以有多个 location {}
五、nginx运行一个游戏网站
思路:
1.准备一个游戏的源码 ( qq下载的 )
2.准备一个目录,存放我们的游戏
[root@web01 ~]# mkdir /code
[root@web01 ~]# cd /code
[root@web01 ~]# rz 游戏
[root@web01 ~]# unzip h5game.zip
3.配置Nginx
3.1 配置监听的端口
3.2 配置网站的域名
3.3 配置用户访问网站路径规则 location
3.4 配置默认返回的页面
[root@web01 ~]# cat /etc/nginx/conf.d/game.xu.com.conf
server {
listen 80;
server_name game.xu.com;
location / {
root /code;
index index.html;
}
}
4.检查nginx的语法 {} 成对出现 ; 每一条指令的结尾都有
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5.重启服务,让配置生效
[root@web01 code]# systemctl restart nginx
nginx访问网站的原理(详细过程)
game.oldxu.com 如何访问到网站的?
第一步:浏览器请求 game.xu.com
第二步:浏览会将请求的 game.xu.com --> http://game.xu.com/
第三步:浏览器对 域名进行解析 DNS解析 ( 我们域名是假的,所以,我们配置 hosts 劫持 )
第四步:浏览器通过随机端口,像服务端 80 端口建立TCP连接
第五步:浏览器发起HTTP的请求
第六步:请求被80端口对应的Nginx应用所接受,会交给http层,发现请求的域名是 game.oldxu.com
第七步:接下来检查所有配文件,看是否有配置文件 满足用户请求的域名。 server_name
第八步:满足域名匹配之后,检查用户 请求的路径, / 就会被location / 所匹配
第九步:返回结果,/code下面的index.html 给用户
第十步:nginx应用程序像内核发送请求,获取磁盘中的某个文件,磁盘将数据拷贝至内核的缓存区,然后在 拷贝到nginx应用进程的缓存区
第十一步:nginx应用进程封装数据报文,回传给客户端浏览器。