天天看點

Nginx配置檔案詳解

全局配置

user  nginx nginx;                            # 啟動nginx工作程序的使用者和使用者組
daemon on;                                    # 是否為守護程序方式運作,預設為on, on|off

worker_processes  auto;                       # worker 程序數量,一般為cpu核心數。 num|auto
worker_cpu_affinity 0;                        # cpu親和力,綁定woker程序在指定的cpu核心上運作。

error_log  logs/error.log  error;             # error 日志存儲路徑及級别,[debug | info | notice | warn | error | crit  | alert | emerg]
pid        run/nginx.pid;                     # pid 檔案存儲路徑

worker_priority 0;                            # 程序優先級 -20~19
worker_rlimit_nofile 65535;                   # 所有woker程序打開的檔案書上限。推薦與系統最大打開檔案數一緻 ulimit -n檢視
master_process on;                            # 是否開啟 nginx master - worker 工作模式,預設為on, on | off      

events 語句塊

events {
    
    worker_connections  65535;              # 設定單個工作程序的最大并發連接配接數
    use epoll;                              # 使用epoll事件驅動 支援事件驅動如: select、poll、epoll
    accept_mutex on;                        # on 為同一時刻一個請求輪流由work程序處理,而防止被同時喚醒所有worker,避免多個睡眠程序被喚醒的設定,預設為off,新請求會喚醒所有worker程序,此過程也稱為"驚群",是以nginx剛安裝完以後要進行适當的優化。建議設定為on
    multi_accept on;                        # on時Nginx伺服器的每個工作程序可以同時接受多個新的網絡連接配接,此指令預設為off,即預設為一個工作程序隻能一次接受一個新的網絡連接配接,打開後幾個同時接受多個。建議設定為on
}      

 http配置塊

通用配置

include       mime.types;                #在響應封包中将指定的檔案擴充名映射至MIME對應的類型
    default_type  application/octet-stream;  #除mime.types中的類型外,指定其它檔案的預設MIME類型,浏覽器一般會提示下載下傳      

響應封包server首部

charset utf-8;             # 設定編碼格式 也可設定 off
    server_tokens off;         # 是否顯示版本      

優化參數

server_names_hash_bucket_size 128;          # 儲存域名的 hash 表大小
    client_header_buffer_size 32k;              # 設定讀取用戶端請求頭的緩沖區大小。如果請求行或請求頭字段不适合此緩沖區,則配置設定由large_client_header_buffers指令配置的更大緩沖區 。
    large_client_header_buffers 4 32k;          # 設定最大number和size用于讀取大的客戶請求頭緩沖器。  
    client_max_body_size 64m;                   # 設定允許用戶端上傳單個檔案的最大值,預設值為1m,上傳檔案超過此值會
出413錯誤
    client_body_buffer_size 64k;                # 用于接收每個用戶端請求封包的body部分的緩沖區大小;預設16k;超出此大小時,其将被暫存到磁盤上的由client_body_temp_path指令所定義的位置
    client_body_temp_path  /tmp/nginx/client_temp 1 2 2;
    sendfile       on;                          # sendfile系統調用在兩個檔案描述符之間直接傳遞資料(完全在核心中操作),進而避免了資料在核心緩沖區和使用者緩沖區之間的拷貝,操作效率很高,被稱之為零拷貝。
    tcp_nopush     on;                          # 在開啟了sendfile的情況下,合并請求後統一發送給用戶端,必須開啟
sendfile
    tcp_nodelay    on;                          # 在開啟了keepalived模式下的連接配接是否啟用TCP_NODELAY選項,當為off時,延遲0.2s發送,預設On時,不延遲發送,立即發送使用者響應封包。
    #keepalive_timeout  0;
    keepalive_timeout  120 60;                  # 設定會話保持時間,第二個值為響應首部:keepAlived:timeout=65,可以和第一個值不同      

