主頁 > 軟體設計 > nginx簡介

nginx簡介

2021-10-28 07:37:21 軟體設計

1. nginx簡介

文章目錄

    • 1. nginx簡介
    • 2. nginx的特性與優點
      • 2.1 nginx的特性
      • 2.2 nginx的優點
    • 3. nginx的功能及應用類別
      • 3.1 nginx的基本功能
      • 3.2 nginx的擴展功能
      • 3.3 nginx的應用類別
    • 4. nginx的模塊與作業原理
      • 4.1 nginx的模塊分類
      • 4.2 nginx的作業原理
      • nginx的行程架構:
    • 5. Nginx的應用環境
    • 6. Nginx的安裝
    • 6.1 nginx安裝后配置
    • 7 nginx的組態檔詳解
      • 7.1 nginx.conf配置詳解
      • 7.2 用于除錯、定位問題的配置引數
      • 7.3 正常運行必備的配置引數
      • 7.4 優化性能的配置引數
      • 7.5 事件相關的配置:event{}段中的配置引數
      • 7.6 網路連接相關的配置引數
      • 7.7 fastcgi的相關配置引數
      • 7.8 常需要進行調整的引數
      • 7.9 nginx作為web服務器時使用的配置:http{}段的配置引數
        • 7.9.1http 協議
        • 7.9.2 http{}段配置指令:
        • 7.9.3錯誤頁面
        • 7.9.4平滑升級
        • 7.9.5 location區段
        • 7.9.6 訪問控制
        • 7.9.7 基于用戶認證
        • 7.9.8 https配置

nginx(發音同engine x)是一款輕量級的Web服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,并在一個BSD-like協議下發行,

nginx由俄羅斯的程式設計師Igor Sysoev所開發,最初供俄國大型的入口網站及搜尋引擎Rambler使用,

nginx的特點是占有記憶體少,并發能力強,事實上nginx的并發能力確實在同型別的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等,

Nginx是一款自由的、開源的、高性能的HTTP服務器和 反向代理 服務器;同時也是一個IMAP、POP3、SMTP代理服務器;Nginx可以作為一個HTTP服務器進行網站的發布處理,另外Nginx可以作為反向代理進行負載均衡的實作,

Nginx使用基于事件驅動架構,使得其可以支持數以百萬級別的TCP連接
高度的模塊化和自由軟體許可證使得第三方模塊層出不窮(開源)

Nginx是一個跨平臺服務器,可以運行在Linux,Windows,FreeBSD,Solaris,AIX,Mac OS等作業系統上

2. nginx的特性與優點

2.1 nginx的特性

nginx是一個很牛的高性能Web和反向代理服務器,它具有很多非常優越的特性:

  • 在高連接并發的情況下,nginx是Apache服務器不錯的替代品,能夠支持高達50000個并發連接數的回應
  • 使用epoll and kqueue作為開發模型
  • nginx作為負載均衡服務器:nginx既可在內部直接支持和PHP程式對外進行服務,也可支持作為HTTP代理服務器對外進行服務
  • nginx采用C進行撰寫,不論系統資源開銷還是CPU使用效率都比Perlbal要好很多

2.2 nginx的優點

  • 高并發連接:官方測驗能夠支撐5萬并發連接,在實際生產環境中跑到2-3萬并發連接數
  • 記憶體消耗少:在3萬并發連接下,開啟的10個nginx行程才消耗150M記憶體(15M*10=150M)
  • 組態檔非常簡單:風格跟程式一樣通俗易懂
  • 成本低廉:nginx為開源軟體,可以免費使用,而購買F5 BIG-IP、NetScaler等硬體負載均衡交換機則需要十多萬至幾十萬人民幣
  • 支持Rewrite重寫規則:能夠根據域名、URL的不同,將HTTP請求分到不同的后端服務器群組
  • 內置的健康檢查功能:如果Nginx Proxy后端的某臺Web服務器宕機了,不會影響前端訪問
  • 節省帶寬:支持GZIP壓縮,可以添加瀏覽器本地快取的Header頭
  • 穩定性高:用于反向代理,宕機的概率微乎其微
  • 模塊化設計:模塊可以動態編譯
  • 外圍支持好:檔案全,二次開發和模塊較多
  • 支持熱部署:可以不停機多載組態檔
  • 支持事件驅動、AIO(AsyncIO,異步IO)、mmap(Memory Map,記憶體映射)等性能優化

3. nginx的功能及應用類別

3.1 nginx的基本功能

  • 靜態資源的web服務器,能快取打開的檔案描述符
  • http、smtp、pop3協議的反向代理服務器
  • 快取加速、負載均衡
  • 支持FastCGI(fpm,LNMP),uWSGI(Python)等
  • 模塊化(非DSO機制),過濾器zip、SSI及影像的大小調整
  • 支持SSL

3.2 nginx的擴展功能

  • 基于名稱和IP的虛擬主機
  • 支持keepalive
  • 支持平滑升級
  • 定制訪問日志、支持使用日志緩沖區提高日志存盤性能
  • 支持URL重寫
  • 支持路徑別名
  • 支持基于IP及用戶的訪問控制
  • 支持速率限制,支持并發數限制

3.3 nginx的應用類別

  • 使用nginx結合FastCGI運行PHP、JSP、Perl等程式
  • 使用nginx作反向代理、負載均衡、規則過濾
  • 使用nginx運行靜態HTML網頁、圖片
  • nginx與其他新技術的結合應用

4. nginx的模塊與作業原理

nginx由內核和模塊組成,其中,內核的設計非常微小和簡潔,完成的作業也非常簡單,僅僅通過查找組態檔將客戶端請求映射到一個location block(location是nginx配置中的一個指令,用于URL匹配),而在這個location中所配置的每個指令將會啟動不同的模塊去完成相應的作業,

因此模塊可以看做Nginx真正的勞動作業者,通常一個location中的指令會涉及一個handler模塊和多個filter模塊(當然,多個location可以復用同一個模塊),handler模塊負責處理請求,完成回應內容的生成,而filter模塊對回應內容進行處理,

