第一章 Nginx介紹
Nginx是什么
Nginx 是一個(gè)開源且高性能、可靠的 Http Web 服務(wù)、代理服務(wù)汤徽。
開源: 直接獲取源代碼
高性能: 支持海量并發(fā)
可靠: 服務(wù)穩(wěn)定
我們?yōu)槭裁催x擇 Nginx 服務(wù)
Nginx 非常輕量
功能模塊少 (源代碼僅保留 http 與核心模塊代碼,其余不夠核心代碼會(huì)作為插件來安裝)
代碼模塊化 (易讀涵叮,便于二次開發(fā),對于開發(fā)人員非常友好)
互聯(lián)網(wǎng)公司都選擇 Nginx
1.Nginx 技術(shù)成熟熙含,具備的功能是企業(yè)最常使用而且最需要的
2.適合當(dāng)前主流架構(gòu)趨勢, 微服務(wù)罚缕、云架構(gòu)、中間層
3.統(tǒng)一技術(shù)棧, 降低維護(hù)成本, 降低技術(shù)更新成本怎静。
Nginx重要特性
Nginx 采用 Epool 網(wǎng)絡(luò)模型邮弹, Apache 采用 Select 模型
Select: 當(dāng)用戶發(fā)起一次請求, select 模型就會(huì)進(jìn)行一次遍歷掃描蚓聘,從而導(dǎo)致性能低下腌乡。
Epool: 當(dāng)用戶發(fā)起請求, epool 模型會(huì)直接進(jìn)行處理夜牡,效率高效与纽,并無連接限制
Nginx應(yīng)用場景
第二章 Nginx安裝部署
Nginx分為幾種
1.源碼編譯(1.版本隨意 2.安裝復(fù)雜 3.升級繁瑣)
2.epel倉庫(1.版本較低 2.安裝簡單 3.配置不易讀)
3.官方倉庫(1.版本較新 2.安裝簡單 3.配置易讀,推薦)
下面分別介紹編譯安裝和yum安裝方法
1.編譯安裝方法
創(chuàng)建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 gcc gcc+ -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
配置編譯參數(shù)
[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
創(chuàng)建軟鏈接
[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
啟動(dòng)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
2.YUM安裝方法
安裝依賴包
[root@web01 ~]# yum install openssl-devel pcre-devel gcc gcc+ -y
配置官方y(tǒng)um源
[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服務(wù)
[root@web01 ~]# yum install nginx -y
啟動(dòng)服務(wù)并配置開機(jī)自啟動(dòng)
[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啟動(dòng)方式說明
編譯安裝啟動(dòng)管理方式
nginx -t
nginx
nginx -s reload
nginx -s stop
yum安裝啟動(dòng)管理方法
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 #默認(rèn)展示的頁面一樣
/etc/nginx/mime.types #媒體類型 (http協(xié)議中的文件類型)
/etc/sysconfig/nginx #systemctl 管理 nginx的使用的文件
/usr/lib/systemd/system/nginx.service #systemctl 管理nginx(開 關(guān) 重啟 reload)配置文件
/usr/sbin/nginx #nginx命令
/usr/share/nginx/html #站點(diǎn)目錄 網(wǎng)站的根目錄
/var/log/nginx #nginx日志 access.log 訪問日志
...................................................
查看已經(jīng)編譯的模塊
[root@web01 ~]# nginx -V
配置文件注解
Nginx 主配置文件/etc/nginx/nginx.conf 是一個(gè)純文本類型的文件,整個(gè)配置文件是以區(qū)塊的形式組織的急迂。一般影所,每個(gè)區(qū)塊以一對大括號{}來表示開始與結(jié)束。
Nginx 主配置文件整體分為三塊進(jìn)行學(xué)習(xí)僚碎,分別是
CoreModule(核心模塊)
EventModule(事件驅(qū)動(dòng)模塊)
HttpCoreModule(http 內(nèi)核模塊)
第一部分:配置文件主區(qū)域配置
user nginx; #定義運(yùn)行nginx進(jìn)程的用戶
worker_processes 1; #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支持的媒體類型庫文件
default_type application/octet-stream; #默認(rèn)的媒體類型
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; #連接超時(shí)時(shí)間
#gzip on; #開啟壓縮
include /etc/nginx/conf.d/*.conf; #包含子配置文件
}
第四部分:子配置文件內(nèi)容
[root@web01 ~]# egrep -v "#|^$" /etc/nginx/conf.d/default.conf
server {
listen 80; #指定監(jiān)聽端口
server_name localhost; #指定監(jiān)聽的域名
location / {
root /usr/share/nginx/html; #定義站點(diǎn)的目錄
index index.html index.htm; #定義首頁文件
}
error_page 500 502 503 504 /50x.html; #優(yōu)雅顯示頁面信息
location = /50x.html {
root /usr/share/nginx/html;
}
}
http server location 擴(kuò)展了解項(xiàng)
http{}層下允許有多個(gè) Server{}層猴娩,一個(gè) Server{}層下又允許有多個(gè) Location
http{} 標(biāo)簽主要用來解決用戶的請求與響應(yīng)。
server{} 標(biāo)簽主要用來響應(yīng)具體的某一個(gè)網(wǎng)站勺阐。
location{} 標(biāo)簽主要用于匹配網(wǎng)站具體 URL 路徑
第四章 Nginx虛擬主機(jī)配置實(shí)戰(zhàn)
基于域名的虛擬主機(jī)
[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;
}
}
}
基于端口的虛擬主機(jī)
[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的虛擬主機(jī)
添加第二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虛擬主機(jī)配置優(yōu)化
所有配置都寫入一個(gè)配置文件維護(hù)起來比較麻煩卷中,如果修改錯(cuò)了,影響所有的頁面渊抽,所以我們應(yīng)該拆分nginx的配置文件為各個(gè)子配置,并且每個(gè)域名擁有自己獨(dú)立的訪問日志
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;
access_log /var/log/nginx/www.access.log main;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
子配置文件blog
[root@web01 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/02-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
access_log /var/log/nginx/blog.access.log main;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
創(chuàng)建代碼目錄及首頁
[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
檢查語法重啟服務(wù)
[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狀態(tài)模塊
nginx狀態(tài)模塊: --with-http_stub_status_module
狀態(tài)模塊配置文件
[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 # 記錄通用的本地時(shí)間
$time_iso8601 # 記錄 ISO8601 標(biāo)準(zhǔn)格式下的本地時(shí)間
$request # 記錄請求的方法以及請求的 http 協(xié)議
$status # 記錄請求狀態(tài)碼(用于定位錯(cuò)誤信息)
$body_bytes_sent # 發(fā)送給客戶端的資源字節(jié)數(shù)蟆豫,不包括響應(yīng)頭的大小
$bytes_sent # 發(fā)送給客戶端的總字節(jié)數(shù)
$msec # 日志寫入時(shí)間。單位為秒腰吟,精度是毫秒无埃。
$http_referer # 記錄從哪個(gè)頁面鏈接訪問過來的
$http_user_agent # 記錄客戶端瀏覽器相關(guān)信息
$http_x_forwarded_for #記錄客戶端 IP 地址
$request_length # 請求的長度(包括請求行, 請求頭和請求正文)毛雇。
$request_time # 請求花費(fèi)的時(shí)間嫉称,單位為秒,精度毫秒
# 注:如果 Nginx 位于負(fù)載均衡器灵疮, nginx 反向代理之后织阅, web 服務(wù)器無法直接獲取到客 戶端真實(shí)的 IP 地址。
# $remote_addr 獲取的是反向代理的 IP 地址震捣。 反向代理服務(wù)器在轉(zhuǎn)發(fā)請求的 http 頭信息中荔棉,
# 增加 X-Forwarded-For 信息,用來記錄客戶端 IP 地址和客戶端請求的服務(wù)器地址蒿赢。
第八章 Nginx注意事項(xiàng)
1.如果是yum安裝润樱,啟動(dòng)關(guān)閉命令推薦使用systemctl。不要混著nginx -s這樣用羡棵。
2.相同域名相同端口會(huì)報(bào)沖突壹若,比如都是0.0.0.0:80
3.沒有首頁會(huì)報(bào)403而不是404
4.如果訪問的域名全部都沒有匹配上,那么會(huì)默認(rèn)訪問第一個(gè)網(wǎng)站皂冰,按照a-z順序排列店展,如果想指定一個(gè)網(wǎng)站為首先跳轉(zhuǎn),可以配置文件名為01-www.conf類似秃流。
日志切割
[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
}