#nginx可以緩存以下三種資訊:

    (1) 檔案中繼資料:檔案的描述符、檔案大小和最近一次的修改時間

    (2) 打開的目錄結構

    (3) 沒有找到的或者沒有權限通路的檔案的相關資訊

max=N:        #可緩存的緩存項上限數量;達到上限後會使用LRU(Least recently used,最近最少使用)算法實作管理

inactive=time:#緩存項的非活動時長,在此處指定的時長内未被命中的或命中的次數少于

     open_file_cache max=10000 inactive=60s;      # 最大緩存10000個檔案,非活動資料逾時時長60s

     open_file_cache_valid 60s;                   # 緩存項有效性的檢查驗證頻率,預設值為60s

     open_file_cache_min_uses 3;                  # 60秒内至少被命中通路3次才被标記為活動資料

     open_file_cache_errors on;                   # 是否緩存查找時發生錯誤的檔案一類的資訊,預設值為off

Sendfile簡述

read/write

在傳統的檔案傳輸方式(read、write/send方式),具體流程細節如下:

調用read函數,檔案資料拷貝到核心緩沖區

read函數傳回,資料從核心緩沖區拷貝到使用者緩沖區

調用write/send函數,将資料從使用者緩沖區拷貝到核心socket緩沖區

資料從核心socket緩沖區拷貝到協定引擎中

在這個過程當中,檔案資料實際上是經過了四次拷貝操作:

硬碟—>核心緩沖區—>使用者緩沖區—>核心socket緩沖區—>協定引擎

Nginx配置檔案詳解

sendfile系統調用則提供了一種減少拷貝次數,提升檔案傳輸性能的方法。

注意: Nginx 是作為一個反向代理來使用的時候,SENDFILE 則沒什麼用了,因為 Nginx 是反向代理的時候。 in_fd 就不是檔案句柄而是 socket,此時就不符合 sendfile 函數的參數要求了。

sendfile系統調用利用DMA(直接存儲器通路:外部裝置不通過CPU而直接與系統記憶體交換資料的接口技術)将資料拷貝到核心緩沖區,之後資料被拷貝到與socket相關的核心緩沖區。這裡沒有 使用者态和核心态 之間的切換,在核心中直接完成了從一個 buffer 到另一個 buffer 的拷貝

DMA擎将資料從核心socket緩沖區拷貝到協定引擎中

這裡沒有使用者态和核心态之間的切換,也沒有核心緩沖區和使用者緩沖區之間的拷貝,大大提升了傳輸性能。

注意:sendfile系統調用是一種檔案傳輸的系統調用和kernel系統調用關系不大。

這個過程資料經曆的拷貝操作如下:

硬碟—>核心緩沖區—>核心socket緩沖區—>協定引擎

Nginx配置檔案詳解

帶有DMA收集拷貝功能的sendfile

對于帶有DMA收集拷貝功能的sendfile系統調用,還可以再減少一次核心緩沖區之間的拷貝。

具體流程如下:dfile系統調用利用DMA引擎将檔案資料拷貝到核心緩沖區,之後,将帶有檔案位置和長度資訊的緩沖區描述符添加到核心socket緩沖區中引擎會将資料直接從核心緩沖區拷貝到協定引擎中這個過程資料經曆的拷貝操作如下: 硬碟—>核心緩沖區—>協定引擎

 gzip壓縮

gzip on;                            #  啟用或禁用gzip壓縮,預設關閉
    gzip_buffers 16 8k;                 # 指定Nginx服務需要向伺服器申請的緩存空間的個數和大小,平台不同,預設:32 4k或者16 8k;
    gzip_comp_level 6;                  # 壓縮比由低到高從1到9,預設為1
    gzip_http_version 1.1;              # 啟用壓縮功能時,協定的最小版本,預設HTTP/1.1
    gzip_min_length 256;                # gzip壓縮的最小檔案,小于設定值的檔案将不會壓縮
    gzip_proxied any;                   # 
    gzip_vary on;                        # 如果啟用壓縮,是否在響應封包首部插入“Vary: Accept-Encoding”,一般建議打開
    gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
      text/javascript application/javascript application/x-javascript
      text/x-json application/json application/x-web-app-manifest+json
      text/css text/plain text/x-component
      font/opentype application/x-font-ttf application/vnd.ms-fontobject
      image/x-icon;                      # 指明僅對哪些類型的資源執行壓縮操作;預設為gzip_types text/html,不用顯示指定,否則出錯
    gzip_disable "MSIE [1-6]\.(?!.*SV1)"; # 禁用IE6 gzip功能      