4.1 nginx的模塊分類

nginx的模塊從結構上分為核心模塊、基礎模塊和第三方模塊

  • HTTP模塊、EVENT模塊和MAIL模塊等屬于核心模塊
  • HTTP Access模塊、HTTP FastCGI模塊、HTTP Proxy模塊和HTTP Rewrite模塊屬于基本模塊
  • HTTP Upstream模塊、Request Hash模塊、Notice模塊和HTTP Access Key模塊屬于第三方模塊

用戶根據自己的需要開發的模塊都屬于第三方模塊,正是有了如此多模塊的支撐,nginx的功能才會如此強大

ginx模塊從功能上分為三類,分別是:

  • Handlers(處理器模塊),此類模塊直接處理請求,并進行輸出內容和修改headers資訊等操作,handlers處理器模塊一般只能有一個
  • Filters(過濾器模塊),此類模塊主要對其他處理器模塊輸出的內容進行修改操作,最后由nginx輸出
  • Proxies(代理器模塊),就是nginx的HTTP Upstream之類的模塊,這些模塊主要與后端一些服務比如fastcgi等操作互動,實作服務代理和負載均衡等功能

nginx模塊分為:核心模塊、事件模塊、標準Http模塊、可選Http模塊、郵件模塊、第三方模塊和補丁等

  • nginx基本模塊:所謂基本模塊,指的是nginx默認的功能模塊,它們提供的指令,允許你使用定義nginx基本功能的變數,在編譯時不能被禁用,包括:
    • 核心模塊:基本功能和指令,如行程管理和安全,常見的核心模塊指令,大部分是放置在組態檔的頂部
    • 事件模塊:在Nginx內配置網路使用的能力,常見的events(事件)模塊指令,大部分是放置在組態檔的頂部
    • 配置模塊:提供包含機制

具體的指令,可參考nginx的官方檔案

4.2 nginx的作業原理

nginx的模塊直接被編譯進nginx,因此屬于靜態編譯方式,

啟動nginx后,nginx的模塊被自動加載,與Apache不一樣,首先將模塊編譯為一個so檔案,然后在組態檔中指定是否進行加載,

在決議組態檔時,nginx的每個模塊都有可能去處理某個請求,但是同一個處理請求只能由一個模塊來完成,

nginx的行程架構:

啟動nginx時,會啟動一個Master行程,這個行程不處理任何客戶端的請求,主要用來產生worker執行緒,一個worker執行緒用來處理n個request

請添加圖片描述

Nginx主要由一個master主行程和多個worker行程

  • master主行程主要是管理worker行程,對網路事件進行收集和分發:

    • 接收來自外界的信號

    • 向各worker行程發送信號

    • 監控worker行程的運行狀態,當worker行程退出后(例外情況下),會自動重新啟動新的worker行程

  • nginx用一個獨立的worker行程來處理一個請求,一個worker行程可以處理多個請求:

  • 當一個worker行程在accept這個連接之后,就開始讀取請求,決議請求,處理請求,產生資料后,再回傳給客戶端,最后才斷開連接,

  • 一個請求,完全由worker行程來處理,而且只在一個worker行程中處理,采用這種方式的好處

    • 節省鎖帶來的開銷,對于每個worker行程來說,獨立的行程,不需要加鎖,所以省掉了鎖帶來的開銷,同時在編程以及問題查上時,也會方便很多

    • 獨立行程,減少風險,

    • 采用獨立的行程,可以讓互相之間不會影響,一個行程退出后,其它行程還在作業,服務不會中斷,master行程則很快重新啟動新的worker行程,

    • 在一次請求里無需行程切換

每個worker行程都有一個獨立的連接池,連接池的大小是worker_connections,這里的連接池里面保存的其實不是真實的連接,它只是一個worker_connections大小的一個ngx_connection_t結構的陣列,并且,nginx會通過一個鏈表free_connections來保存所有的空閑ngx_connection_t,每次獲取一個連接時,就從空閑連接鏈表中獲取一個,用完后,再放回空閑連接鏈表里面,一個nginx能建立的最大連接數,應該是worker_connections * worker_processes,當然,這里說的是最大連接數,對于HTTP請求本地資源來說,能夠支持的最大并發數量是worker_connections * worker_processes,而如果是HTTP作為反向代理來說,最大并發數量應該是worker_connections * worker_processes/2,因為作為反向代理服務器,每個并發會建立與客戶端的連接和與后端服務的連接,會占用兩個連接,

下圖展示了基本的WEB服務請求步驟

在這里插入圖片描述

網頁訪問流程
利用DNS協議進行域名決議 --> 建立tcp協議三次握手程序 --> 客戶端發出訪問網站相應頁面請求(發出http協議請求報文) --> 服務端發出相應訪問頁面的請求資訊(發出http) --> 斷開tcp協議四次揮手程序

5. Nginx的應用環境

  • Nginx 代理
    說到代理,首先我們要明確一個概念,所謂代理就是一個代表、一個渠道此時就涉及到兩個角色,一個是被代理角色 (A_),一個是目標角色 (B_),A_ 通過這個代理訪問 B_ 完成一些任務的程序稱為代理 (C_) 操作程序;比如客人去買雙鞋,這個店鋪就是 (C_),(A_) 就是廠家,(B_) 就是用戶,代理呢又分為正向代理和反向代理

  • 反向代理
    反向代理應該是Nginx做的最多的一件事了,什么是反向代理呢,以下是百度百科的說法:反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的連接請求,然后將請求轉發給內部網路上的服務器,并將從服務器上得到的結果回傳給internet上請求連接的客戶端,此時代理服務器對外就表現為一個反向代理服務器,簡單來說就是真實的服務器不能直接被外部網路訪問,所以需要一臺代理服務器,而代理服務器能被外部網路訪問的同時又跟真實服務器在同一個網路環境,當然也可能是同一臺服務器,埠不同而已,

