Nginx基本簡述
Nginx是一個開源且高性能,可靠的Http Web服務、代理服務,
- 開源: 直接獲取源代碼
- 高性能: 支持海量并發
- 可靠: 服務穩定
我們為什么選擇Nginx服務
Nginx非常輕量
- 功能模塊少(源代碼僅保留http與核心模塊代碼,其余不夠核心代碼會作為插件來安裝)
- 代碼模塊化(易讀,便于二次開發,對于開發人員是非常友好的)
互聯網公司都選擇Nginx
- Nginx技術成熟,具備的功能是企業最常使用而且最需要的
- 適合當前主流架構趨勢,微服務,云架構,中間層
- 統一技術堆疊,降低維護成本,降低技術更新成本
Nginx采用Epool網路模型,Apache采用Select模型
- Select: 當用戶發起一次請求,select模型就會進行一次遍歷掃描,從而導致性能低下,
- Epool: 當用戶發起請求,epool模型會直接進行處理,效率高效,并無連接限制,
Nginx 經典應用場景

- 開源的Web服務器
- 靜態資源
nginx
apache
IIS
lighttpd
tengine
openresty
- 動態資源
Tomcat
Jboos
resin
Nginx安裝
- nginx官網 :
http://nginx.org/
方式一: yum安裝(最好使用官方的源,epel源安裝的nginx組態檔內容比較亂)
1. 添加nginx的官方源
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-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
module_hotfixes=true
2. 下載nginx軟體
yum -y install nginx
3. 查看nginx版本
nginx -v
# nginx version: nginx/1.20.0
- 查看nginx當前的引數
nginx -V
# nginx version: nginx/1.20.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. 下載原始碼包
wget http://nginx.org/download/nginx-1.20.0.tar.gz
2. 解壓nginx原始碼包
tar -xf nginx-1.20.0.tar.gz
3. 編譯前系統檢查
cd nginx-1.20.0/
./configure (后面跟--prefix=/etc/nginx... nginx當前的引數,如果沒有需求跟指定的nginx引數的話,直接使用命令即可)
# ./configure: error: the HTTP rewrite module requires the PCRE library.
4. 安裝進行編譯時所缺少的庫
yum install pcre pcre-devel -y
5. 編譯前系統檢查
./configure (后面跟--prefix=/etc/nginx... nginx當前的引數,如果沒有需求跟指定的nginx引數的話,直接使用命令即可)
# ./configure: error: SSL modules require the OpenSSL library.
6. 安裝進行編譯時所缺少的庫
yum -y install openssl openssl-devel
7. 第三次進行系統檢查
./configure (后面跟--prefix=/etc/nginx... nginx當前的引數,如果沒有需求跟指定的nginx引數的話,直接使用命令即可)
# 沒有出現報錯
8. 開始進行編譯并安裝
make && make install
9. 運行nginx
/usr/local/nginx/sbin/nginx
# 我們安裝完成后,可以打開瀏覽器,輸入虛擬機的IP,如果顯示出Welcome to nginx!那就是代表安裝完成,
10. 添加環境變數
vim /root/.bash_profile 添加服務器系統的環境變數
NGINX_HOME=/usr/local/nginx/sbin
PATH=$PATH:$NGINX_HOME
export PATH
source /root/.bash_profile 多載root用戶系統的環境變數
# 加載完環境變數之后,執行nginx,就不需要再加檔案路徑了
nginx啟動與停止的兩種方式
方式一(適用于原始碼編譯安裝):
nginx 啟動
nginx -s stop 停止
nginx -s restart/reload 重啟
方式二(適用于yum安裝):
systemctl start nginx 啟動
systemctl restart nginx 重啟
systemctl stop nginx 停止
注意: 建議不要混合使用這兩種方式,如果我們使用第一種方式啟動nginx,那么是無法使用第二種方式進行管理的,
nginx目錄結構
rpm -ql nginx 查看nginx的目錄結構
/etc/logrotate.d/nginx 日志輪轉
/etc/nginx 默認的組態檔
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types 存放靜態資源的關系映射檔案
/etc/nginx/modules 模塊
/etc/nginx/nginx.conf 主組態檔
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx nginx命令執行檔案
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.20.0
/usr/share/doc/nginx-1.20.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx 默認的網站頁面
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx 快取
/var/log/nginx 日志
Nginx組態檔
- Nginx主組態檔
/etc/nginx/nginx.conf是一個純文本型別的檔案,整個組態檔是以區塊的形式組織的,每一個區塊以一對大括號{}來表示開始與結束, - Nginx主組態檔整體分為三塊,分別是CoreModule(核心模塊),EventModule(事件驅動模塊),HttpCoreModule(http內核模塊),
- 主組態檔
vim /etc/nginx/nginx.conf
======================================核心模塊==========================================
user nginx; nginx行程運行的用戶
worker_processes auto; nginx作業的行程數量(輔助行程自動)
error_log /var/log/nginx/error.log notice; nginx的錯誤日志【警告型別及其警告以上的都記錄】
pid /var/run/nginx.pid; nginx行程運行后的行程id
======================================事件模塊==========================================
events {
worker_connections 1024; 一個work行程的最大連接數
use epool; 使用epool的網路模型
}
===================================http核心層模塊=========================================
http {
include /etc/nginx/mime.types; 包含資源型別檔案
default_type application/octet-stream; 默認以下載方式傳輸給瀏覽器(前提是該資源在/etc/nginx/mime.types中無法找到)
日志格式定義
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; 包含目錄下的*.conf檔案
}
- 主組態檔中定義的區域組態檔
vim /etc/nginx/conf.d/default.conf
server { 定義一個網站
listen 80; 監聽埠
server_name localhost; 域名
#access_log /var/log/nginx/host.access.log main; 訪問日志(default.conf下的日志檔案優先級高于Nginx主組態檔中的日志檔案)
location / { 位置
root /usr/share/nginx/html; 代碼的主檔案位置
index index.html index.htm; 服務端默認回傳給用戶的檔案
}
}
- http server location擴展了解項
http{}層下允許有多個server{}層,一個server{}層下又允許有多個location,
http{}標簽主要用來解決用戶的請求與回應,
server{}標簽主要用來回應具體的某一個網站,
location{}標簽主要用于匹配網站具體URL路徑,
Nginx搭建一個靜態資源Web服務器
- 撰寫Nginx組態檔
vim /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name game.nana.com;
location / {
root /code;
index index.html index.html;
}
}
- 根據組態檔,創建目錄,上傳代碼
mkdir /code
cd /code
上傳一個游戲包到/code目錄下(rz -E 游戲包html.zip)
unzip 游戲包html.zip # 將壓縮包解壓到/code目錄下
- 重啟nginx服務
方式一:
systemctl restart nginx 立即重啟,強制重啟
方式二:
systemctl reload nginx 平滑重啟(等待互動完成之后再重啟)
- 配置域名決議
windows用戶:
C:\Windows\System32\drivers\etc
打開hosts檔案,添加:
192.168.15.7 game.nana.com
Mac用戶:
sudo vim /etc/hosts
打開hosts檔案,添加:
192.168.15.7 game.nana.com
- 檢查域名決議的ip是否是192.168.15.7
打開cmd,輸入
C:\Users\彭于晏>ping game.nana.com
# 正在 Ping game.nana.com [192.168.15.7] 具有 32 位元組的資料:
# 來自 192.168.15.7 的回復: 位元組=32 時間<1ms TTL=64
# 來自 192.168.15.7 的回復: 位元組=32 時間<1ms TTL=64
# 來自 192.168.15.7 的回復: 位元組=32 時間<1ms TTL=64
# 來自 192.168.15.7 的回復: 位元組=32 時間<1ms TTL=64
# 192.168.15.7 的 Ping 統計資訊:
# 資料包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失),
# 往返行程的估計時間(以毫秒為單位):
# 最短 = 0ms,最長 = 0ms,平均 = 0ms
- 通過瀏覽器訪問對應的專案
打開瀏覽器輸入:
game.nana.com
如果可以成功訪問到游戲頁面就表示nginx搭建成功了
Nginx配置虛擬主機有如下三種方式
方式一: 基于主機多IP方式
方式二: 基于埠的配置方式
方式三: 基于多個hosts名稱方式(多域名方式)
基于主機多IP方式
http://1.1.1.1 ------> ip 1.1.1.1 -------> 虛擬主機A
http://1.1.1.2 ------> ip 1.1.1.2 -------> 虛擬主機B
http://1.1.1.3 ------> ip 1.1.1.3 -------> 虛擬主機C
一個域名對應一個IP地址,一個IP地址對應一個網卡
- 基于多IP的方式,有如下兩種方式
方式一、多個網卡多IP的方式:
網卡1 <----------> ip1(1.1.1.1)
網卡2 <----------> ip2(1.1.1.2)
方式二、單網卡多IP的方式:
網卡1 <----------> ip1(1.1.1.1)
網卡1 <----------> ip2(1.1.1.2)
- 方式一: 多個網卡多IP的虛擬主機配置方式:
1. 撰寫Nginx組態檔
vim /etc/nginx/conf.d/ip.conf
server {
listen 172.16.1.7:80;
server_name _;
location / {
root /code_ip_eth0;
index index.html;
}
}
server {
listen 192.168.15.7:80;
server_name _;
location / {
root /code_ip_eth1;
index index.html;
}
}
2. 根據組態檔,創建目錄
mkdir /code_ip_eth0
echo eth0 > /code_ip_eth0/index.html
mkdir /code_ip_eth1
echo eth1 > /code_ip_eth1/index.html
3. 重啟nginx服務
nginx -t 檢查nginx語法是否錯誤
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx
4. 使用curl命令測驗
curl 172.16.1.7
# eth0
curl 192.168.15.7
# eth1
- 方式二: 單個網卡多IP的虛擬主機配置方式:
1. 給eth0網卡添加一個ip
ip addr add 172.16.1.8/24 dev eth0
2. 撰寫Nginx組態檔
vim /etc/nginx/conf.d/ip01.conf
server {
listen 172.16.1.8:80;
server_name _;
location / {
root /code_ip_eth0;
index index.html;
}
}
vim /etc/nginx/conf.d/ip02.conf
server {
listen 192.168.15.7:80;
server_name _;
location / {
root /code_ip_eth1;
index index.html;
}
}
3. 根據組態檔,創建目錄
mkdir /code_ip_eth0
echo Eth0 > /code_ip_eth0/index.html
mkdir /code_ip_eth1
echo Eth1 > /code_ip_eth1/index.html
4. 重啟nginx服務
nginx -t 檢查nginx主組態檔語法是否錯誤
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx
5. 使用curl命令測驗
curl 172.16.1.8
# Eth0
curl 192.168.15.7
# Eth1
基于埠的配置方式 (適用于企業內部)
http://1.1.1.1:80 ------> Listen:80 -------> 虛擬主機A
http://1.1.1.1:81 ------> Listen:81 -------> 虛擬主機B
http://1.1.1.1:82 ------> Listen:82 -------> 虛擬主機C
同一個ip,埠不一致,不同的埠對應不同的專案
- 基于埠的配置方式
1. 撰寫Nginx組態檔
vim /etc/nginx/conf.d/port.conf
server {
listen 81;
location / {
root /code_81;
index index.html;
}
}
server {
listen 82;
location / {
root /code_82;
index index.html;
}
}
2. 根據組態檔,創建目錄
mkdir /code_{81..82}
echo 81 > /code_81/index.html
echo 82 > /code_82/index.html
3. 重啟nginx服務
nginx -t 檢查nginx主組態檔語法是否錯誤
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx
4. 使用curl命令測驗
curl 192.168.15.7:81
# 81
curl 192.168.15.7:82
# 82
注意:如果我們在Nginx組態檔中沒有指定ip,
那么該服務器中的所有ip地址只要加上對應的埠號,都是可以訪問到組態檔中對應檔案中的內容的,
基于多個hosts名稱方式(多域名方式)
http://1.nana.com ------> 1.nana.com -------> 虛擬主機A
http://2.nana.com ------> 2.nana.com -------> 虛擬主機B
http://3.nana.com ------> 3.nana.com -------> 虛擬主機C
基于多個域名,一臺服務器,不同的域名訪問不同的專案
- 基于多域名的配置方式
cd /etc/nginx/conf.d
1. 撰寫Nginx組態檔
vim test01.nana.com.conf
server {
listen 80;
server_name test01.nana.com;
location / {
root /code/test01;
index index.html;
}
}
vim test02.nana.com.conf
server {
listen 80;
server_name test02.nana.com;
location / {
root /code/test02;
index index.html;
}
}
2. 根據組態檔,創建目錄
mkdir -p /code/test{01..02}
echo test01_server > /code/test01/index.html
echo test02_server > /code/test02/index.html
3. 重啟nginx服務
nginx -t 檢查nginx主組態檔語法是否錯誤
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx
4. 配置域名決議
windows用戶:
C:\Windows\System32\drivers\etc
打開hosts檔案,添加:
192.168.15.7 test01.nana.com
192.168.15.7 test02.nana.com
- 檢查域名決議的ip是否是192.168.15.7
打開cmd,輸入
C:\Users\彭于晏>ping test01.nana.com
C:\Users\彭于晏>ping test02.nana.com
5. 通過瀏覽器訪問該網站
http://test01.nana.com/ --訪問結果--> test01_server
http://test02.nana.com/ --訪問結果--> test02_server
- 我們將域名決議檔案(C:\Windows\System32\drivers\etc)中的
192.168.15.7 test02.nana.com修改成192.168.15.7 test03.nana.com,我們通過瀏覽器去訪問test03.nana.com,發現訪問的結果為test01_server,訪問的域名和網頁內容不匹配(域名和ip地址沒有對應),

