007.Nginx基本配置与虚拟主机
Nginx的基本使用
Nginx分为master/workers结构,一个master主进程,负责管理和维护多个worker进程,真正处理用户请求的是worker进程,master不对用户请求进行处理。master主进程负责分析并加载配置文件,管理worker进程,接收用户信号传递及平滑升级等功能。nginx具有强大的缓存功能,其中Cache Loader负责载入缓存对象,Cache Manager负责管理缓存对象
默认启动nginx是不会产生Cache Loader和Cache Manager进程的,此两个进程负责将后端的一些数据缓存到本地磁盘或内存中,用户访问进来时,如果在缓存有效期内则会直接将本地的缓存内容交给用户,不需要再从后端读取,提升nginx响应速度
Nginx的目录结构
[root@web01 ~]# rpm -ql nginx
/etc/logrotate.d/nginx # 日志轮转
/etc/nginx # 此目录保存配置文件,其中mime.types文件中保存静态文件的映射关系
..
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service # 服务启动文件
/usr/lib64/nginx
/usr/lib64/nginx/modules # nginx的模块
...
/usr/sbin/nginx # nginx的执行文件
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.22.1 # 此目录下保存nginx的文档文件
...
/usr/share/nginx/html # 此目录下保存nginx默认的网站页面
...
/var/cache/nginx # 缓存
/var/log/nginx # 日志
Nginx的启停
Nginx默认使用80端口,在启动nginx之前需要确认主机的80端口是否被占用,从而关闭占用80端口的服务或调整nginx监听的端口;对于nginx的管理,可以直接使用nginx命令,也可以通过systemd工具进行管理,但两者不能混合使用
# 1.关闭apache服务
[root@web01 ~]# systemctl stop httpd
[root@web01 ~]# systemctl disable httpd
# 2.systemd管理nginx服务
[root@web01 ~]# systemctl enable nginx
[root@web01 ~]# systemctl start nginx
# 3.nginx命令管理服务
[root@web01 ~]# /usr/sbin/nginx # 启动nginx
[root@web01 ~]# /usr/sbin/nginx -s stop # 关闭nginx
[root@web01 ~]# /usr/sbin/nginx -s reload # 重载nginx
Nginx配置文件概述
Nginx主配置文件/etc/nginx/nginx.conf以区块形式构成,每个区块以一对大括号{}来表示开始与结束。Nginx的主配置文件整体分三块,分别是CoreModule(核心模块)、EventModule(事件驱动模块)、HttpCoreModule(http内核模块)
CoreModule核心模块
user nginx; # nginx进程所属用户
worker_processes auto; # nginx运行的work进程数量(建议与CPU数量一致或auto)
error_log /var/log/nginx/error.log notice; # 错误日志,notice表示记录此级别和高于此级别的日志
pid /var/run/nginx.pid; # nginx服务运行后产生的pid进程号
events事件模块
events {
worker_connections 1024; # 一个work进程的最大连接数。通常一台服务能处理的连接数在2w~3w
use epoll; # 使用epoll网络模型。nginx默认使用epoll模型,此选项可以不写
}
http内核模块
http {
include /etc/nginx/mime.types; # 静态资源映射关系
default_type application/octet-stream; # 默认文件类型(下载)。在mime.types中没有映射关系的静态资源,会以默认类型返回浏览器
# log_format定义nginx的日志格式,它声明一个变量main,在main下面是nginx的内置变量。用户访问nginx产生的日志以log_format定义的格式记录
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; # 访问日志。nginx的日志以main变量的格式记录到access.log文件中
sendfile on; # 高效文件传输方式
#tcp_nopush on;
keepalive_timeout 65; # 长连接的超时时间
#gzip on; # 是否开启压缩功能
include /etc/nginx/conf.d/*.conf; # 其他的实际网站文件放在此路径下
}
从主配置文件的选项include /etc/nginx/conf.d/*.conf;可以确定,在/etc/nginx/conf.d/目录下的所有文件也隶属于http内核模块,安装nginx后生成一个默认文件/etc/nginx/conf.d/default.conf
server { # 定义一个网站
listen 80; # 监听端口
server_name localhost; # 域名
#access_log /var/log/nginx/host.access.log main; # 局部访问日志配置
location / { # 定义'/'的路径
root /usr/share/nginx/html; # 代码文件的存放位置
index index.html index.htm; # 默认响应的文件名称
}
}
多数情况下浏览器访问一个网站时只用输入一个域名或IP,例如www.baidu.com,浏览器会自动补齐协议、端口、路径等信息,例如https://www.baidu.com:80/,默认补齐的路径/就对应到nginx的配置文件location /,nginx会响应这个location下的默认配置到客户端,也就是响应/usr/share/nginx/html/index.html文件到客户端
http{}层下允许有多个server{}层,一个Server{}层下又允许有多个location
http{}标签主要用于解决用户的请求与响应
server{}标签主要用于响应具体的某一个网站
location{}标签主要用于匹配网站具体的URL路径
Nginx网站配置
用户使用域名访问服务器的情况下,由http{}接收用户的访问请求,并根据用户访问的域名,将请求转交给对应的server{},再根据location{}和用户的访问参数返回对应的资源到用户。这种请求与响应是建立在用户使用域名访问的前提下,如果客户使用IP访问服务器,服务器会随机返回一个页面给用户
# 1.新增nginx配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/hebor.conf
server {
listen 80;
server_name hebor.example.com;
location / {
root /opt/code;
index index.html index.htm;
}
}
# 2.上线代码
[root@web01 ~]# mkdir /opt/code
[root@web01 ~]# tar -xzf compress.tar.gz -C /opt/code/
[root@web01 ~]# systemctl restart nginx
# 3.修改客户端hosts本地解析,浏览器访问域名
[root@hebor ~]# more /etc/hosts
172.16.1.7 hebor.example.com
重载nginx服务有两个选项,分别是restart和reload,后者表示平滑重载,restart会将所有连接直接断开,reload会等待客户连接断开
Nginx虚拟主机
虚拟主机可以实现在一个Nginx上运行相对独立的多个服务。Nginx配置虚拟主机有3种方式
- 基于IP配置的虚拟主机
基于IP的配置方式分2种情况:多网卡多IP、单网卡多IP。单网卡多IP的配置与多网卡完全一致,只不过在修改nginx配置文件之前需要先为网卡添加多IP
# 多网卡多IP配置
vim /etc/nginx/conf.d/ip.conf
server{
listen 192.168.122.7:80; # 指定IP的端口
server_name _; # 表示域名为空
location / {
root /opt/code_ip_eth0;
index index.html index.htm;
}
}
server{
listen 172.16.1.7:80;
server_name _;
location / {
root /opt/code_ip_ens9;
index index.html index.htm;
}
}
[root@web01 ~]# nginx -t # 检查nginx配置文件语法是否正常
[root@web01 ~]# systemctl restart nginx
[root@web01 ~]# mkdir /opt/code_ip_eth0
[root@web01 ~]# echo "Eth0" >/opt/code_ip_eth0/index.html # 创建测试文件
[root@web01 ~]# curl 192.168.122.7 # 测试IP访问
[root@web01 ~]# mkdir /opt/code_ip_ens9
[root@web01 ~]# echo "ens9" > /opt/code_ip_ens9/index.html # 创建测试文件
[root@web01 ~]# curl 192.168.122.7 # 测试IP访问
# 单网卡多IP配置
[root@web01 ~]# ip addr add 10.0.0.7/24 dev eth0 # 临时配置多IP
[root@web01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0 # 新建网卡别名文件长期有效
TYPE="Ethernet"
BOOTPROTO="static"
DEVICE="eth0:0"
NAME="eth0:0"
ONBOOT="yes"
IPADDR="10.0.0.7"
PREFIX="24"
# 除了新建网卡别名文件,也可以直接在网卡配置文件里添加IPADDR1选项参数
- 基于端口配置
[root@web01 ~]# vim /etc/nginx/conf.d/port.conf
server {
listen 81;
location / {
root /opt/code_81;
index index.html index.htm;
}
}
server {
listen 82;
location / {
root /opt/code_82;
index index.html index.htm;
}
}
[root@web01 ~]# echo "81port" > /opt/code_81/index.html
[root@web01 ~]# echo "82port" > /opt/code_82/index.html
[root@web01 ~]# curl 172.16.1.7:81
[root@web01 ~]# curl 172.16.1.7:82
- 基于域名配置
[root@web01 ~]# more /etc/nginx/conf.d/vhost1.conf
server {
listen 80;
server_name hebor1.example.com;
location / {
root /opt/hebor1;
index index.html index.htm;
}
}
server {
listen 80;
server_name hebor2.example.com;
location / {
root /opt/hebor2;
index index.html index.htm;
}
}
[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl restart nginx
[root@hebor ~]# curl hebor1.example.com
[root@hebor ~]# curl hebor2.example.com
关于域名虚拟主机配置,比较常见的一个问题是配置nginx完成后,能正常访问网页,但访问不到域名对应的网页,大概率是由于域名的问题导致的。如果DNS解析配置了正确的IP,但与IP对应的域名有误时,访问nginx服务端,nginx收到了用户请求,但找不到对应的域名,此时nginx会将所有配置文件中的第一个配置文件的页面返回给用户
Nginx日志管理
nginx -t命令实际上是检查nginx的主配置文件/etc/nginx/nginx.conf,而nginx.conf文件的尾部有一个配置参数include /etc/nginx/conf.d/*.conf;,这个参数会将/etc/nginx/conf.d目录下的所有配置文件都包含到语法检查中;除了-t选项检查语法,还有一个延展选项-c指定检查具体的某个配置文件
nginx -t的作用是为了减少排查故障的范围,每一次配置修改之后都先检查nginx的配置文件语法是一个好习惯。在配置文件检查没有问题、nginx服务能够正常启动的前提下,客户端访问nginx时再出现问题,就需要借助nginx日志排查故障了
Nginx有非常灵活的日志记录模式,每个层级(http{}、server{}、location{})的配置可以有各自独立的访问日志。日志格式通过log_format命令定义格式,Nginx的日志分为error.log和access.log两种,此处主要针对access.log的日志格式进行解析
-
log_format定义日志格式语法
# 配置语法:包括error.log access.log Syntax:log_format name [escape=default|json] string ...; Default: log_format combined "..."; # log_format默认值 Context: http # log_format关键词只能配置到http层作用域默认日志以文本格式记录,也可以修改成json格式记录
-
默认Nginx定义的日志语法
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; -
Nginx日志格式允许的内置变量
$remote_addr 客户端IP地址 $remote_user 客户端用户名 $time_local 通用的本地时间 $time_iso8601 ISO8601标准格式下的本地时间 $request 请求方法和http协议版本 $status http状态码 $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头信息中,增加$http_x_forwarded_for信息,用于记录客户端IP和客户端请求的服务器地址 -
access_log日志配置语法Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off; Default: access_log logs/access.log combined; Context: http, server, location, if in location, limit_execpt # 作用域
局部配置的access_log日志优先级要高于全局配置,局部没有配置access_log时,以全局配置的access_log为准
Nginx Access日志配置示例
server {
listen 80;
server_name web01.example.com;
access_log /var/log/nginx/web01.example.com main; # 修改日志文件保存路径
location / {
root /usr/share/nginx/html/;
index index.html index.htm;
}
location /favicon.ico {
access_log off; # 用户访问/favicon.ico时不记录日志
return 200;
}
}
- 日志切割logrotate
[root@web01 ~]# more /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily # 每天切割日志
missingok # 日志丢失忽略
rotate 52 # 日志保留52天
compress # 日志文件压缩
delaycompress # 延迟压缩日志
notifempty # 不切割空文件
create 640 nginx adm # 创建新日志文件的权限
sharedscripts
postrotate # 切割日志执行的命令
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
kill -USR1 `cat /var/run/nginx.pid`这条命令的意思是重新加载nginx日志,nginx日志轮询切割后,原本的access.log文件被重命名成了一个新日志文件,而此时nginx仍还在往access.log文件写入日志,在找不到access.log文件的情况下,nginx的日志会丢失,所以此时需要重启一下nginx的日志模块,重新生成一个access.log文件

浙公网安备 33010602011771号