第1章 Nginx介紹
1.Nginx是什么
Nginx (engine x) 是一個(gè)高性能的HTTP和反向代理web服務(wù)器.
Nginx是由伊戈?duì)枴べ愃饕驗(yàn)槎砹_斯訪問(wèn)量第二的Rambler.ru站點(diǎn)(俄文:Рамблер)開(kāi)發(fā)的.
第一個(gè)公開(kāi)版本0.1.0發(fā)布于2004年10月4日.
Nginx主要特點(diǎn)有
開(kāi)源: 直接獲取源代碼
高性能: 支持海量并發(fā)
可靠: 服務(wù)穩(wěn)定
2.為什么選擇 Nginx 服務(wù)
互聯(lián)網(wǎng)公司大都選擇 Nginx
1.Nginx 技術(shù)成熟,具備的功能是企業(yè)最常使用而且最需要的
2.適合當(dāng)前主流架構(gòu)趨勢(shì), 微服務(wù)、云架構(gòu)、中間層
3.統(tǒng)一技術(shù)棧,降低維護(hù)成本,降低技術(shù)更新成本.
3.Nginx重要特性
1.開(kāi)源,可以從官網(wǎng)直接獲取源代碼
2.高性能,Nginx性能非常殘暴,支持海量并發(fā)
3.高可靠,服務(wù)穩(wěn)定,占用內(nèi)存底
4.模塊化,Nginx具有豐富的模塊可以按需使用,并且有開(kāi)發(fā)能力的技術(shù)人員還可以二次開(kāi)發(fā)
5.支持熱更新配置文件,一般情況下修改配置文件可以平滑生效,不用重新啟動(dòng)服務(wù)
4.Nginx應(yīng)用場(chǎng)景
1.提供靜態(tài)網(wǎng)頁(yè)服務(wù)
2.作為多個(gè)網(wǎng)站和域名的虛擬主機(jī)服務(wù)
3.反向代理負(fù)載均衡服務(wù)
4.提供簡(jiǎn)單的下載服務(wù)
第2章 Nginx架構(gòu)
Nginx是多進(jìn)程架構(gòu),當(dāng)我們啟動(dòng)時(shí)會(huì)使用root用戶(hù)創(chuàng)建一個(gè)Master進(jìn)程,然后再M(fèi)aster進(jìn)程創(chuàng)建出多個(gè)Worker進(jìn)程.
1.master 主進(jìn)程功能
1.啟動(dòng)時(shí)讀取并檢查Nginx配置文件是否有語(yǔ)法或拼寫(xiě)錯(cuò)誤
2.根據(jù)配置文件里的參數(shù)創(chuàng)建和監(jiān)控worker進(jìn)程狀態(tài)
3.監(jiān)聽(tīng)本地的socekt,接收用戶(hù)發(fā)起的請(qǐng)求,然后worker進(jìn)程競(jìng)爭(zhēng)連接,獲勝的處理并響應(yīng)用戶(hù)請(qǐng)求
4.接收管理員發(fā)送的管理Nginx操作信號(hào)并將接收的管理信號(hào)發(fā)送給worker進(jìn)程
5.如果管理員發(fā)送了平滑重啟的命令,則會(huì)讀取配置文件并創(chuàng)建新的worker進(jìn)程,然后結(jié)束舊的worker進(jìn)程
2.worker 工作進(jìn)程功能
1.實(shí)際處理網(wǎng)絡(luò)請(qǐng)求的進(jìn)程是worker進(jìn)程
2.master進(jìn)程根據(jù)配置文件的參數(shù)決定創(chuàng)建多少個(gè)worker進(jìn)程
3.當(dāng)有用戶(hù)請(qǐng)求的事件產(chǎn)生時(shí),worker進(jìn)程會(huì)向master進(jìn)程競(jìng)爭(zhēng),獲勝的工作進(jìn)程和建立連接,并處理用戶(hù)的請(qǐng)求
4.接收用戶(hù)請(qǐng)求后,與后端服務(wù)器進(jìn)行通信,后端處理完后接收處理結(jié)果
5.接收并處理master進(jìn)程發(fā)送的信號(hào),例如啟動(dòng)/重啟/結(jié)束等信號(hào)
3.Nginx進(jìn)程間架構(gòu)圖

4.Nginx處理HTTP請(qǐng)求