如下圖:
在這里插入圖片描述

  • 負載均衡
    負載均衡也是Nginx常用的一個功能,簡單而言就是當有2臺或2臺以上服務器時,根據規則隨機的將請求分發到指定的服務器上處理,負載均衡配置一般都需要同時配置反向代理,通過反向代理跳轉到負載均衡,而Nginx目前支持自帶3種負載均衡策略,還有2種常用的第三方策略

?如下圖:
在這里插入圖片描述

  • WEB服務器
    Nginx本身也是一個靜態資源的服務器,當只有靜態資源的時候,就可以使用Nginx來做服務器,同時現在也很流行動靜分離,就可以通過Nginx來實作,首先看看Nginx做靜態資源服務器

  • 靜態分離
    動靜分離是讓動態網站里的動態網頁根據一定規則把不變的資源和經常變的資源區分開來,動靜資源做好了拆分以后,我們就可以根據靜態資源的特點將其做快取操作,這就是網站靜態化處理的核心思路,

如下圖:

在這里插入圖片描述

6. Nginx的安裝

環境說明

系統IP
centos8192.168.25.144
//關閉防火墻
[root@nginx ~]# systemctl disable --now firewalld
[root@nginx ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@nginx ~]# setenforce 0

//安裝依賴包
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@nginx ~]# yum -y groups mark install 'Development Tools'

//創建系統用戶Nginx
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx

//創建日志存放目錄
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx/