Nginx報錯排查
- 修改完組態檔,記得使用nginx -t檢查語法,
- 如果沒有檢查語法,直接多載導致報錯,使用命令
systemctl status nginx.server -l/journalctl -xe進行排錯,
Nginx日志
- 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 記錄請求的方法以及請求的http協議
$status 記錄請求的狀態碼(用于定位錯誤資訊)
$body_bytes_sent 發送給客戶端的資源位元組數,不包回應頭的大小
$msec 日志寫入時間,單位為秒,精度是毫秒
$http_referer 記錄從哪個頁面鏈接訪問過來的
$http_user_agent 記錄客戶端瀏覽器相關資訊
$http_x_forwarded_for 記錄客戶端IP地址
$request_length 請求的長度(包括請求行,請求頭和請求正文)
$request_time 請求花費的時間,單位為秒,精度為毫秒
注:如果Nginx位于負載均衡器,nginx反向代理之后,為服務器無法直接獲取客戶端的真實IP地址,
$remote_addr獲取的是反向代理的IP地址,反向代理服務器在轉發請求的http頭資訊中,
增加x-forwarded-for資訊,用來記錄客戶端IP地址和客戶端請求的服務器地址,
- 修改主組態檔訪問日志的格式,進行測驗
vim /etc/nginx/nginx.conf
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format ttt '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" "$request_time"';
access_log /var/log/nginx/access.log ttt;
我們重繪http://test01.nana.com/瀏覽器頁面做測驗,
tail -1 /var/log/nginx/access.log
# 192.168.15.1 - - [06/May/2021:15:48:56 +0800] "GET / HTTP/1.1" 200 14 "-" "0.000"
Nginx訪問日志與錯誤日志
訪問日志(默認訪問日志路徑/var/log/nginx/access.log)
- 如果在http層配置了access_log,在server層沒有配置,那么所有的server日志都寫入http層,
- 如果在http層和server層都配置了access_log,那么所有的server日志都寫入server層,
- server層日志寫入的優先級高于http層,
在server層配置access_log做訪問測驗
1. 將當前的server網站的訪問日志記錄至對應的目錄,使用main格式
vim test01.nana.com.conf
server {
listen 80;
server_name test01.nana.com;
access_log /var/log/nginx/test01.log main; # 日志目錄存在,那么記錄日志的檔案會自動創建
location / {
root /code/test01;
index index.html;
}
# 當有人請求改favicon.ico時,不記錄日志
# location /favicon.ico {
# access_log off;
# return 200;
# }
}
2. 重啟服務
nginx -t
systemctl restart nginx
- 在瀏覽器清空快取多載http://test01.nana.com/,查看日志檔案下的內容
ls /var/log/nginx/
# access.log error.log test01.log
tail -f /var/log/nginx/test01.log
# 192.168.15.1 - - [06/May/2021:16:25:46 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.51" "-"
錯誤日志(默認錯誤日志路徑/var/log/nginx/error.log)
我們在訪問請求中的報錯,要查看錯誤日志的報錯進行排錯
- 模擬訪問請求報錯(1):
我們在瀏覽器中故意輸錯域名: http://test01.nana.com/xxx
查看錯誤日志報錯資訊
tail -f /var/log/nginx/error.log
# 2021/05/06 16:44:47 [error] 2882#2882: *1 open() "/code/test01/xxx" failed (2: No such file or directory), client: 192.168.15.1, server: test01.nana.com, request: "GET /xxx HTTP/1.1", host: "test01.nana.com"
- 模擬訪問請求報錯(2):
我們將server層中指定的檔案index.html,檔案名故意改成index.htm
cd /code/test01/
mv index.html index.htm
查看錯誤日志報錯資訊
tail -f /var/log/nginx/error.log
# 2021/05/06 16:48:55 [error] 2882#2882: *5 directory index of "/code/test01/" is forbidden, client: 192.168.15.1, server: test01.nana.com, request: "GET / HTTP/1.1", host: "test01.nana.com"
日志的切割logrotate
- 日志的記錄如果一直記錄在一個日志檔案中,隨著時間的推移,日志檔案會變得越來越大,可能會導致日志檔案無法查看,為了解決這個問題,nginx服務自帶了日志切割(
logrotate)的功能, - 原始碼編譯可能會需要自己去寫腳本,yum安裝nginx會自帶日志自動切割功能,
- 查看日志切割默認的組態檔
cat /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 # mv access.log --> access.log-20210506.gz
kill -USR1 `cat /var/run/nginx.pid` # 發送信號,切割好之后重新加載日志檔案
fi
endscript
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/283216.html
標籤:其他
上一篇:【筆記】性能優化28個策略原則