gzip_proxied 字段

根據請求和響應啟用或禁用對代理請求的響應進行 gzip 壓縮。請求被代理的事實由“Via”請求頭字段的存在決定。該指令接受多個參數:

  • off

禁用所有代理請求的壓縮,忽略其他參數;
  • expired

如果響應頭包含具有禁用緩存值的“Expires”字段,則啟用壓縮;
  • no-cache

如果響應頭包含帶有“ 

no-cache

”參數的“Cache-Control”字段,則啟用壓縮;
  • no-store

no-store

  • private

private

  • no_last_modified

如果響應頭不包含“Last-Modified”字段,則啟用壓縮;
  • no_etag

如果響應頭不包含“ETag”字段,則啟用壓縮;
  • auth

如果請求标頭包含“授權”字段,則啟用壓縮;
  • any

        為所有代理請求啟用壓縮。

日志配置

log_format main escape=json '{"@timestamp":"$time_iso8601",'          # 時間配置
                       '"server_addr":"$server_addr",'                   # 伺服器位址
                       '"remote_addr":"$remote_addr",'                   # 用戶端位址公網IP
                       '"scheme":"$scheme",'                             # 協定
                       '"request_method":"$request_method",'             # 請求方法
                       '"request_uri": "$request_uri",'                  # uri
                       '"request_length": "$request_length",'            # 請求長度
                       '"uri": "$uri", '                                 # 請求rui全路徑 rui + file
                       '"request_time":$request_time,'                   # 耗時
                       '"body_bytes_sent":$body_bytes_sent,'             # body 發送自截長度
                       '"bytes_sent":$bytes_sent,'                       # 發送位元組長度
                       '"status":"$status",'                             # 狀态碼
                       '"upstream_time":"$upstream_response_time",''"upstream_host":"$upstream_addr",'
                       '"upstream_status":"$upstream_status",'
                       '"host":"$host",'                                 # 伺服器位址
                       '"http_referer":"$http_referer",'                 # 引用
                       '"http_user_agent":"$http_user_agent"'            #請求用戶端 
                       '}';


    access_log  logs/access.log  main;                                    # 全局日志配置      

server語句塊通用指令

server {
    listen 80;                                    # 監聽端口
    server_name _;                                # 主機名
    access_log logs/devops_access.log main;       # 通路日志
    error_log logs/devops_error.log;              # 錯誤日志
    root /data/nginx/html/devops;                 # 主目錄
    index index.html index.htm index.php;         # 首頁檔案
    error_page 404 /404.html;                     # 404 頁面
    error_page 502 /502.html;                     # 502 頁面
}      

root & alias

location /test {
        root /data/nginx/html/devops/;
    }
    location /ceshi {
        alias  /data/nginx/html/devops/;
    }
# 實際通路磁盤路徑如下
curl 10.211.55.106/test/index.html
磁盤路徑: /data/nginx/html/devops/test/index.html


curl 10.211.55.106/ceshi/index.html
磁盤徑路: /data/nginx/html/devops/index.html      

localtion指令

通過指定模式來與用戶端請求的URI相比對

#文法規則:

location [ = | ~ | ~* | ^~ ] uri { ... }

  • =   #用于标準uri前,需要請求字串與uri精确比對,大小敏感,如果比對成功就停止向下比對并立即處理請求
  • ^~  #用于标準uri前,表示包含正規表達式,并且比對以指定的正規表達式開頭,對URI的最左邊部分做比對檢查,不區分字元大小寫
  • ~   #用于标準uri前,表示包含正規表達式,并且區分大小寫
  • ~*  #用于标準uri前,表示包含正規表達式,并且不區分大寫不帶符号 #比對起始于此uri的所有的uri
  • \   #用于标準uri前,表示包含正規表達式并且轉義字元。可以将 . * ?等轉義為普通符号
  • #比對優先級從高到低:
    • =, ^~, ~/~*, 不帶符号
location = /log.jpg {
        root /data/nginx/devops/;
    }
    location ~ /A.?\.jpg {
         root /data/nginx/devops/;
    }

    location ~* /A.?\.jpg {
        root /data/nginx/devops/;
    }
    location ^~ /images {
        root /data/nginx/devops/;
    }
    location /api {
        alias /data/nginx/api;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
    location /.well-known {
      allow all;
    }      

四層通路控制

location = /login/ {
   root /data/nginx/html/pc;
   allow 10.0.0.0/24;
   deny all;
 }
location /about {
   alias /data/nginx/html/pc;
   index index.html;
   deny  192.168.1.1;
   allow 192.168.1.0/24;
   allow 10.1.1.0/16;
   allow 2001:0db8::/32;
   deny all;  #按先小範圍到大範圍排序
 }      

賬戶認證

#CentOS安裝包
[root@centos8 ~]#yum -y install httpd-tools
#Ubuntu安裝包
root@ops106:~# apt -y install apache2-utils

root@ops106:~#  htpasswd -cb /usr/local/nginx/conf/.htpassword devops 123456
Adding password for user devops
    location = /login/ {
       root /data/nginx/html/devops/;
       index index.html;
       auth_basic           "login password";
       auth_basic_user_file  /usr/local/nginx/conf/.htpasswd;
    }      

作為下載下傳伺服器

location /download {
   autoindex on;               #自動索引功能
   autoindex_exact_size   on;  #計算檔案确切大小(機關bytes),此為預設值,off隻顯示大概大小(機關kb、mb、gb)  
   autoindex_localtime   on;   #on表示顯示本機時間而非GMT(格林威治)時間,默為為off顯示GMT時間
   limit_rate 1024k;           #限速,預設不限速
   root /data/nginx/html/devops/;
 }      

狀态頁

基于nginx 子產品 ngx_http_stub_status_module 實作,在編譯安裝nginx的時候需要添加編譯參數 --with-http_stub_status_module,否則配置完成之後監測會是提示文法錯誤

location /nginx_status {
   stub_status;
   auth_basic           "auth login";
   auth_basic_user_file /apps/nginx/conf/.htpasswd;
   allow 192.168.0.0/16;
   allow 127.0.0.1;
   deny all;
 }


#狀态頁用于輸出nginx的基本狀态資訊:
#輸出資訊示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三個數字分别對應accepts,handled,requests三個值
Reading: 6 Writing: 179 Waiting: 106
Active connections:              #目前處于活動狀态的用戶端連接配接數,包括連接配接等待空閑連接配接數
=reading+writing+waiting
accepts:                         #統計總值,Nginx自啟動後已經接受的用戶端請求連接配接的總數。
handled:                         #統計總值,Nginx自啟動後已經處理完成的用戶端請求連接配接總數,通常等于accepts,除非有因worker_connections限制等被拒絕的連接配接
requests:                        #統計總值,Nginx自啟動後用戶端發來的總的請求數。
Reading:                         #目前狀态,正在讀取用戶端請求封包首部的連接配接的連接配接數,數值越大,說明排隊現象嚴重,性能不足
Writing:                         #目前狀态,正在向用戶端發送響應封包過程中的連接配接數,數值越大,說明通路量很大
Waiting:                         #目前狀态,正在等待用戶端送出請求的空閑連接配接數,開啟 keep-alive的情況下,這個值等于
active – (reading+writing)      

https功能

自簽名證書

Nginx配置檔案詳解
Nginx配置檔案詳解
1 # 自簽名CA憑證
  2 root@ops106:/usr/local/nginx/certs# openssl req -newkey rsa:4096 -nodes -sha256 -keyout  ca.key -x509 -days 3650 -out ca.crt
  3 Generating a RSA private key
  4 ............................................................................++++
  5 ...........................++++
  6 writing new private key to 'ca.key'
  7 -----
  8 You are about to be asked to enter information that will be incorporated
  9 into your certificate request.
 10 What you are about to enter is what is called a Distinguished Name or a DN.
 11 There are quite a few fields but you can leave some blank
 12 For some fields there will be a default value,
 13 If you enter '.', the field will be left blank.
 14 -----
 15 Country Name (2 letter code) [AU]:CN
 16 State or Province Name (full name) [Some-State]:BeiJing
 17 Locality Name (eg, city) []:BeiJing
 18 Organization Name (eg, company) [Internet Widgits Pty Ltd]:SuperOps
 19 Organizational Unit Name (eg, section) []:Ops
 20 Common Name (e.g. server FQDN or YOUR name) []:ca.devops.com
 21 Email Address []:[email protected]
 22 root@ops106:/usr/local/nginx/certs# 
 23 root@ops106:/usr/local/nginx/certs# ls
 24 ca.crt  ca.key
 25 
 26 
 27 # 自制 Key 和 Csr 檔案
 28 root@ops106:/usr/local/nginx/certs# openssl req -newkey rsa:4096 -nodes -sha256 -keyout www.devops.com.key -out www.devops.com.csr
 29 Generating a RSA private key
 30 ..............................++++
 31 .....................................................................................................................................................................++++
 32 writing new private key to 'www.devops.com.key'
 33 -----
 34 You are about to be asked to enter information that will be incorporated
 35 into your certificate request.
 36 What you are about to enter is what is called a Distinguished Name or a DN.
 37 There are quite a few fields but you can leave some blank
 38 For some fields there will be a default value,
 39 If you enter '.', the field will be left blank.
 40 -----
 41 Country Name (2 letter code) [AU]:CN
 42 State or Province Name (full name) [Some-State]:BeiJing
 43 Locality Name (eg, city) []:BeiJing
 44 Organization Name (eg, company) [Internet Widgits Pty Ltd]:SuperOps
 45 Organizational Unit Name (eg, section) []:SuperOps
 46 Common Name (e.g. server FQDN or YOUR name) []:www.devops.com
 47 Email Address []:[email protected]
 48 
 49 Please enter the following 'extra' attributes
 50 to be sent with your certificate request
 51 A challenge password []:
 52 An optional company name []:
 53 root@ops106:/usr/local/nginx/certs# ls
 54 ca.crt  ca.key  www.devops.com.csr  www.devops.com.key
 55 
 56 
 57 # 簽發證書
 58 root@ops106:/usr/local/nginx/certs#  openssl x509 -req -days 3650 -in www.devops.com.csr  -CA ca.crt -CAkey ca.key  -CAcreateserial -out www.devops.com.crt
 59 Signature ok
 60 subject=C = CN, ST = BeiJing, L = BeiJing, O = SuperOps, OU = SuperOps, CN = www.devops.com, emailAddress = [email protected]
 61 Getting CA Private Key
 62 root@ops106:/usr/local/nginx/certs# ls
 63 ca.crt  ca.key  ca.srl  www.devops.com.crt  www.devops.com.csr  www.devops.com.key
 64 
 65 
 66 # 驗證證書内容
 67 root@ops106:/usr/local/nginx/certs# openssl x509 -in www.devops.com.crt -noout -text
 68 Certificate:
 69     Data:
 70         Version: 1 (0x0)
 71         Serial Number:
 72             1b:a6:3e:33:cd:72:fb:04:d0:8c:ac:bf:8d:5b:78:a7:98:79:ca:27
 73         Signature Algorithm: sha256WithRSAEncryption
 74         Issuer: C = CN, ST = BeiJing, L = BeiJing, O = SuperOps, OU = Ops, CN = ca.devops.com, emailAddress = [email protected]
 75         Validity
 76             Not Before: Dec  4 17:16:28 2021 GMT
 77             Not After : Dec  2 17:16:28 2031 GMT
 78         Subject: C = CN, ST = BeiJing, L = BeiJing, O = SuperOps, OU = SuperOps, CN = www.devops.com, emailAddress = [email protected]
 79         Subject Public Key Info:
 80             Public Key Algorithm: rsaEncryption
 81                 RSA Public-Key: (4096 bit)
 82                 Modulus:
 83                     00:d1:1f:f5:ed:1d:94:64:61:86:a5:31:5d:d6:23:
 84                     c3:0d:44:52:4c:ce:e4:fe:e1:84:3e:42:8b:3d:ae:
 85                     94:8a:b9:e6:43:63:1a:44:f0:5f:65:09:51:05:8f:
 86                     f4:66:db:52:c8:75:2c:69:50:dd:48:99:21:7d:5d:
 87                     2d:06:cf:90:2a:f9:2e:65:5b:4e:8b:b7:70:45:78:
 88                     91:4e:0c:96:3e:79:e6:0b:30:24:ff:ff:ea:5d:5c:
 89                     4d:f9:62:b6:0e:d8:5d:49:a4:0e:79:44:94:e5:d7:
 90                     52:3d:85:e7:62:2e:22:ca:03:64:40:11:33:5b:0a:
 91                     fe:33:a0:1e:85:d8:a1:a1:00:49:ed:9c:d5:4d:f5:
 92                     b1:c7:0d:ce:4d:2e:8b:30:73:b4:fb:55:0b:d4:1a:
 93                     8f:01:3c:4c:eb:33:10:c6:df:56:7e:b8:fd:b6:1e:
 94                     25:a9:9f:64:03:cb:86:9a:1c:f1:43:16:3d:18:bd:
 95                     fa:00:20:62:46:7d:20:c1:11:32:7b:24:16:bc:cc:
 96                     16:4f:29:6f:0a:66:a6:fc:45:89:8c:b3:fc:b9:3c:
 97                     e5:bb:ea:a3:ae:18:f1:29:c6:39:20:1d:2f:44:2b:
 98                     2b:3f:19:3d:5c:8d:5c:ad:b1:d3:d4:98:3a:7a:7b:
 99                     69:d1:72:32:31:80:01:8a:ee:55:60:24:60:5e:d7:
100                     4e:87:da:91:cf:a6:b6:64:03:1a:fa:2d:b7:be:d9:
101                     0d:18:d9:37:b1:e0:5c:52:e0:0d:a6:f2:51:9c:93:
102                     ad:f2:80:7d:d9:d9:31:47:21:57:4e:52:a8:b0:11:
103                     d2:d3:a2:5d:92:4b:5a:ca:35:b2:4e:16:6e:e7:76:
104                     e7:da:0b:ca:e3:31:6f:09:1e:aa:ae:3d:0f:63:72:
105                     c1:2e:2b:8d:b9:13:0d:77:ae:ea:d7:4d:da:e4:61:
106                     58:d4:31:84:fa:3e:43:3c:ef:ea:b1:d0:2b:37:d4:
107                     4e:19:ae:59:81:bf:9f:a1:5c:53:c3:5f:8e:0c:16:
108                     47:9e:d2:8f:a9:c4:73:54:23:5c:dd:f1:67:46:5e:
109                     fd:ac:91:85:0a:ff:af:d3:79:a5:d4:a6:7e:72:af:
110                     4c:f1:71:97:42:e6:ad:cf:89:fa:6e:b1:ea:f9:05:
111                     3c:fb:92:c3:72:a5:fb:60:1b:0f:f1:1a:93:b5:32:
112                     d2:74:4c:01:ff:2c:47:cb:c4:8b:b2:45:5b:c6:09:
113                     0b:06:f8:0d:06:fc:2c:10:06:2f:0f:92:ef:ad:cc:
114                     9d:ad:23:7e:60:bc:d6:0d:3f:5c:f8:9b:72:be:03:
115                     10:c5:18:26:bf:c8:e7:af:84:2b:16:d6:63:e9:03:
116                     bf:97:24:ae:c2:1e:11:93:5f:97:96:54:cc:0d:bc:
117                     4c:2a:67
118                 Exponent: 65537 (0x10001)
119     Signature Algorithm: sha256WithRSAEncryption
120          61:a5:10:40:e3:98:54:75:b5:b2:9b:b1:52:88:42:f2:81:3f:
121          ee:6b:ef:93:64:ec:53:9f:a2:ae:97:e7:09:8e:4b:f9:27:2d:
122          cd:00:6d:71:02:c3:7a:c1:84:23:6c:60:c2:77:e8:a3:7a:8a:
123          2e:5e:31:b7:7c:e4:4b:8e:7b:50:5a:d6:9c:c9:70:5c:ef:31:
124          87:96:0d:03:10:b6:3a:76:86:5c:a1:0c:7e:eb:38:aa:0a:3b:
125          db:f0:75:8b:6c:e3:25:39:cb:ef:ac:dc:27:87:58:93:1e:5c:
126          88:2b:27:20:76:36:ef:f4:2c:66:b8:3f:57:bf:cc:9f:0d:32:
127          08:89:64:a7:e3:cf:45:a7:6e:f6:c9:df:ce:03:7d:0a:08:54:
128          46:c7:53:c0:3b:92:66:50:2d:fa:d3:34:2f:cc:92:13:ae:39:
129          49:cf:33:3c:ba:57:2b:f3:62:91:9a:40:de:2b:ce:50:31:6a:
130          67:26:e2:c1:b9:bb:a9:55:71:a4:3f:36:a4:c2:8c:ef:c1:48:
131          5a:80:54:9b:4a:a6:e9:b0:dc:77:35:c0:b3:7f:5e:cb:e9:fe:
132          aa:2f:c5:da:63:93:c5:cc:e0:af:cb:66:a4:e3:c2:d1:4e:8b:
133          1e:6c:28:cc:76:4b:c1:09:32:dc:fe:ed:92:96:9e:1e:be:d7:
134          f8:40:f8:b5:dd:92:91:e3:a4:38:b8:9e:ca:63:d4:22:08:5e:
135          31:04:2e:06:e0:eb:83:48:9f:93:bd:a9:a1:6a:1a:af:ea:7e:
136          1e:7f:9f:63:57:2d:34:11:14:97:bb:75:5a:04:07:e1:4c:9e:
137          12:ce:6f:5e:f6:80:57:c3:fa:be:a2:de:27:be:b8:85:04:1f:
138          27:52:4d:60:54:28:2e:18:3f:22:fe:c8:3b:c7:5e:46:ef:bf:
139          9d:81:58:04:47:58:07:71:76:32:46:04:63:03:aa:cd:b1:3d:
140          be:9d:59:b3:4a:e6:4f:fb:96:5f:0f:f0:9f:e9:51:07:e3:0f:
141          b0:7d:26:55:96:74:91:69:f4:ab:d9:20:6d:59:3d:b8:3c:89:
142          9b:8b:ec:a8:90:7a:45:e4:13:f8:b7:49:1d:e4:c8:d9:5a:2a:
143          96:0e:d2:c1:5f:ad:27:c9:1b:64:89:5c:45:97:89:b0:5a:45:
144          c0:a5:5f:45:3b:fc:52:61:c4:c9:1a:1a:30:52:ef:e2:46:30:
145          75:7f:a1:2e:e2:f7:65:ef:6a:ab:be:3a:1a:44:b7:77:a8:a1:
146          67:cf:a8:77:e1:a1:37:4f:c7:1e:ab:33:58:0b:17:26:f4:60:
147          5d:2c:2b:a7:13:22:2b:aa:33:a2:ca:af:b2:ff:5f:f7:a7:55:
148          1d:2b:6e:61:a6:e6:fc:e9
149 
150  #合并CA和伺服器證書成一個檔案,注意伺服器證書在前        
151 root@ops106:/usr/local/nginx/certs# cat www.devops.com.crt ca.crt  > www.devops.com.pem      

View Code

配置資訊

server {
    listen 80;
    listen 443 ssl;                                                     # 啟用ssl 并指定端口
    ssl_certificate /usr/local/nginx/certs/www.devops.com.pem;          # crt 檔案
    ssl_certificate_key /usr/local/nginx/certs/www.devops.com.key;      # 私鑰檔案
    ssl_session_cache shared:sslcache:20m;                              # ssl 緩存 #在各worker之間使用一個共享的緩存,需要定義一個緩存名稱和緩存空間大小,一兆可以存儲4000個會話資訊,多個虛拟主機可以使用相同的緩存名稱
    ssl_session_timeout 10m;                                            # 用戶端連接配接可以複用ssl session cache中緩存的有效時長,預設5m
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #表示使用的加密套件的類型。
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;                                #表示使用的TLS協定的類型。支援ssl協定版本,早期為ssl現在是TLS,預設為後三個      
server_name www.devops.com;
    error_log /usr/local/nginx/logs/devops.com_error.log notice;
    access_log /usr/local/nginx/logs/devops.com_access.log main;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    location / {
        root /data/nginx/html/devops;
        if ( $scheme = http ) {
            rewrite ^/(.*)$ https://www.devops.com/$1 redirect;
        }
    }
}      

nginx 變量

其他配置

rewrite 指令

     Nginx伺服器利用 ngx_http_rewrite_module 子產品解析和處理rewrite請求,此功能依靠 PCRE(perlcompatible regular expression),是以編譯之前要安裝PCRE庫,rewrite是nginx伺服器的重要功能之一,用于實作URL的重寫,URL的重寫是非常有用的功能,比如它可以在我們改變網站結構之後,不需要用戶端修改原來的書簽,也無需其他網站修改我們的連結,就可以設定為通路,另外還可以在一定程度上提高網站的安全性。

官方文檔: https://nginx.org/en/docs/http/ngx_http_rewrite_module.html

ngx_http_rewrite_module

if 指令

set 指令

break 指令

return 指令

rewrite_log 指令

    通過正規表達式的比對來改變URI,可以同時存在一個或多個指令,按照順序依次對URI進行比對,rewrite主要是針對使用者請求的URL或者是URI做具體處理

    官方文檔:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewriterewrite

    可以配置在 server、location、if

    文法格式 : rewrite regex replacement [flag]; 

    rewrite将使用者請求的URI基于regex所描述的模式進行檢查,比對到時将其替換為表達式指定的新的URI

    注意:如果在同一級配置塊中存在多個rewrite規則,那麼會自下而下逐個檢查;被某條件規則替換完成後,會重新一輪的替換檢查,隐含有循環機制,但不超過10次;如果超過,提示500響應碼,[flag]所表示的标志位用于控制此循環機制如果替換後的URL是以http://或https://開頭,則替換結果會直接以重定向傳回給用戶端, 即永久重定向301

rewrite flag 使用介紹

防盜鍊

其他功能

 第三方子產品

https://github.com/agile6v/awesome-nginx/

自動生成 nginx 配置檔案

https://www.digitalocean.com/community/tools/nginx

一鍵部署 nginx 內建環境

https://oneinstack.com/auto/ 

作者:闫世成

出處:http://cnblogs.com/yanshicheng

聯系:[email protected]

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,如有問題或建議,請多多賜教,非常感謝。

繼續閱讀