//下載Nginx原始碼包
[root@nginx ~]# cd /usr/local/src/
[root@nginx src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz


//編譯安裝
[root@nginx src]# tar xf nginx-1.20.1.tar.gz
[root@nginx src]# cd nginx-1.20.1
[root@nginx nginx-1.20.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log

[root@nginx nginx-1.20.1]# make  && make install

// 啟動nginx
[root@nginx ~]# /usr/local/nginx/sbin/nginx 
[root@Nginx ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process   
LISTEN   0        128              0.0.0.0:80            0.0.0.0:*               
LISTEN   0        128              0.0.0.0:22            0.0.0.0:*               
LISTEN   0        128                 [::]:22               [::]:*   

6.1 nginx安裝后配置

//配置環境變數
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# source /etc/profile.d/nginx.sh



//服務控制方式,使用nginx命令
    -t  //檢查組態檔語法
    -v  //輸出nginx的版本
    -c  //指定組態檔的路徑
    -s  //發送服務控制信號,可選值有{stop|quit|reopen|reload}


[root@nginx ~]# nginx -t    //檢查組態檔語法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


[root@nginx ~]# nginx -v    //查看nginx的版本
nginx version: nginx/1.20.1


[root@nginx ~]# nginx -V    // 查看更加詳細的資訊
nginx version: nginx/1.20.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-3) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log


[root@nginx ~]# nginx -h    //查看幫助
nginx version: nginx/1.20.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -e filename   : set error log file (default: /var/log/nginx/error.log)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file


// 修改組態檔的方法
[root@nginx conf]# cp nginx.conf /opt/
[root@nginx conf]# cp mime.types /opt/
[root@nginx opt]# ls
mime.types  nginx.conf
[root@nginx opt]# vim nginx.conf 
#user  nobody;
worker_processes  1;
[root@nginx ~]# nginx -s stop;nginx -c /opt/nginx.conf     //指定組態檔的路徑
root@Nginx ~]# ps -ef | grep nginx
root        1817       1  0 04:42 ?        00:00:00 nginx: master process nginx -c /opt/nginx.conf
nginx       1818    1817  0 04:42 ?        00:00:00 nginx: worker process
root        1821    1734  0 04:42 pts/0    00:00:00 grep --color=auto nginx




[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  4;    //修改nginx的worker行程數量,默認為1

[root@nginx ~]# nginx -s reload    //發送服務控制信號,重新加載組態檔
[root@nginx ~]# ps -ef | grep nginx
root        1725       1  0 04:10 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx       1769    1725  0 04:22 ?        00:00:00 nginx: worker process
nginx       1770    1725  0 04:22 ?        00:00:00 nginx: worker process
nginx       1771    1725  0 04:22 ?        00:00:00 nginx: worker process
nginx       1772    1725  0 04:22 ?        00:00:00 nginx: worker process
root        1779    1734  0 04:22 pts/0    00:00:00 grep --color=auto nginx


7 nginx的組態檔詳解

主組態檔:安裝路徑/conf/nginx.conf

  • 默認啟動nginx時,使用的組態檔是:安裝路徑/conf/nginx.conf檔案
  • 可以在啟動nginx時通過-c選項來指定要讀取的組態檔

nginx常見的組態檔及其作用

組態檔作用
nginx.confnginx的基本組態檔
mime.typesMIME型別關聯的擴展檔案
fastcgi.conf與fastcgi相關的配置
proxy.conf與proxy相關的配置
sites.conf配置nginx提供的網站,包括虛擬主機

7.1 nginx.conf配置詳解

nginx.conf的內容分為以下幾段

  • main配置段:全域配置段,其中main配置段中可能包含event配置段
  • event {}:定義event模型作業特性
  • http {}:定義http協議相關的配置

配置指令:要以分號結尾,語法格式如下

derective value1 [value2 ...];

支持使用變數

  • 內置變數:模塊會提供內建變數定義
  • 自定義變數:set var_name value

7.2 用于除錯、定位問題的配置引數

daemon {on|off}; //是否以守護行程方式運行nginx,除錯時應設定為off

//用法
Syntax:	daemon on | off;					#語法
Default: daemon on;	 						#默認值
Context:	main							#可以配置在那個欄位中

[root@Nginx ~]# vim /opt/nginx.conf 
#user  nobody;
worker_processes  1;
daemon off;

//重啟nginx
[root@Nginx ~]# nginx -s stop;nginx -c /opt/nginx.conf
[root@Nginx ~]# nginx -s stop
[root@Nginx ~]# nginx
//此時nginx將不會以守護行程的方式運行,會一直占據著前臺螢屏    

m aster_process {on|off}; //是否以master/worker模型來運行nginx,除錯時可以設定為off

master_process {on|off}; //是否以master/worker模型來運行nginx,除錯時可以設定為off

//用法
Syntax:	master_process on | off;
Default:  master_process on;
Context:  main

//nginx默認是以master/worker模型來運行
[root@nginx ~]# ps -ef | grep nginx
root        1777       1  0 08:01 ?        00:00:00 nginx: master process nginx
nginx       1778    1777  0 08:01 ?        00:00:00 nginx: worker process
root        1800    1779  0 08:02 pts/0    00:00:00 grep --color=auto nginx

[root@nginx ~]# vim /opt/nginx.conf 
#user  nobody;
worker_processes  1;
master_process off;


//重讀nginx組態檔
[root@nginx ~]# nginx -s stop;nginx -c /opt/nginx.conf 
[root@nginx ~]# ps -ef | grep nginx
root        1804       1  0 08:04 ?        00:00:00 nginx -c /opt/nginx.conf
root        1806    1779  0 08:04 pts/0    00:00:00 grep --color=auto nginx

error_log 位置 級別; //配置錯誤日志

//用法,在不同的欄位有不同的含義,比如在main欄位寫就會對整個nginx產生影響,在http欄位寫就會對某個網站產生影響
Syntax:	error_log file [level];
Default: error_log logs/error.log error;
Context:  main, http, mail, stream, server, location

[root@nginx ~]# vim /opt/nginx.conf 
#user  nobody;
worker_processes  1;

error_log  logs/error.log;    //沒有寫記錄日志的級別,表示默認記錄error級別的日志

//重讀nginx組態檔
[root@nginx ~]# vim /opt/nginx.conf 
[root@nginx ~]# nginx -s stop;nginx -c /opt/nginx.conf 
[root@nginx ~]# ls /usr/local/nginx/logs/
error.log  nginx.pid

//訪問一個不存在的資源
[root@nginx ~]# curl http://192.168.25.147/tt.txt
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@nginx ~]# tail -f /usr/local/nginx/logs/error.log
2021/10/25 09:24:45 [error] 1817#0: *1 open() "/usr/local/nginx/html/tt.txt" failed (2: No such file or directory), client: 192.168.200.144, server: localhost, request: "GET /tt.txt HTTP/1.1", host: "192.168.200.144"

error_log里的位置和級別能有以下可選項

位置含義
file記錄到某個檔案中
stdree記錄到標準錯誤中(不推薦)
syslog:server=address[,parameter=value]記錄到某臺日志服務器中
memory:size記錄到記憶體中(不推薦)
日志級別含義
debug若要使用debug級別,需要在編譯nginx時使用–with-debug選項
info一般資訊
notice不影響正常功能,需要注意的訊息
warn可能影響系統功能,需要提醒用戶的重要事件
error錯誤資訊(常用的)
crit緊急,比較嚴重的
alert必須馬上處理的
emerg會導致系統不可用的

7.3 正常運行必備的配置引數

user USERNAME [GROUPNAME];    //指定運行worker行程的用戶和組

//用法
Syntax:	user [group];       #語法
Default: user nobody nobody;    #默認值
Context: main                  #可以配置在那個欄位中
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user  nginx;					#建議手動指定用戶
worker_processes  1;

pid /path/to/pid_file;    //指定nginx守護行程的pid(守護行程),使用時需要把前面的注釋取消掉

//用法
Syntax:	pid file;
Default: pid logs/nginx.pid;
Context: main
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


worker_rlimit_nofile number;    //設定所有worker行程最大可以打開的檔案數,默認為1024,最大值為655350

//用法
Syntax:	worker_rlimit_nofile number;
Default:	1024
Context:	main

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes  1;
worker_rlimit_nofile 1024;


worker_rlimit_core size;    //指明所有worker行程所能夠使用的總體的最大核心檔案大小,保持默認即可

//用法
Syntax:	worker_rlimit_core size;
Default:	—
Context:	main

7.4 優化性能的配置引數

worker_processes n;    //啟動n個worker行程,這里的n為了避免背景關系切換,通常設定為cpu總核心數-1或等于總核心數
worker_cpu_affinity cpumask ...;    //將行程系結到某cpu中,避免頻繁重繪快取
//cpumask:使用8位二進制表示cpu核心,如:
    0000 0001   //第一顆cpu核心
    0000 0010   //第二顆cpu核心
    0000 0100   //第三顆cpu核心
    0000 1000   //第四顆cpu核心
    0001 0000   //第五顆cpu核心
    0010 0000   //第六顆cpu核心
    0100 0000   //第七顆cpu核心
    1000 0000   //第八顆cpu核心

示例:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user  nginx; 
worker_processes  3;   //設定cpu核心數
worker_rlimit_nofile 1024;
worker_cpu_affinity cpumask 0001 0010 0100;  //收到將行程系結到某cpu核心


timer_resolution interval;    //計時器決議度,降低此值,可減少gettimeofday()系統呼叫的次數
worker_priority number;    //指明worker行程的nice值

示例:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user  nginx;
worker_processes  2;
worker_rlimit_nofile 1024;
worker_cpu_affinity 0001 0010;
worker_priority -20;

[root@nginx ~]# nginx -s stop;nginx
[root@nginx ~]# ps -elf|grep nginx  //原先的80現在設定成了60
1 S root      109329       1  0  80   0 - 20408 -      16:36 ?        00:00:00 nginx: master process nginx
5 S nginx     109330  109329  0  60 -20 - 28556 do_epo 16:36 ?        00:00:00 nginx: worker process
5 S nginx     109331  109329  0  60 -20 - 28556 do_epo 16:36 ?        00:00:00 nginx: worker process
0 S root      109836    1605  0  80   0 -  3087 -      16:36 pts/0    00:00:00 grep --color=auto nginx



7.5 事件相關的配置:event{}段中的配置引數

accept_mutex {off|on};    //master調度用戶請求至各worker行程時使用的負載均衡鎖;on表示能讓多個worker輪流地、序列化地去回應新請求
lock_file file;    //accept_mutex用到的互斥鎖鎖檔案路徑
use [epoll | rtsig | select | poll];    //指明使用的事件模型,建議讓nginx自行選擇


worker_connections  number;    //每個行程能夠接受的最大連接數
示例:
user  nginx;
worker_processes  2;
worker_rlimit_nofile 1024;
worker_cpu_affinity 0001 0010;
worker_priority -20;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  10240; //再此設定每個行程最大連接數
}