5.Nginx模塊介紹
Nginx一個(gè)非常重要的特性就是擁有豐富的模塊,有核心的模塊,拓展的模塊和第三方展模塊.
Nginx模塊主要可以分為以下幾類(lèi):
核心模塊:
1.HTTP 模塊:用來(lái)發(fā)布http web服務(wù)網(wǎng)站的模塊。
2.event模塊:用來(lái)處理nginx 訪問(wèn)請(qǐng)求,并進(jìn)行回復(fù)。
基本模塊:
HTTP Access模塊: 用來(lái)進(jìn)行虛擬主機(jī)發(fā)布訪問(wèn)模塊,起到記錄訪問(wèn)日志.
HTTP FastCGI模塊:用于和PHP程序進(jìn)行交互的模塊,負(fù)責(zé)將來(lái)訪問(wèn)nginx 的PHP請(qǐng)求轉(zhuǎn)發(fā)到后端的PHP上.
HTTP Proxy模塊:配置反向代理轉(zhuǎn)發(fā)的模塊,負(fù)責(zé)向后端傳遞參數(shù).
HTTP Rewrite模塊:支持Rewrite 規(guī)則重寫(xiě),支持域名跳轉(zhuǎn).
第3章 Nginx安裝部署
Nginx分為幾種:
1.源碼編譯(1.版本隨意 2.安裝復(fù)雜 3.升級(jí)繁瑣)
2.epel倉(cāng)庫(kù)(1.版本較低 2.安裝簡(jiǎn)單 3.配置不易讀)
3.官方倉(cāng)庫(kù)(1.版本較新 2.安裝簡(jiǎn)單 3.配置易讀,推薦)
1.編譯安裝方法
官方文檔
http://nginx.org/en/docs/configure.html
1.創(chuàng)建www用戶(hù)
groupadd www -g 1001
useradd www -s /sbin/nologin -M -u 1001 -g 1001
id www
2.安裝依賴(lài)包
#注意yum源需先優(yōu)化
yum install openssl-devel pcre-devel -y
3.下載解壓軟件包
#創(chuàng)建下載軟件包目錄并解壓
mkdir /data/soft -p
cd /data/soft/
wget http://nginx.org/download/nginx-1.19.0.tar.gz
tar zxvf nginx-1.19.0.tar.gz
4.配置編譯參數(shù)
cd /data/soft/nginx-1.19.0/
./configure --help
./configure --user=www --group=www --prefix=/opt/nginx-1.19.0 --with-http_stub_status_module --with-http_ssl_module --with-pcre
5.編譯安裝
cd /data/soft/nginx-1.19.0/
make && make install
6.創(chuàng)建軟鏈接
cd /opt/
ln -s /opt/nginx-1.19.0/ /opt/nginx
ls -lh /opt/
7.檢查語(yǔ)法
[root@web-7 ~]# /opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx-1.19.0/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.19.0/conf/nginx.conf test is successful
[root@web-7 ~]# /opt/nginx/sbin/nginx -V
8.啟動(dòng)nginx
/opt/nginx/sbin/nginx
或者:
/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
9.檢查測(cè)試
[root@web-7 /opt/nginx]# netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12828/nginx: master
[root@web-7 /opt/nginx]# curl 10.0.0.7
或者:ps -ef|grep nginx
10.管理命令
#停止命令
/opt/nginx/sbin/nginx -s stop
#重新加載命令
/opt/nginx/sbin/nginx -s reload
11..編寫(xiě)systemd啟動(dòng)服務(wù)腳本
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=/opt/nginx/pid/nginx.pid
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
EOF
12.測(cè)試
1. systemctl start nginx
2. ps aux|grep nginx
3. systemctl stop nginx
ps -ef|grep nginx
systemctl reload nginx
ps aux|grep nginx
2..YUM安裝方法
1.安裝依賴(lài)包
yum install openssl-devel pcre-devel -y
2.配置官方y(tǒng)um源
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
[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
EOF
3.安裝nginx服務(wù)
yum install nginx -y
4.啟動(dòng)服務(wù)并配置開(kāi)機(jī)自啟動(dòng)
[root@web-7 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-7 ~]# systemctl start nginx
[root@web-7 ~]# systemctl enable nginx
5.測(cè)試訪問(wèn)
curl 10.0.0.7
3.nginx編譯安裝-ansible
說(shuō)明:流程如下
1.編寫(xiě)劇本
2.創(chuàng)建角色目錄
3.把劇本復(fù)制到tasks?錄
4.把編譯好的包?件復(fù)制到file?錄
5.拆分handlers
6.拆分vars
#7.精簡(jiǎn)tasks任務(wù)?件
8.編寫(xiě)調(diào)用文件
9.編寫(xiě)主機(jī)清單
10.調(diào)試運(yùn)行
1.創(chuàng)建???錄
cd /etc/ansible/roles/
mkdir nginx_server/{tasks,handlers,files,templates,vars} -p
[root@m-61 /etc/ansible/roles]# tree nginx_server/
nginx_server/
├── files
├── handlers
├── tasks
├── templates
└── vars
2.把劇本復(fù)制到tasks?錄
[root@m01 tasks] # cat /etc/ansible/roles/nginx_server/tasks/main.yml
- name: 01-tar
unarchive:
src: nginx-1.19.0.tar.gz
dest: /opt/
- name: 02-file
file:
src: /opt/nginx-1.19.0
dest: /opt/nginx
state: link
- name: 02-copy
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- { src: 'nginx.conf.j2' ,dest: '/opt/nginx/conf/nginx.conf' }
- { src: 'img.conf.j2' ,dest: '/opt/nginx/conf.d/img.conf' }
- { src: 'www.conf.j2' ,dest: '/opt/nginx/conf.d/www.conf' }
- { src: 'nginx.service.j2' ,dest: '/usr/lib/systemd/system/nginx.service' }
notify:
- restart nginx
- name: 03-start
systemd:
name: nginx
state: started
enabled: yes
daemon_reload: yes
3.把編譯好的包?件復(fù)制到file?錄
[root@m01 files]# pwd
/etc/ansible/roles/nginx_server/files
[root@m01 files]# ll
-rw-r--r-- 1 root root 3117817 May 11 17:39 nginx-1.19.0.tar.gz
4.拆分handlers
[root@m01 nginx_server]# cd handlers/
[root@m01 handlers]# cat main.yml
- name: restart nginx
systemd:
name: nginx
state: restarted
5.拆分vars
[root@m01 nginx_server]# cd vars/
[root@m01 vars]# cat main.yml
ssh_port: '22'
6.編寫(xiě)調(diào)用文件
cd /etc/ansible/
vim nginx_server.yaml
- hosts: nginx_server
roles:
- init
- nginx_server
7.調(diào)用:init--角色
[root@m01 roles]# cd init/
[root@m01 init]# ll
total 0
drwxr-xr-x 2 root root 6 May 11 18:24 files
drwxr-xr-x 2 root root 6 May 11 18:24 handlers
drwxr-xr-x 2 root root 22 May 11 19:50 tasks
drwxr-xr-x 2 root root 6 May 11 18:24 templates
drwxr-xr-x 2 root root 6 May 11 18:24 vars
[root@m01 init]# tree
.
├── files
├── handlers
├── tasks
│ └── main.yml
├── templates
└── vars
[root@m01 init]# cat tasks/main.yml
- name: create_group1
group:
name: www
gid: 1001
- name: create_user2
user:
name: www
uid: 1001
group: www
create_home: no
shell: /sbin/nologin
- name: create_data3
file:
path: "{{ item }}"
state: directory
owner: www
group: www
mode: '755'
loop:
- /data/
- /backup/
- /code/
- name: install_soft4
yum:
name: "{{ item }}"
state: latest
loop:
- rsync
- nfs-utils
8.編寫(xiě)主機(jī)清單
vim hosts
[nginx_server]
172.16.1.200
9.編寫(xiě)調(diào)用文件nginx.yaml
[root@m01 ansible]# vim nginx.yaml
- hosts: nginx_server
roles:
- init
- nginx_server
10.調(diào)試運(yùn)行
cd /etc/ansible/
ansible-playbook -C nginx.yaml
ansible-playbook nginx.yaml
注意:
[root@m01 files]# cd /opt/
[root@m01 opt]# ll
drwxr-xr-x 7 root root 77 May 11 17:12 nginx-1.19.0
-rw-r--r-- 1 root root 3057488 May 11 19:27 nginx-1.19.0.tar.gz
4.Nginx啟動(dòng)方式說(shuō)明
A.編譯安裝啟動(dòng)管理方式
nginx -t
nginx
nginx -s reload
nginx -s stop
B.yum安裝啟動(dòng)管理方式
nginx -t
systemctl start nginx
systemctl reload nginx
systemctl restart nginx
systemctl stop nginx
第4章 Nginx重要配置文件說(shuō)明
1.查看重要文件
[root@web-7 ~]# rpm -ql nginx
...................................................
/etc/logrotate.d/nginx #nginx日志切割的配置文件
/etc/nginx/nginx.conf #nginx主配置文件
/etc/nginx/conf.d #子配置文件
/etc/nginx/conf.d/default.conf #默認(rèn)展示的頁(yè)面一樣
/etc/nginx/mime.types #媒體類(lèi)型 (http協(xié)議中的文件類(lèi)型)
/etc/sysconfig/nginx #systemctl 管理 nginx的使用的文件
/usr/lib/systemd/system/nginx.service #systemctl 管理nginx(開(kāi)關(guān)重啟reload)配置文件
/usr/sbin/nginx #nginx命令
/usr/share/nginx/html #站點(diǎn)目錄 網(wǎng)站的根目錄
/var/log/nginx #nginx日志 access.log 訪問(wèn)日志
...................................................
2.查看已經(jīng)編譯的模塊
[root@web-7 ~]# nginx -V
3.配置文件注解
Nginx 主配置文件/etc/nginx/nginx.conf 是一個(gè)純文本類(lèi)型的文件,整個(gè)配置文件是以區(qū)塊的形式組織的.
每個(gè)區(qū)塊以一對(duì)大括號(hào){}來(lái)表示開(kāi)始與結(jié)束.
Nginx 主配置文件整體分為三塊進(jìn)行學(xué)習(xí)
CoreModule(核心模塊)
EventModule(事件驅(qū)動(dòng)模塊)
HttpCoreModule(http 內(nèi)核模塊)
第一部分:配置文件主區(qū)域配置
user nginx; #定義運(yùn)行nginx進(jìn)程的用戶(hù)
worker_processes auto; #Nginx運(yùn)行的work進(jìn)程數(shù)量(建議與CPU數(shù)量一致或auto)
error_log /var/log/nginx/error.log warn; #nginx錯(cuò)誤日志
pid /var/run/nginx.pid; #nginx運(yùn)行pid
第二部分:配置文件事件區(qū)域
events {
worker_connections 1024; #每個(gè) worker 進(jìn)程支持的最大連接數(shù)
}
第三部分:配置http區(qū)域
http {
include /etc/nginx/mime.types; #Nginx支持的媒體類(lèi)型庫(kù)文件
default_type application/octet-stream; #默認(rèn)的媒體類(lèi)型
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; #訪問(wèn)日志保存路徑
sendfile on; #開(kāi)啟高效傳輸模式
#tcp_nopush on; #必須配合tcp_nopush使用,當(dāng)數(shù)據(jù)包累計(jì)到一定大小后就發(fā)送
keepalive_timeout 65; #連接超時(shí)時(shí)間,單位是秒
#gzip on; #開(kāi)啟文件壓縮
include /etc/nginx/conf.d/*.conf; #包含子配置文件
}
第四部分:子配置文件內(nèi)容
server {
listen 80; #指定監(jiān)聽(tīng)端口
server_name localhost; #指定監(jiān)聽(tīng)的域名
location / {
root /usr/share/nginx/html; #定義站點(diǎn)的目錄
index index.html index.htm; #定義首頁(yè)文件
}
}
http server location 擴(kuò)展了解項(xiàng)
http{}層下允許有多個(gè) Server{}層,一個(gè) Server{}層下又允許有多個(gè) Location
http{} 標(biāo)簽主要用來(lái)解決用戶(hù)的請(qǐng)求與響應(yīng)。
server{} 標(biāo)簽主要用來(lái)響應(yīng)具體的某一個(gè)網(wǎng)站。
location{} 標(biāo)簽主要用于匹配網(wǎng)站具體 URL 路徑
第5章 Nginx虛擬主機(jī)配置實(shí)戰(zhàn)
1.基于域名的虛擬主機(jī)
[root@web-7 /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@web-7 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/01-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
2.基于端口的虛擬主機(jī)
[root@web-7 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/01-www.conf
server {
listen 81;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
[root@web-7 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/01-blog.conf
server {
listen 81;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
3.基于IP的虛擬主機(jī)
添加第二IP
ip addr add 10.0.0.11/24 dev eth0
配置文件
[root@web-7 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/01-www.conf
server {
listen 10.0.0.7:81;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
[root@web-7 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/01-blog.conf
server {
listen 10.0.0.11:81;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
第6章 Nginx虛擬主機(jī)配置優(yōu)化
所有配置都寫(xiě)入一個(gè)配置文件維護(hù)起來(lái)比較麻煩,如果修改錯(cuò)了,影響所有的頁(yè)面,所以我們應(yīng)該拆分nginx的配置文件為各個(gè)子配置
1.Nginx主配置文件
cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
}
2.子配置文件www
[root@web-7 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/01-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
3.子配置文件blog
[root@web-7 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/01-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
4.創(chuàng)建代碼目錄及首頁(yè)
mkdir /usr/share/nginx/html/{www,blog}
echo "www" > /usr/share/nginx/html/www/index.html
echo "blog" > /usr/share/nginx/html/blog/index.html
5.檢查語(yǔ)法重啟服務(wù)
[root@web-7 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-7 ~]# systemctl restart nginx
6.訪問(wèn)測(cè)試
[root@web-7 ~]# tail -1 /etc/hosts
10.0.0.7 www.oldboy.com blog.oldboy.com
[root@web-7 ~]# curl www.oldboy.com
www
[root@web-7 ~]# curl blog.oldboy.com
blog
第7章 Nginx日志
1.Nginx日志說(shuō)明
Nginx的日志分為訪問(wèn)日志和錯(cuò)誤日志兩種,其中訪問(wèn)日志的格式我們可以根據(jù)自己的需求定義成不同的格式,比如為了方便日后的日志分析,我們可以將Nginx日志設(shè)置為json格式.
2.Nginx日志字段解釋
$remote_addr #記錄客戶(hù)端 IP 地址
$remote_user #記錄客戶(hù)端用戶(hù)名
$time_local #記錄通用的本地時(shí)間
$time_iso8601 #記錄 ISO8601 標(biāo)準(zhǔn)格式下的本地時(shí)間
$request #記錄請(qǐng)求的方法以及請(qǐng)求的 http 協(xié)議
$status #記錄請(qǐng)求狀態(tài)碼(用于定位錯(cuò)誤信息)
$body_bytes_sent #發(fā)送給客戶(hù)端的資源字節(jié)數(shù),不包括響應(yīng)頭的大小
$bytes_sent #發(fā)送給客戶(hù)端的總字節(jié)數(shù)
$msec #日志寫(xiě)入時(shí)間。單位為秒,精度是毫秒。
$http_referer #記錄從哪個(gè)頁(yè)面鏈接訪問(wèn)過(guò)來(lái)的
$http_user_agent #記錄客戶(hù)端瀏覽器相關(guān)信息
$http_x_forwarded_for #記錄客戶(hù)端 IP 地址
$request_length #請(qǐng)求的長(zhǎng)度(包括請(qǐng)求行, 請(qǐng)求頭和請(qǐng)求正文)。
$request_time #請(qǐng)求花費(fèi)的時(shí)間,單位為秒,精度毫秒
# 注:如果 Nginx 位于負(fù)載均衡器,nginx 反向代理之后,web 服務(wù)器無(wú)法直接獲取到客戶(hù)端真實(shí)的 IP 地址.
# $remote_addr 獲取的是反向代理的 IP 地址.反向代理服務(wù)器在轉(zhuǎn)發(fā)請(qǐng)求的 http 頭信息中,
# 增加 X-Forwarded-For 信息,用來(lái)記錄客戶(hù)端 IP 地址和客戶(hù)端請(qǐng)求的服務(wù)器地址。
3.自定義Nginx日志格式
轉(zhuǎn)換為json格式日志:
log_format json '{ "time_local": "$time_local", '
'"remote_addr": "$remote_addr", '
'"referer": "$http_referer", '
'"request": "$request", '
'"status": $status, '
'"bytes": $body_bytes_sent, '
'"agent": "$http_user_agent", '
'"x_forwarded": "$http_x_forwarded_for", '
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"upstream_time": "$upstream_response_time",'
'"request_time": "$request_time"'
' }';
access_log /var/log/nginx/access.log json;
4.Nginx日志切割方法
為什么需要日志切割?
nginx日志默認(rèn)是不切割的,這樣當(dāng)我們運(yùn)行時(shí)間久了之后自然而然的會(huì)產(chǎn)生大量的日志,對(duì)我們?nèi)蘸蠓治霾皇呛苡押?,所以工作中一般都是按天切割日志?
第一種方法:寫(xiě)腳本切割