Nginx基础知识
第一节 Nginx介绍
1.为什么选择nginx
1.高性能、高并发、高可靠
2.热部署 nginx使用网络模型epoll
3.高扩展: 丰富的模块,能够随时随地的集成.
2.其他的web服务器
Apache 早期: 性能低\用得少\上手难
Nginx
Tengine 淘宝基于Nginx做的二次开发
openresty
lighttpd
IIS 微软:
GWS google开发
3.Nginx使用Epoll网络模型,而常听到Apache采用的是Select网络模型。
Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
epoll: 当用户发起请求,epoll模型会直接进行处理,效率高效。
4.nginx应用场景
代理
负载均衡
缓存
静态资源
静态资源Nginx
动态资源Tomcat分开处理
安全服务
https
限速
限流
访问控制
Nginx+Lua构建web应用防火墙 waf
5.Nginx安装
源码编译, 如果去一家公司,编译过nginx,现在让你重新组一个集群,按照他们的需求编译,怎么办?
yum安装
官方的仓库 版本新|适合学习
epel的仓库 版本旧|做了适当的修改
第二节nginx使用
1.配置nginx官方的yum仓库
[root@web01 ~]# 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.安装Nginx
[root@web01 ~]# yum install nginx -y
3.启动nginx
[root@web01 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4.启动nginx,并加入开机自启
[root@web01 nginx]# systemctl start nginx
[root@web01 nginx]# systemctl enable nginx
5.认识Nginx配置文件
[root@web01 nginx]# cat nginx.conf.
CoreModule核心模块
user nginx; #运行nginx进程的用户
worker_processes 1; #worker的进程数量
error_log /var/log/nginx/error.log warn; #错误日志
pid /var/run/nginx.pid; #进程运行后的pid,存放的位置
events事件驱动模块
events {
worker_connections 1024; #worker的最大连接数
use epoll; #使用网络模型epoll
}
http内核模块
http {
include /etc/nginx/mime.types; #包含nginx支持的资源类型
default_type application/octet-stream; #如果出现mime.types中不支持的类型,则以下载的方式发送给浏览器
#日志格式
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;
#使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
server {
listen 80; #监听端口, 默认80
server_name bgx.com; #提供的域名
#控制网站访问路径
location / {
root /usr/share/nginx/html; #存放网站源代码的位置
index index.html index.htm; #默认返回网站的文件
}
}
include /etc/nginx/conf.d/*.conf;
包含conf.d目录下所有以.conf结尾的文件
}
PS: Nginx中的http、server、location之间的关系是?
http{} 层下允许有多个Server{} 层,一个Server{} 层下又允许有多个Location{} 层
http 标签主要用来解决用户的请求与响应。
server 标签主要用来响应具体的某一个网站。
location 标签主要用于匹配网站具体URL路径。
第三节: 快速搭建游戏网站
1.nginx配置文件
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim game.conf
server {
listen 80;
server_name game.oldboy.com;
location / {
root /code;
index index.html;
}
}
2.创建目录
[root@web01 conf.d]# mkdir /code
3.进入目录
[root@web01 conf.d]# cd /code/
[root@web01 code]# rz
[root@web01 code]# unzip html5.zip
4.检查语法,并重载nginx
[root@web01 code]# nginx -t
[root@web01 code]# systemctl reload nginx
5.配置本地hosts解析
C:\Windows\System32\drivers\etc
6.网页链接请求
http:// game.oldboy.com /game/jg/index.html
请求 服务器真实存放的路径
/ --> /code
/game/jg/index.html --> /code/game/jg/index.html
第四节: Nginx配置虚拟主机有如下三种方式:
方式一:基于主机多IP方式
1.配置文件
[root@web01 conf.d]# cat ip.conf
server {
listen 10.0.0.7:80;
location / {
root /ip1;
index index.html;
}
}
server {
listen 172.16.1.7:80;
location / {
root /ip2;
index index.html;
}
}
2.创建目录
[root@web01 conf.d]# mkdir /ip1
[root@web01 conf.d]# mkdir /ip2
3.放置代码
[root@web01 conf.d]# echo "10..." > /ip1/index.html
[root@web01 conf.d]# echo "172..." > /ip2/index.html
4.重载服务
[root@web01 conf.d]# systemctl restart nginx
5.测试
[root@nfs ~]# curl http://10.0.0.7
10...
[root@nfs ~]# curl http://172.16.1.7
172...
方式二:基于端口的配置方式 81 82
1.配置文件
[root@web01 conf.d]# cat port.conf
server {
listen 81;
location / {
root /prot1;
index index.html;
}
}
server {
listen 82;
location / {
root /prot2;
index index.html;
}
}
方式三:基于多host名称方式(多域名方式)
1.准备两份配置文件
[root@web01 conf.d]# cat a.oldboy.com.conf
server {
listen 80;
server_name a.oldboy.com;
location / {
root /a;
index index.html;
}
}
[root@web01 conf.d]# cat b.oldboy.com.conf
server {
listen 80;
server_name b.oldboy.com;
location / {
root /b;
index index.html;
}
}
第五节: nginx日志
Syntax: log_format name [escape=default|json] string ...;
Default: log_format combined "...";
Context: http server location
A--->/var/log/nginx/a.oldboy.com.log
B--->/var/log/nginx/b.oldboy.com.log
建议:
1.保留http的access_log
2.新增加一个网站,就添加一个日志记录的位置.
3.至于location有些不重要的内容不想记录,可以aceess_log off;关闭记录日志.