7.6 網路連接相關的配置引數

keepalive_timeout number;    //長連接的超時時長,默認為65s
keepalive_requests number;    //在一個長連接上所能夠允許請求的最大資源數
keepalive_disable [msie6|safari|none];    //為指定型別的UserAgent禁用長連接
tcp_nodelay on|off;    //是否對長連接使用TCP_NODELAY選項,為了提升用戶體驗,通常設為on
client_header_timeout number;    //讀取http請求報文首部的超時時長
client_body_timeout number;    //讀取http請求報文body部分的超時時長
send_timeout number;    //發送回應報文的超時時長

7.7 fastcgi的相關配置引數

LNMP:php要啟用fpm模型
配置示例如下:

location ~ \.php$ {
  root html;
  fastcgi_pass 127.0.0.1:9000;      //定義反向代理
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  include fastcgi_params;
}

7.8 常需要進行調整的引數

  • worker_processes
  • worker_connections
  • worker_cpu_affinity
  • worker_priority

7.9 nginx作為web服務器時使用的配置:http{}段的配置引數

http{…}:配置http相關,由ngx_http_core_module模塊引入,nginx的HTTP配置主要包括四個區塊,結構如下:

7.9.1http 協議

http {//協議級別
  include mime.types;
  default_type application/octet-stream;
  keepalive_timeout 65;
  gzip on;
  upstream {//負載均衡配置
    ...
  }
  server {//服務器級別,每個server類似于httpd中的一個<VirtualHost>(也就是相當于一個網站)
    listen 80;
    server_name localhost;
    location / {//請求級別,類似于httpd中的<Location>,用于定義URL與本地檔案系統的映射關系
      root html;
      index index.html index.htm;
    }
  }
}

我們自己撰寫一個網站

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#gzip  on;
       server {   //我們自己撰寫的網站
        listen 88;
        server_name www.caiccc.com;
      
   location / {
            root html/test;
            index index.html;
        }
    }

  

    
    server {
        listen       80; 
        server_name  localhost; 

        #charset koi8-r;

[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# ls
50x.html  index.html
[root@nginx html]# mkdir test
[root@nginx html]# echo 'xiedezhenshuai!!!' > test/index.html
[root@nginx ~]# nginx -s stop;nginx
[root@nginx ~]# ss -antl
\State  Recv-Q Send-Q Local Address:Port  Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:80         0.0.0.0:*            
LISTEN 0      128          0.0.0.0:22         0.0.0.0:*            
LISTEN 0      128          0.0.0.0:88         0.0.0.0:*            
LISTEN 0      128             [::]:22            [::]:*            
[root@nginx ~]# 
 

原網頁
請添加圖片描述
我們增加埠后訪問網頁
請添加圖片描述

7.9.2 http{}段配置指令:

//listen:指定監聽的地址和埠
listen address[:port];			
listen port;

//server_name NAME`[...]; 后面可跟多個主機,名稱可使用正則運算式或通配符

當有多個server時,匹配順序如下:

1. 先做精確匹配檢查
2. 左側通配符匹配檢查,如*.example.com
3. 右側通配符匹配檢查,如web.*
4. 正則運算式匹配檢查,如~ ^.*\.example\.com$
5. default_server
  • root path; 設定資源路徑映射,用于指明請求的URL所對應的資源所在的檔案系統上的起始路徑
location / {
            root   html;
            index  index.html index.htm;
        }

  • alias path; 用于location配置段,定義路徑別名
[root@nginx ~]# mkdir -p /var/www/html
[root@nginx ~]# chown -R nginx.nginx /var/www/html/
[root@nginx ~]# echo "hahahhh" > /var/www/html/index.html
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf

server  {
        listen       80; 
        server_name  localhost; 

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            alias   /var/www/html/;   //這里路徑寫絕對路徑
            index  index.html index.htm;
        }
}

root path;和alias path;設定的區別
	root path方式設定location表示最終訪問的資源地址是root path指定的位置+location后面的URI
	alias方式設定表示最終訪問的資源地址是alias后面跟的路徑
	

請添加圖片描述

  • index file; 默認主頁面
 location / {
            root   html;
            index  index.html index.htm;    //網頁默認訪問主頁面
        }





server  {
        listen       80; 
        server_name  localhost; 

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
location / {
            root   html;
            index test  index.html index.htm;    //自定義網頁默認訪問頁面
        }
}

請添加圖片描述

7.9.3錯誤頁面

error_page code [...] [=code] URI | @name根據http回應狀態碼來指明特用的錯誤頁面,例如 error_page 404 /404_customed.html

[=code]:以指定的回應碼進行回應,而不是默認的原來的回應,默認表示以新資源的回應碼為其回應碼,例如 error_page 404 =200 /404_customed.html

[root@lamp ~]# vim /usr/local/nginx/conf/nginx.conf

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index index.php  index.html index.htm;
        }

        error_page  404            /404.html;      //狀態碼如果是404就用404.html頁面來回應

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        
[root@lamp ~]# cd /usr/local/nginx/html/
[root@lamp html]# ls
50x.html  index.html  index.php
[root@lamp html]# vi 404.html

<html>
<head>
<title>test page</title>
</head>
<body>
<a href="http://www.baidu.com">baidu</a>
</body>
</html>

[root@lamp html]# nginx -s stop;nginx

訪問一個不存在的頁面

請添加圖片描述

請添加圖片描述

  • log_format 定義日志格式
  • access_log logs 訪問日志
  • logs/access.log 訪問日志檔案
  • main 格式名稱
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  logs/access.log  main;

