文章目录
展开Nginx概述
Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。
开源: 直接获取源代码
高性能: 支持海量并发 (同一时间 用户的访问量就是并发量 海量就是大量的访问量)
可靠: 服务稳定
nginx非常轻量
Nginx采用Epool网络模型,Apache采用Select模型
Eoool:当用户发起请求, epool模型会直接进行处理,效率高效,并无连接限制。
select:当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
nginx快速安装
- rpm安装(yum安装)
- epel仓库(阿里云)
- 官方仓库
- 源码安装
使用官方源
添加nginx官方源↓
## 添加nginx官方源
[root@web01 ~]# vim /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
## 安装nginx
[root@web01 ~]# yum install -y nginx
## 启动并加入开机自启
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
## 查看nginx版本
[root@web01 ~]# nginx -version
nginx version: nginx/1.24.0
或↓
[root@web01 ~]# nginx -v
nginx version: nginx/1.24.0
## 查看nginx的版本和源码安装生成步骤时指定的参数有哪些
[root@web01 ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
nginx的启停
# 1.启动
- systemctl start nginx
- nginx
- /app/nginx/sbin/nginx
# 2.停止
- systemctl stop nginx
- nginx -s stop
- /app/nginx/sbin/nginx -s stop
# 3.重新加载
- systemctl reload nginx
- nginx -s reload
- /app/nginx/sbin/nginx -s reload
## nginx选项
-c:指定配置文件的路径(当nginx配置文件在别的目录时 需要指定)
-t:检查配置文件的语法(无法检测单词拼写)
-s:启停重载,服务操作
-v:查看版本号
-V:查看版本和编译参数
## nginx启动脚本systemd管理
[root@web01 ~]# vim /usr/lib/systemd/system/nginx.service
## 建议改完配置文件使用 reload,一定要先检查nginx语法:nginx -t -c /其它路径配置文件
[Unit] # 第一段里 除了标签 其他都可有可无
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
# nginx启动以后的pid进程号存放文件位置
PIDFile=/var/run/nginx.pid
# nginx的启动程序 指定配置文件的位置
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
# nginx的重新加载命令 相当于kill -1
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
# nginx的停止命令 相当于kill -15
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
# 指定开机自启的运行级别
WantedBy=multi-user.target
## 源码安装,使用fpm打包并加入systemd管理,脚本内容:
# 先写脚本
[root@web01 ~]# vim post_install_nginx.sh
ln -s /application/nginx-1.20.2 /opt/nginx
echo 'PATH="/usr/local/nginx/sbin:$PATH"' > /etc/profile.d/nginx.sh
cat >> /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/application/nginx/nginx.pid
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /application/nginx/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /application/nginx/nginx.pid)"
[Install]
WantedBy=multi-user.target
EOF
nginx配置文件
1.Nginx主配置文件
路径 | 类型 | 作用 |
---|---|---|
/etc/nginx/nginx.conf | 配置文件 | nginx主配置文件 |
/etc/nginx/conf.d/default.conf | 配置文件 | nginx网站示例配置文件(server层) |
2.Nginx代理相关参数文件
路径 | 类型 | 作用 |
---|---|---|
/etc/nginx/fastcgi_params | 配置文件 | Fastcgi代理配置文件 |
/etc/nginx/scgi_params | 配置文件 | scgi代理配置文件 |
/etc/nginx/uwsgi_params | 配置文件 | uwsgi代理配置文件 |
3.Nginx编码相关配置文件
路径 | 类型 | 作用 |
---|---|---|
/etc/nginx/win-utf | 配置文件 | Nginx编码转换映射文件 |
/etc/nginx/koi-utf | 配置文件 | Nginx编码转换映射文件 |
/etc/nginx/koi-win | 配置文件 | Nginx编码转换映射文件 |
/etc/nginx/mime.types | 配置文件 | Content-Type与扩展名 |
4.Nginx管理相关命
路径 | 类型 | 作用 |
---|---|---|
/usr/sbin/nginx | 命令 | Nginx命令行管理终端工具 |
/usr/sbin/nginx-debug | Nginx命令行与终端调试工具 |
5.Nginx日志相关目录与文件
路径 | 类型 | 作用 |
---|---|---|
/var/log/nginx | 目录 | Nginx默认存放日志目录 |
/etc/logrotate.d/nginx | 配置文件 | Nginx默认的日志切割 |
查看nginx配置文件,日志文件位置的方法
## 如果nginx配置文件位置被改动 不在默认的位置 查找的方法 在进程管理里找 -c 指定的配置文件
[root@web01 ~]# ps -ef|grep nginx
root 1877 1 0 14:33 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
## 如果nginx日志文件位置被改动 不在默认的位置 查找的方法 在配置文件中找指定日志文件的行
[root@web01 ~]# cat /etc/nginx/nginx.conf
找到 error_log /var/log/nginx/error.log notice; 这一行 看看指定的位置
nginx配置文件详解
## nginx配置文件 当用阿里云下载时还会有 .default 结尾的文件 是阿里云帮你准备的备份文件
[root@web01 nginx]# ll
total 28
drwxr-xr-x 2 root root 42 Aug 30 19:35 conf.d
drwxr-xr-x 2 root root 6 Nov 11 2022 default.d
-rw-r--r-- 1 root root 1007 Apr 12 01:22 fastcgi_params
-rw-r--r-- 1 root root 5349 Apr 12 01:22 mime.types
lrwxrwxrwx 1 root root 29 Aug 30 19:35 modules -> ../../usr/lib64/nginx/modules
-rw-r--r-- 1 root root 1135 Aug 27 20:11 nginx.conf
-rw-r--r-- 1 root root 648 Apr 12 01:21 nginx.conf.rpmnew
-rw-r--r-- 1 root root 636 Apr 12 01:22 scgi_params
-rw-r--r-- 1 root root 664 Apr 12 01:22 uwsgi_params
## nginx主配置文件 只查看生效内容的指令
[root@web01 nginx]# grep -Ev '^$|#'
/etc/nginx/nginx.conf
# 注意:nginx配置文件,一对 {} 内的内容是一层 以 ; 结尾 算一行 每一行结尾都要有 ;
## nginx主配置文件详情
[root@web01 nginx]# vim /etc/nginx/nginx.conf
## 核心层(核心模块)、全局配置
# nginx启动用户配置
user nginx;
# nginx工作线程数量(cpu亲和)
worker_processes auto;(auto 自动根据cpu的核心数来启动对应的工作进程数)
# 错误日志 日志路径 日志级别
error_log /var/log/nginx/error.log notice;
# 程序启动进程号(pid号)存放的路径
pid /var/run/nginx.pid;
## 事件层(事件模块)
events {
# 一个worker进程的最大连接数 CPU每一个核同一时间支持的最大请求数
worker_connections 1024;
}
## http层,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;
## 长链接,超时时间 65s
keepalive_timeout 65;
## 数据传输过程中,使用gzip压缩
#gzip on;
## 包含 nginx其他子配置文件(网站虚拟主机配置文件server)
## 指定在 etc/nginx/conf.d/ 创建 .conf结尾的文件 文件中写入你的server层配置
include /etc/nginx/conf.d/*.conf;
}
## 日志格式
$remote_addr:远端的IP(上一个节点的IP)
$remote_user:登录的用户
[$time_local]:时间
"$request":请求方式、请求uri、HTTP协议版本号
$status:状态码
$body_bytes_sent:流量
"$http_referer":跳转地址(从哪个网站跳转过来的)
"$http_user_agent":访问你的客户端的信息
"$http_x_forwarded_for":记录透传IP地址(获取用户的真实IP 没有这个 只能获取上一个节点的IP)
## 虚拟主机配置文件
server {
## 该网站的监听端口
listen 80;
## 该网站的主机IP或域名
server_name localhost;
## 该网站的日志路径及日志格式
#access_log /var/log/nginx/host.access.log main;
## uri跳转
location / {
## 站点目录 系统会在站点目录下找以 .html 结尾的 下面你指定的索引页面
root /usr/share/nginx/html;
## 默认首页,索引页面
index index.html index.htm;
}
}
## server层是网站配置层,包含在http层中 (location内还能写location)
## location是跳转 根据不同的uri来跳转不同的页面 location内还能写location
http{
server{
location /{
location /xxx{}
}
location /api{}
location /hg{}
}
server{
}
...
}
## 示例:
http{
access_log /var/log/nginx/access.log main;
server{
listen 80;
server_name www.wodeyumengouwo.com;
...
access_log /var/log/nginx/www.wodeyumengouwo.com.access.log main;
}
server{
listen 80;
server_name blog.wodeyumengouwo.com;
...
access_log /var/log/nginx/blog.wodeyumengouwo.access.log main;
}
}
多虚拟主机(多web网站配置)
在企业中,是不可能用一个nginx对应一套业务,多个网站都在一个nginx中配置
基于IP的多虚拟主机
## 创建虚拟网卡
[root@web01 nginx]# ifconfig eth0:0 10.0.0.10
[root@web01 nginx]# ifconfig eth0:1 10.0.0.11
[root@web01 nginx]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.7 netmask 255.255.255.0 broadcast 10.0.0.255
ether 00:0c:29:9e:96:72 txqueuelen 1000 (Ethernet)
RX packets 147631 bytes 97149935 (92.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 165013 bytes 128462517 (122.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.10 netmask 255.0.0.0 broadcast 10.255.255.255
ether 00:0c:29:9e:96:72 txqueuelen 1000 (Ethernet)
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.11 netmask 255.0.0.0 broadcast 10.255.255.255
ether 00:0c:29:9e:96:72 txqueuelen 1000 (Ethernet)
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.1.7 netmask 255.255.255.0 broadcast 172.16.1.255
inet6 fe80::20c:29ff:fe9e:967c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9e:96:7c txqueuelen 1000 (Ethernet)
RX packets 1533 bytes 650984 (635.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1145 bytes 244484 (238.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 277 bytes 34792 (33.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 277 bytes 34792 (33.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
## 配置虚拟主机文件
[root@web01 nginx]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim hg.conf
server{
listen 80;
server_name 10.0.0.10;
location /{
root /xixi;
index index.html;
}
}
## 创建指定的站点目录 xixi 并创建指定的索引页面文件
[root@web01 conf.d]# mkdir /xixi
[root@web01 conf.d]# vim /xixi/index.html
xixi
## 重启nginx服务 然后也浏览器访问 10.0.0.10
[root@web01 conf.d]# systemctl restart nginx
## 配置虚拟主机文件
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# cat > hg1.conf<<EOF
> server{
> listen 80;
> server_name 10.0.0.11;
> root /xixi1; # 可以指定的站点目录写在前面 那么下面的location的指定目录就都是/xixi1
>
> location /{
> index index.html;
> }
> }
> EOF
## 创建指定的站点目录 xixi1 并创建指定的索引页面文件
[root@web01 conf.d]# mkdir /xixi1
[root@web01 conf.d]# echo '111' > /xixi1/index.html
## 重启nginx服务 然后也浏览器访问 10.0.0.11
[root@web01 conf.d]# systemctl restart nginx
基于多端口的虚拟主机
## 修改配置文件
[root@web01 xixi]# vim /etc/nginx/conf.d/hg.conf
server{
listen 8081;
server_name 10.0.0.7;
location /{
root /xixi;
index index.html;
}
}
## 重启nginx
[root@web01 conf.d]# systemctl restart nginx
## 浏览器访问10.0.0.7:8081
## 修改配置文件
[root@web01 conf.d]# vim hg1.conf
server{
listen 8082;
server_name 10.0.0.7;
root /xixi1;
location /{
index index.html;
}
}
## 重启nginx
[root@web01 xixi]# systemctl restart nginx
## 浏览器访问10.0.0.7:8082
基于多域名的虚拟主机
## 修改配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/hg.conf
server{
listen 80;
server_name www.gouwo.com;
root /xixi;
index index.html;
}
## 重启nginx
[root@web01 conf.d]# systemctl restart nginx
## 本地域名解析访问
## 修改配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/hg1.conf
server{
listen 80;
server_name www.gouwo1.com;
root /xixi1;
index index.html;
}
## 重启nginx
[root@web01 conf.d]# systemctl restart nginx
## 本地域名解析访问
本地域名解析
### 哪个服务器访问网站 就得在哪个服务器上做域名解析
## 例如 web01 服务器上做本地DNS解析
[root@web01 ~]# vim /etc/hosts
10.0.0.7 www.gouwo.com
## 想在浏览器访问 就得在windows上做本地DNS解析配置↓
在windows系统中,配置本地的DNS
按win+r打开运行 输入drivers
进入到etc目录下 鼠标右键 选择 Notepad++ 打开文件hosts
nginx网站访问排错思路
1.当查看网站的状态码 如果有403 404这样的报错 就说明你的请求是可以到达你的服务器 这时候就得去查看你的服务器配置 查看conf配置文件 站点目录文件 索引页面 端口 ....
2.当网站都无法访问时 有可能是改完配置文件没重新加载 防火墙没关 端口没通 先ping一下你的服务器
重新加载命令:
1.systemctl reload nginx
2.systemctl restart nginx
检查端口的方法:
1,在windows 或者在本地shell上 ping 10.0.0.7
2,在windows 或者在本地shell上 telnet 10.0.0.7 80
3,在windows 或者在本地shell上 telnet 域名 80
日志管理
Nginx有非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志。日志格式通过log_format命令定义格式。
log_format详解
在nginx默认的配置文件中,log_format已经将日志格式定死,但是我们可不可以修改呢?
log_format
的作用是定义日志格式语法
# 配置语法: 包括: error.log access.log
Syntax: log_format name [escape=default|json] string ...;
Default: log_format combined "...";
Context: http
- 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 # 记录请求的方法以及请求的UIR 请求的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地址和客户端请求的服务器地址。
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_except
Nginx Access
日志配置实践
server {
listen 80;
server_name www.wodeyumengouwo.com;
#将当前的server网站的访问日志记录至对应的目录,使用main格式
access_log /var/log/nginx/www.wodeyumengouwo.com.log main;
location / {
root /code;
}
#当有人请求改favicon.ico时,不记录日志
location /favicon.ico {
access_log off;
return 200;
}
}
日志切割
使用logrotate
切割日志
[root@ecs ~]# cat /etc/logrotate.d/nginx
/var/log/nginx/*log {
create 0644 nginx nginx ## 指定日志轮转后新生成的日志文件的权限和所属用户组。
daily ## 每天切割日志
rotate 10 ## 日志保留10天
missingok ## 日志丢失忽略
notifempty ## 不切割空文件
compress ## 日志文件压缩
sharedscripts
postrotate ## # 切割日志执行的命令
/bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
endscript
}
日志切割后的效果
0 ✓ 15:46:21 root@gouwo,10.0.16.16:~ # ll /var/log/nginx/
total 5304
wodeyumengouwo.com_access.log-20230731.gz
-rw-r----- 1 www adm 23556 Aug 1 03:45 wodeyumengouwo.com_access.log-20230801.gz
-rw-r----- 1 www adm 36403 Aug 2 03:28 wodeyumengouwo.com_access.log-20230802.gz
-rw-r----- 1 www adm 34494 Aug 3 03:20 wodeyumengouwo.com_access.log-20230803.gz
-rw-r----- 1 www adm 19837 Aug 4 03:19 wodeyumengouwo.com_access.log-20230804.gz
Comments | NOTHING