1.介绍Nginx
Nginx 是一个高性能的HTTP和反向代理服务器
2.常见的Web服务器
- httpd
- nginx
3.常见的Nginx应用场景
代理
负载均衡
代理缓存 (proxy_cache)
静态资源
动静分离
Https
冰山模型中的一角 ---> 还有很多个使用场景
4.Nginx 安装 配置 启动
- 安装
第一种: 源码安装
第二种: yum --> 官方仓库 新 配置容易入手
第三中: yum --> epel仓库 旧 配置比较复杂
1.安装官方仓库源
[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
2.使用yum直接安装
[root@web01 ~]# yum install nginx -y
3.启动nginx
[root@web01 ~]# systemctl start nginx
5.Nginx 配置
[root@web01 ~]# cat /etc/nginx/nginx.conf
user nginx; # nginx进程的用户身份
worker_processes 1; # nginx的工作进程数量
error_log /var/log/nginx/error.log warn; # 错误日志的路径 [警告级别才会记录]
pid /var/run/nginx.pid; # 进程运行后,会产生一个pid
events { # 事件模型
worker_connections 1024; # 每个work能够支持的连接数
use epoll; # 使用epoll网络模型
}
http { # 接收用户的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; #启用压缩功能
#使用Server配置网站, 每个Server{}代表一个网站
server {
listen 80;
server_name test.oldxu.com;
location / { #控制网站访问的路径
root ...;
}
}
include /etc/nginx/conf.d/*.conf; 包含哪些文件
}
PS: Nginx中的http、server、location之间的关系是?
http 标签主要用来解决用户的请求与响应。
server 标签主要用来响应具体的某一个网站。
location 标签主要用于匹配网站具体url路径。
http{} 层下允许有多个Server{},可以有多个网站.
一个Server{} 下又允许有多个location{} 每个网站的uri路径不同,所以要分别进行匹配.
6.Nginx 搭建 游戏网站
1.注释掉之前的默认网站
[root@web01 html]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# gzip default.conf
2.编写游戏网站Nginx配置文件
[root@web01 conf.d]# cat game.oldboy.com.conf
server{
listen 80;
server_name game.oldboy.com;
location / {
root /code;
index index.html;
}
}
3.根据Nginx的配置文件,初始化
[root@web01 conf.d]# mkdir /code
4.上传代码
[root@web01 conf.d]# cd /code/
[root@web01 code]# rz html5.zip
[root@web01 code]# unzip html5.zip
5.检测语法
[root@web01 code]# nginx -t
6.重载服务
[root@web01 code]# systemctl restart nginx
7.配置域名解析
C:\Windows\System32\drivers\etc\hosts
7.Nginx访问的整体流程
http:// game.oldxu.com / game/yibihua/index.html
请求的uri: /game/yibihua/index.html
真实映射位置: /code/game/yibihua/index.html
8.Nginx配置虚拟主机有如下三种方式:
方式一、基于主机多IP方式 10.0.0.7 172.16.1.7
方式二、基于端口的配置方式 80 81 82 83
方式三、基于名称方式(多域名方式) test1 test2 test3 <---推荐