//注意:此處可用變數為nginx各模塊內建變數
[root@lamp ~]# vim /usr/local/nginx/conf/nginx.conf  
http {
    include       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  logs/access.log  main;

[root@nginx ~]# nginx -s reload     //去重新訪問一下nginx網站
[root@nginx ~]# tail -f /usr/local/nginx/logs/access.log
Last login: Wed Oct 27 14:23:46 2021 from 192.168.200.1
[root@nginx ~]# tail -f /usr/local/nginx/logs/access.log 
192.168.200.1(代表本機) -(代表本身) -(代表用戶) [27/Oct/2021:15:02:22 +0800](訪問時間) "GET / HTTP/1.1"(用get方法獲取根下面的資源,用的http1.1版本號) 304(狀態碼) 0 "-" (代表訪問網站時沒用跳轉)"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"(所使用的瀏覽器) "-"(是否是跳轉過來訪問的,不是用 - 代表,是就表示跳轉的路徑)

7.9.4平滑升級

  • 獲取現有的程式編譯的引數 -V
[root@nginx html]# nginx -V
nginx version: nginx/1.20.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-3) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@nginx html]# 

  • 獲取新版本的軟體包或功能包
[root@nginx ~]# ls
echo-nginx-module-master.zip
anaconda-ks.cfg            nginx-1.20.1
 echo-nginx-module-master   nginx-1.20.1.tar.gz
  • 將新功能或新版版進行編譯
[root@nginx ~]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master    //添加新的模塊

[root@nginx ~]# make
  • 備份原程式
[root@nginx nginx-1.20.1]# ls objs/
addon         nginx              ngx_auto_headers.h  src
autoconf.err  nginx.8            ngx_modules.c
Makefile      ngx_auto_config.h  ngx_modules.o

[root@nginx nginx-1.20.1]# ll objs/nginx /usr/local/nginx/sbin/nginx 
-rwxr-xr-x. 1 root root 6831056 10月 27 15:38 objs/nginx
-rwxr-xr-x. 1 root root 6308640 10月 25 11:35 /usr/local/nginx/sbin/nginx

[root@nginx nginx-1.20.1]# cp /usr/local/nginx/sbin/nginx  /opt/
[root@nginx nginx-1.20.1]# ls /opt/
nginx
[root@nginx nginx-1.20.1]# nginx -s stop;objs/nginx -c /usr/local/nginx/conf/nginx.conf

[root@nginx nginx-1.20.1]# ss -antl
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128             [::]:22             [::]:*            

[root@nginx nginx-1.20.1]# ps -ef|grep nginx
root      220850       1  0 15:40 ?        00:00:00 nginx: master process objs/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     220851  220850  0 15:40 ?        00:00:00 nginx: worker process
nginx     220852  220850  0 15:40 ?        00:00:00 nginx: worker process
root      221979    1503  0 15:41 pts/0    00:00:00 grep --color=auto nginx
[root@nginx nginx-1.20.1]# 

  • 替換原程式

檢測新程式有沒有問題

        location / {
            root  html;
            index  index.html index.htm;
        }
        location /test {      //自己查找跟下面的test,查找到后列印一句話
            echo "test";

        error_page  404 =200             /404.html;
       } 
[root@nginx ~]# ./nginx-1.20.1/objs/nginx -t    //用新檔案檢查語法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# 
[root@nginx ~]# ./nginx-1.20.1/objs/nginx -s reload

訪問測驗
[root@nginx html]# curl http://192.168.200.144
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>



[root@nginx html]# curl http://192.168.200.144/test
test

替換原程式

[root@nginx ~]# nginx -t   //組態檔修改后用原程式會報錯,新程式不會報錯
nginx: [emerg] unknown directive "echo" in /usr/local/nginx/conf/nginx.conf:51
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@nginx html]# 



 
[root@nginx ~]# cp nginx-1.20.1/objs/nginx /usr/local/nginx/sbin/
cp:是否覆寫'/usr/local/nginx/sbin/nginx'? y
[root@nginx ~]# 
[root@nginx ~]# 
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# 

再切換為原程式來執行

[root@nginx ~]# ./nginx-1.20.1/objs/nginx -s stop;nginx
[root@nginx ~]# ps -ef |grep nginx
root      342545       1  0 16:13 ?        00:00:00 nginx: master process nginx
nginx     342546  342545  0 16:13 ?        00:00:00 nginx: worker process
nginx     342547  342545  0 16:13 ?        00:00:00 nginx: worker process
root      343504   73085  0 16:13 pts/2    00:00:00 grep --color=auto nginx
[root@nginx ~]# 


[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root  html;
            index  index.html index.htm;
        }

        location /test {
            echo "test";
        }


        location = /test{
           echo "rush b";
        }


        error_page  404 =200             /404.html;


[root@nginx ~]# nginx -s reload

[root@nginx ~]# curl http://192.168.200.144/test
rush b
[root@nginx ~]# 

7.9.5 location區段

location區段,通過指定模式來與客戶端請求的URI相匹配

  • 功能:允許根據用戶請求的URI來匹配定義的各location,匹配到時,此請求將被相應的
    location配置塊中的配置所處理,例如做訪問控制等功能

  • 語法:location [ 修飾符 ] pattern {…}

常用修飾符說明:

=精確匹配
~正則運算式模式匹配,區分大小寫
~*正則運算式模式匹配,不區分大小寫
^~前綴匹配,類似于無修飾符的行為,也是以指定模塊開始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正則運算式
@定義命名location區段,這些區段客戶端不能訪問,只可以由內部產生的請求來訪問,如try_files或error_page等
//沒有修飾符表示必須以指定模式開始,此寫法屬于模糊匹配
server {
  server_name www.example.com;
  location /test {
    ......
  }
}

//以上寫法可以匹配到
http://www.example.com/test
http://www.example.com/test?p1=11&p2=22
http://www.example.com/test/111
// =: 表示必須與指定的模式精確匹配
server {
  server_name www.example.com;
  location = /test {
    ......
  }
}


//以上寫法可以匹配到
http://www.example.com/test
http://www.example.com/test?p1=11&p2=22

//以下內容無法匹配
http://www.example.com/test/
http://www.example.com/test/abcde

