基於不一樣的IP、不一樣的端口以及不用得域名實現不一樣的虛擬主機,依賴於核心模塊ngx_http_core_module實現。css
[root@CentOS7 ~]#mkdir /apps/nginx/conf.d [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } } [root@CentOS7 ~]#mkdir /data/nginx/html/pc -pv mkdir: 已建立目錄 "/data/nginx" mkdir: 已建立目錄 "/data/nginx/html" mkdir: 已建立目錄 "/data/nginx/html/pc" [root@CentOS7 ~]#echo "pc web" >>/data/nginx/html/pc/index.html [root@CentOS7 ~]#tail -2 /apps/nginx/conf/nginx.conf include /apps/nginx/conf.d/*.conf; }
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
測試機添加一條主機解析 [root@CentOS-Test ~]#cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.36.104 www.darius.com 訪問測試 [root@CentOS-Test ~]#curl www.darius.com pc web
root:指定web的家目錄,在定義location的時候,文件的絕對路徑等於 root+locationhtml
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } location /about { root /data/nginx/html/pc; # #必需要在pc目錄中建立一個about目錄才能夠訪問,不然報錯。 index index.html; } } [root@CentOS7 ~]#mkdir /data/nginx/html/pc/about [root@CentOS7 ~]#echo "/data/nginx/html/pc/about" >>/data/nginx/html/pc/about/index.html
重啓測試linux
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
訪問nginx
[root@CentOS-Test ~]#curl -L www.darius.com/about /data/nginx/html/pc/about
alias:定義路徑別名,會把訪問的路徑從新定義到其指定的路徑web
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } location /about { # 使用alias的時候uri後面若是加了斜槓則下面的路徑配置必須加斜槓,不然403 #root /data/nginx/html/pc; alias /data/nginx/html/pc; # 當訪問about的時候,會顯示alias定義的/data/nginx/html/pc裏面的內容。 index index.html; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
訪問測試正則表達式
[root@CentOS-Test ~]#curl -L www.darius.com/about pc web
在沒有使用正則表達式的時候,nginx會先在server中的多個location選取匹配度最高的一個uri,uri是用戶請求的字符串,即域名後面的web文件路徑,而後使用該location模塊中的正則url和字符串,若是匹配成功就結束搜索,並使用此location處理此請求。算法
語法規則: location [=|~|~*|^~] /uri/ { … } = #用於標準uri前,須要請求字串與uri精確匹配,若是匹配成功就中止向下匹配並當即處理請求。 ~ #用於標準uri前,表示包含正則表達式而且區分大小寫 ~* #用於標準uri前,表示包含正則表達式而且不區分大寫 !~ #用於標準uri前,表示包含正則表達式而且區分大小寫不匹配 !~* #用於標準uri前,表示包含正則表達式而且不區分大小寫不匹配 ^~ #用於標準uri前,表示包含正則表達式而且匹配以什麼開頭 $ #用於標準uri前,表示包含正則表達式而且匹配以什麼結尾 \ #用於標準uri前,表示包含正則表達式而且轉義字符。能夠轉. * ?等 * #用於標準uri前,表示包含正則表達式而且表明任意長度的任意字符
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } location = /1.jpg { root /data/nginx/images; index index.html; } } [root@CentOS7 ~]#mkdir /data/nginx/images [root@CentOS7 ~]#rz -E rz waiting to receive. [root@CentOS7 ~]#mv 1.jpg /data/nginx/images/ # 上傳1.jpg到/data/nginx/images [root@CentOS7 ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload # 重啓nginx
訪問測試vim
[root@CentOS7 ~]#mv /data/nginx/images/1.jpg /data/nginx/images/Darius.jpg [root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } location ~ /D.*\.jpg { root /data/nginx/images; index index.html; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
測試頁瀏覽器
對用戶請求的uri作模糊匹配,也就是uri中不管都是大寫、都是小寫或者大小寫混合,此模式也都會匹配,一般使用此模式匹配用戶request中的靜態資源並繼續作下一步操做。緩存
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } location ~* /D.*\.jpg { root /data/nginx/images; index index.html; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
#上傳一個和images目錄不同內容的的圖片1.j到/data/nginx/images1 location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ { root /data/nginx/images1; index index.html; } 重啓Nginx並訪問測試
[root@CentOS7-2 ~]#echo "images" >/data/nginx/images123/index.html [root@CentOS7-2 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com_error.log; access_log logs/www_darius_com_access.log; location ^~ /images { root /data/nginx; index index.html; } } [root@CentOS7-2 ~]#nginx -s stop [root@CentOS7-2 ~]#nginx 訪問測試(以images開頭的文件,匹配location規則) [root@CentOS7-2 ~]#curl -L www.darius.com/images123 images
匹配優先級:=, ^~, ~/~*,/ location優先級:(location =) > (location 完整路徑) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 部分起始路徑) > (/) 注: 完整路徑就是 /static 這樣的完整的URL 。 部分就是 使用location ^~ /static 定義了開始,可是後面還有多是 /statici-mage
直接匹配網站根會加速Nginx訪問處理: location = / { ......; } l ocation / { ......; } 靜 態資源配置: location ^~ /static/ { ......; } # 或者 location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { ......; } 多 應用配置 location ~* /app1 { ......; } l ocation ~* /app2 { ......; }
訪問控制基於模塊ngx_http_access_module實現,能夠經過匹配客戶端源IP地址進行限制。
server { listen 80; server_name www.darius.com; error_log logs/www_darius_com.log; location / { alias /data/nginx/html/pc; index index.html; deny 192.168.36.110; allow 192.168.36.0/24; deny all; #先容許小部分,再拒絕大部分 } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
測試
[root@CentOS-Test ~]#curl www.darius.com # 拒絕192.168.36.110主機 <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.14.2</center> </body> </html> [root@CentOS7 ~]#curl www.darius.com # 容許192.168.36.0/24網段主機 pc web
[root@CentOS7 ~]#yum install -y httpd-tools -y [root@CentOS7 ~]#htpasswd -cbm /apps/nginx/conf.d/.htpasswd user1 123456 # 生成認證用戶 Adding password for user user1 [root@CentOS7 ~]#htpasswd -bm /apps/nginx/conf.d/.htpasswd user2 123456 Adding password for user user2 [root@CentOS7 ~]#tail /apps/nginx/conf.d/.htpasswd user1:$apr1$CsaWdjbv$EwlG9UR6hEg/RYKhP0HS/1 user2:$apr1$5/nkjgHY$Sj.vqTB6M4wqapmm.jTu3.
修改配置文件
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com.log; location / { root /data/nginx/html/pc; index index.html; auth_basic "login password"; auth_basic_user_file /apps/nginx/conf.d/.htpasswd; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload # 重啓nginx服務
測試
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_page 500 502 503 504 404 /error.html; # 默認目錄下建立error.html頁面 error_log logs/www_darius_com_error.log; # 錯誤日誌 access_log logs/www_darius_com_access.log; # 訪問日誌 location = /error.html { root html; } location / { root /data/nginx/html/pc; index index.html; } } [root@CentOS7 ~]#echo "ERROR" >/apps/nginx/html/error.html # 建立error頁面 [root@CentOS7 ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
測試
[root@CentOS-Test ~]#curl www.darius.com pc web [root@CentOS-Test ~]#curl www.darius.com/aa # 訪問不存在的頁面,顯示定義的錯誤頁面 ERROR
try_files會按順序檢查文件是否存在,返回第一個找到的文件或文件夾(結尾加斜線表示爲文件夾),若是全部文件或文件夾都找不到,會進行一個內部重定向到最後一個參數。只有最後一個參數能夠引發一個內部重定向,以前的參數只設置內部URI的指向。最後一個參數是回退URI且必須存在,不然會出現內部500錯誤。
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com_error.log; access_log logs/www_darius_com_access.log; location / { root /data/nginx/html/pc; index index.html; try_files $uri $uri/index.html $uri.html =489; # 重啓nginx,當訪問http://www.darius.com/about/xx.html等不存在的uri顯示返回數據的狀態碼 } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
訪問測試
[root@CentOS-Test ~]#curl --head www.darius.com/about/xx.html HTTP/1.1 489 # 489就是自定義的狀態返回碼 Server: nginx/1.14.2 Date: Thu, 30 May 2019 07:56:54 GMT Content-Length: 0 Connection: keep-alive
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com_error.log; access_log logs/www_darius_com_access.log; location / { root /data/nginx/html/pc; index index.html; try_files $uri $uri/index.html $uri.html /about/default.html; # 重啓nginx並測試,當訪問到http://www.darius.com/about/xx.html等不存在的uri會顯示default } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload [root@CentOS7 ~]#echo "default" >> /data/nginx/html/pc/about/default.html
訪問測試
[root@CentOS-Test ~]#curl www.darius.com/about/xx.html default
keepalive_timeout number; #設定保持鏈接超時時長,0表示禁止長鏈接,默認爲75s,一般配置在http字段做爲站點全局配置 keepalive_requests number; #在一次長鏈接上所容許請求的資源的最大數量,默認爲100次
keepalive_requests 3; keepalive_timeout 65 60; 開啓長鏈接後,返回客戶端的會話保持時間爲60s,單次長鏈接累計請求達到指定次數請求或65秒就會被斷開,後面的60爲發送給客戶端應答報文頭部中顯示的超時時間設置爲60s:如不設置客戶端將不顯示超時時間。 Keep-Alive:timeout=60 #瀏覽器收到的服務器返回的報文 若是設置爲0表示關閉會話保持功能,將以下顯示: Connection:close #瀏覽器收到的服務器返回的報文
location /download { autoindex on; # 自動索引功能 autoindex_exact_size on; # 計算文件確切大小(單位bytes),off只顯示大概大小(單位kb、mb、gb) autoindex_localtime on; # 顯示本機時間而非GMT(格林威治)時間 limit_rate 10k; # 限制響應給客戶端的傳輸速率,單位是bytes/second,默認值0表示無限制限速與不限速的對比 root /data/nginx/html/pc; } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
建立測試頁面進行測試
將光盤鏡像掛載到此目錄下進行測試 [root@CentOS7 ~]#mount /dev/sr0 /data/nginx/html/pc/download/ mount: /dev/sr0 寫保護,將以只讀方式掛載 [root@CentOS7 ~]#ll /data/nginx/html/pc/download/ 總用量 1656 -rw-rw-r-- 1 root root 14 11月 26 2018 CentOS_BuildTag drwxr-xr-x 3 root root 2048 11月 26 2018 EFI -rw-rw-r-- 1 root root 227 8月 30 2017 EULA -rw-rw-r-- 1 root root 18009 12月 10 2015 GPL drwxr-xr-x 3 root root 2048 11月 26 2018 images drwxr-xr-x 2 root root 2048 11月 26 2018 isolinux drwxr-xr-x 2 root root 2048 11月 26 2018 LiveOS drwxrwxr-x 2 root root 1656832 11月 25 2018 Packages drwxrwxr-x 2 root root 4096 11月 26 2018 repodata -rw-rw-r-- 1 root root 1690 12月 10 2015 RPM-GPG-KEY-CentOS-7 -rw-rw-r-- 1 root root 1690 12月 10 2015 RPM-GPG-KEY-CentOS-Testing-7 -r--r--r-- 1 root root 2883 11月 26 2018 TRANS.TBL
測試頁
client_max_body_size 1m; #設置容許客戶端上傳單個文件的最大值,默認值爲1m client_body_buffer_size size; #用於接收每一個客戶端請求報文的body部分的緩衝區大小;默認16k; 超出此大小時,其將被暫存到磁盤上的由下面client_body_temp_path指令所定義的位置 client_body_temp_path path [level1 [level2 [level3]]]; #設定存儲客戶端請求報文的body部分的臨時存儲路徑及子目錄結構和數量,目錄名爲16進制的數字,使用hash 以後的值從後往前截取1位、2位、2位做爲文件名: [root@s3 ~]# md5sum /data/nginx/html/pc/index.html 95f6f65f498c74938064851b1bb 96 3d 4 /data/nginx/html/pc/index.html 1級目錄佔1位16進制,即2^4=16個目錄 0-f 2級目錄佔2位16進制,即2^8=256個目錄 00-ff 3級目錄佔2位16進制,即2^8=256個目錄 00-ff 配置示例: client_max_body_size 10m; client_body_buffer_size 16k; client_body_temp_path /apps/nginx/temp 1 2 2; #reload Nginx會自動建立temp目錄
keepalive_disable none | browser ...; # 對哪一種瀏覽器禁用長鏈接 limit_except method ... { ... } # 限制客戶端使用除了指定的請求方法以外的其它方法,僅用於location method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND,PROPPATCH, LOCK, UNLOCK, PATCH 示例: location /upload { root /data/magedu/pc; index index.html; limit_except GET { # 除了GET以外的其餘請求方法,僅容許192.168.36.110主機使用 allow 192.168.36.110; deny all; } }
aio on | off #是否啓用asynchronous file I/O(AIO)功能,須要編譯開啓 linux 2.6以上內核提供如下幾個系統調用來支持aio: 一、SYS_io_setup:創建aio 的context 二、SYS_io_submit: 提交I/O操做請求 三、SYS_io_getevents:獲取已完成的I/O事件 四、SYS_io_cancel:取消I/O操做請求 五、SYS_io_destroy:毀銷aio的context
directio size | off; #操做徹底和aio相反,aio是讀取文件而directio是寫文件到磁盤,啓用直接I/O,默認爲關閉,當文件大於等於給定大小時,例如directio 4m,同步(直接)寫磁盤,而非寫緩存。
open_file_cache off; #是否緩存打開過的文件信息 open_file_cache max=N [inactive=time]; nginx能夠緩存如下三種信息: (1) 文件元數據:文件的描述符、文件大小和最近一次的修改時間 (2) 打開的目錄結構 (3) 沒有找到的或者沒有權限訪問的文件的相關信息 max=N:可緩存的緩存項上限數量;達到上限後會使用LRU(Least recently used,最近最少使用)算法實現管理 inactive=time:緩存項的非活動時長,在此處指定的時長內未被命中的或命中的次數少於 open_file_cache_min_uses指令所指定的次數的緩存項即爲非活動項,將被刪除
open_file_cache_errors on | off; 是否緩存查找時發生錯誤的文件一類的信息 默認值爲off
open_file_cache_min_uses number; open_file_cache指令的inactive參數指定的時長內,至少被命中此處指定的次數方可被歸類爲活動項 默認值爲1
open_file_cache_valid time; 緩存項有效性的檢查驗證頻率,默認值爲60s open_file_cache max=10000 inactive=60s; # 最大緩存10000個文件,非活動數據超時時長60s open_file_cache_valid 60s; # 每間隔60s檢查一下緩存數據有效性 open_file_cache_min_uses 5; # 60秒內至少被命中訪問5次才被標記爲活動數據 open_file_cache_errors on; # 緩存錯誤信息
server_tokens off; # 隱藏Nginx server版本 [root@CentOS7 ~]#grep "server_tokens" /apps/nginx/conf/nginx.conf server_tokens off; [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload 訪問測試 [root@CentOS-Test ~]#curl --head www.darius.com HTTP/1.1 200 OK Server: nginx # 版本號已隱藏 Date: Thu, 30 May 2019 08:27:49 GMT Content-Type: text/html Content-Length: 7 Last-Modified: Thu, 30 May 2019 03:06:03 GMT Connection: keep-alive ETag: "5cef489b-7" Accept-Ranges: bytes