天天看點

Nginx快速入門(安裝、配置詳解)

作者:AndrewNotes
Nginx快速入門(安裝、配置詳解)

Nginx概述

  • 俄羅斯人開發的 engine x
  • 開源 可直接擷取源代碼
  • 高性能:支援高并發:能支援幾萬并發連接配接
  • 資源消耗少:3萬并發,開啟10個Nginx線程消耗不到200M記憶體
  • 可靠,服務穩定
  • 大有趕超老大哥Apache的趨勢 2019 42%

why Nginx

  • Nginx非常輕量 功能子產品少 (源代碼僅保留http與核心子產品代碼,其餘不夠核心代碼會作為插件來安裝) 代碼子產品化 (易讀,便于二次開發,對于開發人員非常友好)
  • 網際網路公司都選擇Nginx
  • 1.Nginx技術成熟,具備的功能是企業最常使用而且最需要的
  • 2.适合目前主流架構趨勢,微服務、雲架構、中間層
  • 3.統一技術棧,降低維護成本,降低技術更新成本。
  • Nginx采用Epool網絡模型,Apache采用Select模型
  • Select: 當使用者發起一次請求,select模型就會進行一次周遊掃描,進而導緻性能低下。
  • Epool: 當使用者發起請求,epool模型會直接進行處理,效率高效,并無連接配接限制。

應用場景

Nginx快速入門(安裝、配置詳解)

Nginx安裝

安裝方式:

1.源碼編譯=>Nginx (1.版本随意 2.安裝複雜 3.更新繁瑣 4.規範 5.便于管理)

2.epel倉庫=>Nginx (1.版本較低 2.安裝簡單 3.配置不易讀)

3.官方倉庫=>Nginx (1.版本較新 2.安裝簡單 3.配置易讀)

2與3安裝對比:

# web01 官方倉庫安裝

https://nginx.org/en/download.html 拉到最底下
https://nginx.org/en/linux_packages.html
https://nginx.org/en/linux_packages.html#RHEL-CentOS

# 先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


 0 ✓ 11:17:56 root@web01,172.16.1.7:~ # nginx -v
nginx version: nginx/1.22.0

# web02 epel倉庫安裝
直接yum install -y nginx 是走eple倉庫

root@web02,172.16.1.8:~ # nginx -v
nginx version: nginx/1.20.1                 (版本較低)
公司沒規定就直接安,因為快           

Nginx啟停

# 啟動
systemctl start nginx
nginx
/usr/sbin/nginx

# nginx加入開機自啟
systemctl enable nginx

# 停止
systemctl stop nginx
nginx -s stop
/usr/sbin/nginx -s stop

# 重新開機
systemctl restart nginx

# 重新加載
systemctl reload nginx


# 選項
-c 指定配置檔案的路徑
-t 檢測配置檔案文法問題(不檢測單詞拼寫)
-s 啟停重載 屬于服務操作
-v 檢視版本号
-V 檢查nginx的編譯參數

           

驗證是否安裝成功:浏覽器通路相應伺服器

注意防火牆和SElinux是否關閉。

Nginx快速入門(安裝、配置詳解)

Nginx配置檔案介紹

0 ✓ 15:44:00 root@web01,172.16.1.7:/ # tree /etc/nginx
/etc/nginx
├── conf.d
│   └── default.conf
├── fastcgi_params
├── mime.types
├── modules -> ../../usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
└── uwsgi_params

 0 ✓ 11:45:13 root@web01,172.16.1.7:/etc/nginx # ll
total 24
drwxr-xr-x 2 root root   26 Sep 28 11:17 conf.d    # <<-----那個預設頁面的配置檔案default.conf就在這裡面
-rw-r--r-- 1 root root 1007 May 24 23:35 fastcgi_params
-rw-r--r-- 1 root root 5349 May 24 23:35 mime.types
wxrwxrwx 1 root root   29 Sep 28 11:17 modules -> ../../usr/lib64/nginx/modules
-rw-r--r-- 1 root root  648 May 24 23:33 nginx.conf
-rw-r--r-- 1 root root  636 May 24 23:35 scgi_params
-rw-r--r-- 1 root root  664 May 24 23:35 uwsgi_params           

1、Nginx主配置檔案

路徑 類型 作用
/etc/nginx/nginx.conf 配置檔案 nginx主配置檔案
/etc/nginx/conf.d/default.conf 配置檔案 nginx網站示例配置檔案

2、Nginx代理相關參數檔案