// ~: 表示指定的正則運算式要區分大小寫
server {
server {
  server_name www.example.com;
  location ~ ^/test$ {
  ......
  }
}


//以上寫法可以匹配
http://www.example.com/test
http://www.example.com/test?p1=11&p2=22

//以下內容無法匹配
http://www.example.com/test/
http://www.example.com/TEST
http://www.example.com/testfs

// ~*: 表示指定的正則運算式不區分大小寫
server {
  server_name www.example.com;
  location ~* ^/test$ {
    ......
  }
}


//以上寫法可以匹配到
http://www.example.com/test
http://www.example.com/test?p1=11&p2=22
http://www.example.com/TEST

//以下內容無法匹配
http://www.example.com/test/
http://www.example.com/testfs

~:類似于無修飾符的行為,也是以指定模式開始,不同的是,如果模式匹配,則停止搜索其他模式

查找順序和優先級:由高到底依次為

  • 帶有=的精確匹配優先
  • 正則運算式按照他們在組態檔中定義的順序
  • 帶有~^修飾符的,開頭匹配
  • 帶有~ 或~*修飾符的,如果正則運算式與URI匹配
  • 沒有修飾符的精確匹配

//優先級次序如下

( location = 路徑 ) --> ( location ^~ 路徑 ) --> ( location ~ 正則 ) --> ( location ~* 正則 ) --> ( location 路徑 )

7.9.6 訪問控制

一般用于location段,也可以用于server欄位,此時就對當前server欄位中的所有location生效,也可以用于http欄位,此時就對當前http欄位中的所有server欄位生效

//用法
allow:白名單,設定允許哪臺或哪些主機訪問
deny:黑名單,設定禁止哪臺或哪些主機訪問

location / {
            root html;
            index index.html;
            deny all;      //拒絕所以IP訪問
        }

7.9.7 基于用戶認證

//語法
auth_basic "歡迎資訊";
auth_basic_user_file "/path/to/user_auth_file"


//user_auth_file內容格式為:
username:password


//生成密碼檔案
[root@nginx ~]# yum -y install httpd-tools
[root@nginx ~]# htpasswd -c -m /usr/local/nginx/conf/.passwd tom
New password: 
Re-type new password: 
Adding password for user tom

[root@nginx ~]# cat /usr/local/nginx/conf/.passwd 
tom:$apr1$KeddkwXz$z0386sTtbbqR45SJV7M3z/
[root@nginx ~]# 


[root@nginx ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
            root  html;
            index  index.html index.htm;
        }

        location /test {
            root html;
            index index.html;
            auth_basic "xiedezhenshuai";
            auth_basic_user_file ".passwd";

        }                                                   

[root@nginx conf]# cd /usr/local/nginx/html/
[root@nginx html]# ls
404.html  50x.html  index.html
[root@nginx html]# mkdir test
[root@nginx html]# vi index.html 
[root@nginx html]# echo "hahhaha" > test/index.html
[root@nginx html]# nginx -s reload

請添加圖片描述
請添加圖片描述

7.9.8 https配置

生成私鑰,生成證書簽署請求并獲得證書,然后在nginx.conf中配置如下內容:
生成證書

//創建密鑰存放目錄
[root@nginx ~]# mkdir -p /etc/pki/CA
[root@nginx ~]# cd /etc/pki/CA
[root@nginx CA]# mkdir private

//生成私鑰
[root@nginx CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)     //生成密鑰,括號必須要
Generating RSA private key, 2048 bit long modulus (2 primes)
.....+++++
........................+++++
e is 65537 (0x010001)
[root@nginx CA]# ls private/
cakey.pem

//CA生成自簽名證書
[root@nginx CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code ) [XX]:CN    //國家的名字,簡寫
State or Province Name (full name) []:HB    //省份的名字
Locality Name (eg, city) [Default City]:WH    //城市的名字
Organization Name (eg, company) [Default Company Ltd]:runtime        //公司的域名
Organizational Unit Name (eg, section) []:runtime     //單位的名字
Common Name (eg, your name or your server's hostname) []:www.aabc.com    //主機的名字
Email Address []:1@34.com    //郵箱

//在CA目錄下創建certs newcerts crl目錄,并生成序列號
[root@nginx CA]# ls
cacert.pem  private

[root@nginx CA]# mkdir certs newcerts crl
[root@nginx CA]# ls
cacert.pem  certs  crl  newcerts  private

[root@nginx CA]# cd /usr/local/nginx/conf/ssl
[root@nginx CA]# touch index.txt && echo 01 > serial
[root@nginx CA]# ls
cacert.pem  certs  crl  index.txt  newcerts  private  serial

//創建證書存放目錄
[root@nginx ~]# mkdir /usr/local/nginx/conf/ssl

//生成密鑰
[root@nginx ssl]# (umask 077;openssl genrsa -out nginx.key 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
......................................+++++
.+++++
e is 65537 (0x010001)
[root@nginx ssl]# ls
nginx.key

// 生成證書簽署請求
[root@nginx ssl]# openssl req -new -key nginx.key -days 365 -out nginx.csr
Ignoring -days; not generating a certificate
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CH        
State or Province Name (full name) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:runtime
Organizational Unit Name (eg, section) []:runtime
Common Name (eg, your name or your server's hostname) []:www.aabc.com
Email Address []:1@34.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:    //是否需要給證書設定密碼,可以直接回車跳過
An optional company name []:

[root@nginx ssl]# openssl ca -in nginx.csr -out nginx.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Oct 27 10:21:49 2021 GMT
            Not After : Oct 27 10:21:49 2022 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = HB
            organizationName          = runtime
            organizationalUnitName    = runtime
            commonName                = www.aabc.com
            emailAddress              = 1@34.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                89:36:99:05:31:41:2C:86:11:00:22:E4:B9:68:7E:68:44:6F:16:9D
            X509v3 Authority Key Identifier: 
                keyid:E5:88:F3:CB:29:F4:DC:17:15:70:9C:32:81:FA:89:AA:0F:E7:B9:AF

Certificate is to be certified until Oct 27 10:21:49 2022 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

[root@nginx ssl]# ls
nginx.crt  nginx.csr  nginx.key

//證書生成完畢

[root@nginx ssl]#  vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       443 ssl;
        server_name  www.aabc.com;

        ssl_certificate      ssl/nginx.crt;        //指定證書位置
        ssl_certificate_key  ssl/nginx.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

}
[root@nginx ssl]# nginx -s reload

請添加圖片描述

請添加圖片描述

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/339100.html

標籤:其他

上一篇:Java版分布式微服務云開發架構 Spring Cloud+Spring Boot+Mybatis

下一篇:單體架構,集群架構,SOA架構,分布式微服務架構

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 面試突擊第一季,第二季,第三季

    第一季必考 https://www.bilibili.com/video/BV1FE411y79Y?from=search&seid=15921726601957489746 第二季分布式 https://www.bilibili.com/video/BV13f4y127ee/?spm_id_fro ......

    uj5u.com 2020-09-10 05:35:24 more
  • 第三單元作業總結

    1.前言 這應該是本學期最后一次寫作業總結了吧。總體來說,對作業的節奏也差不多掌握了,作業做起來的效率也更高了。雖然和之前的作業一樣,作業中都要用到新的知識,但是相比之前,更加懂得了如何利用工具以及資料。雖然之間卡過殼,但總體而言,這幾次作業還算完成的比較好。 2.作業程序總結 相比前兩個單元,此單 ......

    uj5u.com 2020-09-10 05:35:41 more
  • 北航OO(2020)第四單元博客作業暨課程總結博客

    北航OO(2020)第四單元博客作業暨課程總結博客 本單元作業的架構設計 在本單元中,由于UML圖具有比較清晰的樹形結構,因此我對其中需要進行查詢操作的元素進行了包裝,在樹的父節點中存盤所有孩子的參考。考慮到性能問題,我采用了快取機制,一次查詢后盡可能快取已經遍歷過的資訊,以減少遍歷次數。 本單元我 ......

    uj5u.com 2020-09-10 05:35:48 more
  • BUAA_OO_第四單元

    一、UML決議器設計 ? 先看下題目:第四單元實作一個基于JDK 8帶有效性檢查的UML(Unified Modeling Language)類圖,順序圖,狀態圖分析器 MyUmlInteraction,實際上我們要建立一個有向圖模型,UML中的物件(元素)可能與同級元素連接,也可與低級元素相連形成 ......

    uj5u.com 2020-09-10 05:35:54 more
  • 6.1邏輯運算子

    邏輯運算子 1. && 短路與 運算式1 && 運算式2 01.運算式1為true并且運算式2也為true 整體回傳為true 02.運算式1為false,將不會執行運算式2 整體回傳為false 03.只要有一個運算式為false 整體回傳為false 2. || 短路或 運算式1 || 運算式2 ......

    uj5u.com 2020-09-10 05:35:56 more
  • BUAAOO 第四單元 & 課程總結

    1. 第四單元:StarUml檔案決議 本單元采用了圖模型決議UML。 UML檔案可以抽象為圖、子圖、邊的邏輯結構。 在實作中,圖的節點包括類、介面、屬性,子圖包括狀態圖、順序圖等。 采用了三次遍歷UML元素的方法建圖,第一遍遍歷建點,第二、三次遍歷設定屬性、連邊,實作圖物件的初始化。這里借鑒了一些 ......

    uj5u.com 2020-09-10 05:36:06 more
  • 談談我對C# 多型的理解

    面向物件三要素:封裝、繼承、多型。 封裝和繼承,這兩個比較好理解,但要理解多型的話,可就稍微有點難度了。今天,我們就來講講多型的理解。 我們應該經常會看到面試題目:請談談對多型的理解。 其實呢,多型非常簡單,就一句話:呼叫同一種方法產生了不同的結果。 具體實作方式有三種。 一、多載 多載很簡單。 p ......

    uj5u.com 2020-09-10 05:36:09 more
  • Python 資料驅動工具:DDT

    背景 python 的unittest 沒有自帶資料驅動功能。 所以如果使用unittest,同時又想使用資料驅動,那么就可以使用DDT來完成。 DDT是 “Data-Driven Tests”的縮寫。 資料:http://ddt.readthedocs.io/en/latest/ 使用方法 dd. ......

    uj5u.com 2020-09-10 05:36:13 more
  • Python里面的xlrd模塊詳解

    那我就一下面積個問題對xlrd模塊進行學習一下: 1.什么是xlrd模塊? 2.為什么使用xlrd模塊? 3.怎樣使用xlrd模塊? 1.什么是xlrd模塊? ?python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫。 今天就先來說一下xl ......

    uj5u.com 2020-09-10 05:36:28 more
  • 當我們創建HashMap時,底層到底做了什么?

    jdk1.7中的底層實作程序(底層基于陣列+鏈表) 在我們new HashMap()時,底層創建了默認長度為16的一維陣列Entry[ ] table。當我們呼叫map.put(key1,value1)方法向HashMap里添加資料的時候: 首先,呼叫key1所在類的hashCode()計算key1 ......

    uj5u.com 2020-09-10 05:36:38 more
最新发布
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:20:47 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:20:25 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:20:17 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:20:10 more
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:19:44 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:19:07 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:18:57 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:18:49 more
  • 05單件模式

    #經典的單件模式 public class Singleton { private static Singleton uniqueInstance; //一個靜態變數持有Singleton類的唯一實體。 // 其他有用的實體變數寫在這里 //構造器宣告為私有,只有Singleton可以實體化這個類! ......

    uj5u.com 2023-04-19 08:42:51 more
  • 【架構與設計】常見微服務分層架構的區別和落地實踐

    軟體工程的方方面面都遵循一個最基本的道理:沒有銀彈,架構分層模型更是如此,每一種都有各自優缺點,所以請根據不同的業務場景,并遵循簡單、可演進這兩個重要的架構原則選擇合適的架構分層模型即可。 ......

    uj5u.com 2023-04-19 08:42:41 more