第一章Nginx介绍展开目录
Nginx是什么
Nginx是一个开源且高性能,可靠的Http Web服务,代理服务。开源:直接获取源代码高级:支持海量并发可靠:服务稳定
我们为什么选择Nginx服务
Nginx非常轻量功能模块少(来源唯一保留http与核心模块代码,其余不够核心代码会作为插件来安装)代码转换(易读,交替二次开发,对于开发人员非常友好)互联网公司都选择Nginx1.Nginx技术成熟,具有的功能是企业最常使用而且最需要的2。适合当前主流架构趋势,微服务,云架构,中间层3。统一技术栈,降低维护成本,降低技术更新成本。
Nginx重要特性
Nginx采用Epool网络模型,Apache采用选择模型选择:当用户发起一次请求,选择模型就会进行一次遍历扫描,从而导致性能低下。Epool:当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制
Nginx应用场景
第二章Nginx安装部署展开目录
Nginx分为几种。原始码编译(1.版本随意2.安装复杂3.升级繁琐)2.epel仓库(1.版本版本2.安装简单3.配置不易读)3.官方仓库(1.版本较新2.安装简单3.配置易读,推荐)下面分别介绍编译安装和yum安装方法
1.编译安装方法
创建www用户
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -s /sbin/nologin -M -u 666 -g 666
[root@web01 ~]# id www
uid=666(www) gid=666(www) 组=666(www)
安装依赖包
[root@web01 ~]# yum install openssl-devel pcre-devel -y
下载解压常数
[root@web01 ~]# mkdir /data/soft -p
[root@web01 ~]# cd /data/soft/
[root@web01 /data/soft]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
[root@web01 /data/soft]# tar zxvf nginx-1.16.0.tar.gz
配置编译参数
[root@web01 ~]# cd /data/soft/nginx-1.16.0/
[root@web01 /data/soft/nginx-1.16.0]# ./configure --help
[root@web01 /data/soft/nginx-1.16.0]# ./configure --user=www --group=www --prefix=/opt/nginx-1.16.0/ --with-http_stub_status_module --with-http_ssl_module --with-pcre
编译安装
[root@web01 /data/soft/nginx-1.16.0]# make && make install
创建软链接
[root@web01 /data/soft/nginx-1.16.0]# ln -s /opt/nginx-1.16.0/ /opt/nginx
[root@web01 /data/soft/nginx-1.16.0]# ls -lh /opt/
总用量 4.0K
lrwxrwxrwx 1 root root 18 7月 29 20:27 nginx -> /opt/nginx-1.16.0/
drwxr-xr-x 11 1001 1001 4.0K 7月 29 20:26 nginx-1.16.0
检查语法
[root@web01 /opt/nginx]# /opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.16.0//conf/nginx.conf test is successful
启动nginx
[root@web01 /opt/nginx]# /opt/nginx/sbin/nginx
检查测试
[root@web01 /opt/nginx]# netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12828/nginx: master
[root@web01 /opt/nginx]# curl 10.0.1.7
比喻
下载买菜
./configure切菜。洗菜,做什么菜
做炒菜,抄完之后,菜还在锅里
make install把菜端出来
/ opt / nginx / sbin / nginx -t尝一尝味道
/ opt / nginx / sbin / nginx吃菜
/ opt / nginx / sbin / nginx -s reload重新加载/加菜
/ opt / nginx / sbin / nginx -s stop收拾餐盘
2.YUM安装方法
安装依赖包
[root@web01 ~]# yum install openssl-devel pcre-devel -y
配置官方yum源
[root@web01 ~]# vim /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
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
安装nginx服务
[root@web01 ~]# yum install nginx -y
启动服务并配置开机自启动
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
测试访问
[root@web01 ~]# curl 10.0.1.7
Nginx启动方式说明
编译安装启动管理方式
nginx -t
nginx
nginx -s reload
nginx -s stop
yum安装启动管理方法
nginx -t
systemctl start nginx
systemctl reload nginx
systemctl restart nginx
systemctl stop nginx
第三章Nginx重要配置文件说明展开目录
查看配置文件
[root@web01 ~]# rpm -ql nginx
...................................................
/etc/logrotate.d/nginx #nginx日志切割的配置文件
/etc/nginx/nginx.conf #nginx主配置文件
/etc/nginx/conf.d #子配置文件
/etc/nginx/conf.d/default.conf #默认展示的页面一样
/etc/nginx/mime.types #媒体类型 (http协议中的文件类型)
/etc/sysconfig/nginx #systemctl 管理 nginx的使用的文件
/usr/lib/systemd/system/nginx.service #systemctl 管理nginx(开 关 重启 reload)配置文件
/usr/sbin/nginx #nginx命令
/usr/share/nginx/html #站点目录 网站的根目录
/var/log/nginx #nginx日志 access.log 访问日志
...................................................
查看已经编译的模块
[root@web01 ~]# nginx -V
配置文件注解
Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件由区块链的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。Nginx的主配置文件整体分为三块进行学习,分别是CoreModule(核心模块)EventModule(事件驱动模块)HttpCoreModule(HTTP内核模块)
第一部分:配置文件主区域配置
user nginx; #定义运行nginx进程的用户
worker_processes 1; #Nginx运行的work进程数量(建议与CPU数量一致或 auto)
error_log /var/log/nginx/error.log warn; #nginx错误日志
pid /var/run/nginx.pid; #nginx运行pid
第二部分:配置文件事件区域
events {
worker_connections 1024; #每个 worker 进程支持的最大连接数
}
第三部分:配置http区域
http {
include /etc/nginx/mime.types; #Nginx支持的媒体类型库文件
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; #包含子配置文件
}
第四部分:子配置文件内容
[root@web01 ~]# egrep -v "#|^$" /etc/nginx/conf.d/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;
}
}
http server location 扩展了解项
http{}层下允许有多个 Server{}层,一个 Server{}层下又允许有多个 Location
http{} 标签主要用来解决用户的请求与响应。
server{} 标签主要用来响应具体的某一个网站。
location{} 标签主要用于匹配网站具体 URL 路径
第四章Nginx虚拟主机配置实战展开目录
基于域名的虚拟主机
[root@web01 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
server {
listen 80;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
}
基于端口的虚拟主机
入口号优先级比域名要高
[root@web01 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
server {
listen 81;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
server {
listen 82;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
}
基于IP的虚拟主机
添加第二IP
ip addr add 10.0.0.11/24 dev eth0
配置文件
[root@web01 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
server {
listen 10.0.1.7:81;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
server {
listen 10.0.1.11:82;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
}
第五章Nginx虚拟主机配置优化展开目录
所有配置都写入一个配置文件维护起来比较麻烦,如果修改错了,影响所有的页面,所以我们应该拆分nginx的配置文件为各个子配置
Nginx主配置文件
[root@web01 /etc/nginx/conf.d]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
}
子配置文件www
[root@web01 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/01-www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
子配置文件博客
[root@web01 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/02-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
创建代码目录及首页
[root@web01 /etc/nginx/conf.d]# mkdir /usr/share/nginx/html/{www,blog}
[root@web01 /etc/nginx/conf.d]# echo "www" > /usr/share/nginx/html/www/index.html
[root@web01 /etc/nginx/conf.d]# echo "blog" > /usr/share/nginx/html/blog/index.html
检查语法重启服务
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx
访问测试
[root@web01 ~]# tail -1 /etc/hosts
10.0.1.7 www.oldboy.com blog.oldboy.com
[root@web01 ~]# curl www.oldboy.com
www
[root@web01 ~]# curl blog.oldboy.com
blog
第六章Nginx状态模块展开目录
nginx状态模块:--with-http_stub_status_module
状态模块配置文件
[root@web01 /etc/nginx/conf.d]# cat status.conf
server {
listen 80;
server_name status.oldboy.com;
stub_status on;
access_log off;
}
[root@web01 /etc/nginx/conf.d]# tail -1 /etc/hosts
10.0.1.7 www.oldboy.com blog.oldboy.com status.oldboy.com
[root@web01 /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@web01 /etc/nginx/conf.d]# systemctl restart nginx
[root@web01 /etc/nginx/conf.d]# curl status.oldboy.com
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
第七章Nginx日志展开目录
日志解释
$remote_addr # 记录客户端 IP 地址
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录 ISO8601 标准格式下的本地时间
$request # 记录请求的方法以及请求的 http 协议
$status # 记录请求状态码(用于定位错误信息)
$body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent # 发送给客户端的总字节数
$msec # 日志写入时间。单位为秒,精度是毫秒。
$http_referer # 记录从哪个页面链接访问过来的
$http_user_agent # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端 IP 地址
$request_length # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time # 请求花费的时间,单位为秒,精度毫秒
# 注:如果 Nginx 位于负载均衡器, nginx 反向代理之后, web 服务器无法直接获取到客 户端真实的 IP 地址。
# $remote_addr 获取的是反向代理的 IP 地址。 反向代理服务器在转发请求的 http 头信息中,
# 增加 X-Forwarded-For 信息,用来记录客户端 IP 地址和客户端请求的服务器地址。
第八章nginx连环坑展开目录
1.如果是yum安装,启动关闭命令推荐使用systemctl。不要混着nginx -s这样用。
2.相同域名相同端口会报冲突,某些都是0.0.0.0:80
3.没有家庭会报403而不是404
4.大部分优先级高于域名
5.ip +扩展的优先级是最高的
6.所有域名都匹配不上的时候,交替转发到根据ASCII码排序优先的配置文件
7.可以添加参数指定替代匹配的页面,这样就无需修改文件名了
server {
listen 80 default_server;
server_name www.mysun.com;
location / {
root /code/www;
index index.html index.htm;
}
}
第九章日志切割展开目录
[root@web01 /var/log/nginx]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
dateext
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}