路徑 類型 作用
/etc/nginx/fastcgi_params 配置檔案 Fastcgi代理配置檔案(配合PHP動态服務
/etc/nginx/scgi_params 配置檔案 scgi代理配置檔案
/etc/nginx/uwsgi_params 配置檔案 uwsgi代理配置檔案(配合python

3、Nginx編碼相關配置檔案

路徑 類型 作用
/etc/nginx/win-utf 配置檔案 Nginx編碼轉換映射檔案
/etc/nginx/koi-utf 配置檔案 Nginx編碼轉換映射檔案
/etc/nginx/koi-win 配置檔案 Nginx編碼轉換映射檔案
/etc/nginx/mime.types 配置檔案 媒體類型檔案

4、Nginx管理相關指令

路徑 類型 作用
/usr/sbin/nginx 指令 Nginx指令行管理終端工具
/usr/sbin/nginx-debug 指令 Nginx指令行與終端調試工具

5、Nginx日志相關目錄及檔案

路徑 類型 作用
/var/log/nginx 目錄 Nginx預設存放日志目錄
/etc/logrotate.d/nginx 配置檔案 Nginx預設的日志切割

Nginx主配置檔案介紹

Nginx主配置檔案/etc/nginx/nginx.conf是一個純文字類型的檔案,整個配置檔案是以區塊的形式組織的。一般,每個區塊以一對大括号{}來表示開始與結束。

Nginx主配置檔案整體分為三塊:
分别是CoreModule(核心子產品),EventModule(事件驅動子產品),HttpCoreModule(http核心子產品)

nginx配置檔案,每一行都以';'結尾           
0 ✓ 11:45:14 root@web01,172.16.1.7:/etc/nginx # grep -Ev '^$|#' /etc/nginx/nginx.conf

## 核心層(核心子產品)
user nginx;                    # nginx的啟動使用者        
worker_processes auto;         # nginx運作的work程序數量(建議與CPU數量一緻或auto)
                                  auto:自動根據cpu的核心數來啟動對應的工作程序數
error_log /var/log/nginx/error.log notice;  # nginx錯誤日志存放路徑
pid       /var/run/nginx.pid;   # 啟動後程序号存放路徑

## 事件層(事件驅動子產品)
events {
  worker_connections  1024;   # 每個worker程序支援的最大連接配接數
}

## 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;                           // 高效檔案傳輸
  keepalive_timeout  65;                         // 長連接配接逾時時間
  include /etc/nginx/conf.d/*.conf;             //包含nginx其他子配置檔案(網站虛拟機配置檔案)
}

##(web02的nginx版本便有)
#使用Server配置網站, 每個Server{}标簽對應一個獨立的網站站點(所謂虛拟主機)

server {
      listen       80;                # 監聽端口, 預設80
      server_name driverzeng.com;    # 提供的域名
      access_log access.log;         # 該網站的通路日志

       'location' / {                      # 控制網站通路路徑
          root   /usr/share/nginx/html;   # 存放網站源代碼的位置
          index index.html index.htm;    # 預設傳回網站的檔案
      }
  }
  ...
   # 第二個虛拟主機配置
server {
  ...
  }
           
# 日志格式中的參數
'$remote_addr - $remote_user [$time_local] "$request" '  

$remote_addr 遠端的IP(上一通路你的IP)
$remote_user 登入的使用者(網頁沒有登入使用者則為空)
[$time_local] 時間
"$request"   請求方式 請求URL HTTP協定版本号                    

'$status $body_bytes_sent "$http_referer" '
$status 狀态碼
$body_bytes_sent 流量
"$http_referer" 跳轉位址(從哪個網站跳轉過來的)

'"$http_user_agent" "$http_x_forwarded_for"'
"$http_user_agent" 用戶端資訊
"$http_x_forwarded_for"   記錄透傳ip位址???與代理伺服器相關
           

日志實操

# 檢視日志

root@web01,172.16.1.7:~ # ll /var/log/nginx/
total 68
-rw-r----- 1 nginx adm 31262 Sep 28 11:39 access.log
-rw-r----- 1 nginx adm 33759 Sep 28 11:39 error.log


# 啟動服務後,浏覽器打開10.0.0.7顯示nginx預設頁面,重新整理一下,日志便出來一條

root@web01,172.16.1.7:~ # tail -f /var/log/nginx/access.log
10.0.0.1 - - [29/Sep/2022:09:37:48 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
10.0.0.1 - - [29/Sep/2022:09:39:58 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"


10.0.0.1                       遠端IP,網卡
- -                           該網站不需要登入
[29/Sep/2022:09:37:48 +0800]   時間
"GET / HTTP/1.1"               請求方式 請求URI HTTP協定版本号

304 0 "-" 狀态碼(緩存) 流量 無跳轉

"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" 用戶端資訊

           

繼續閱讀