Nginx服務實踐

[TOC]

本文檔版權歸屬:陳雷雷,僅限學習交流

QQ:370460470 blog:www.chenleilei.net

Nginx服務實踐

簡述Nginx(nginx.org)

Nginx (engine x) 是一個高性能的HTTP和反向代理web服務器,同時也提供了IMAP/POP3/SMTP服務。Nginx是由伊戈爾·賽索耶夫爲俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0發佈於2004年10月4日。
其將源代碼以類BSD許可證的形式發佈,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。2011年6月1日,nginx 1.0.4發佈。
Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,在BSD-like 協議下發行。其特點是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等。

Nginx 適用於各類網站,處理能力和可擴展性都優於apache,目前爲止,主流網站幾乎都採用的nginx作爲核心的服務

2015年排名:

Nginx服務實踐

2017年排名:

Nginx服務實踐

2019年排名

Nginx服務實踐

通過兩張圖我們可以清晰發現,apache份額逐步下降,而nginx則穩步上升,由此可見,nginx的發展前景

兩年時間兩大龍頭Nginx服務與apache服務佔有率差距從 11%差距縮減到 5%,足以見得Nginx的優秀

下面我們將從多方入手,帶你學習掌握Nginx

Nginx優點

  • 配置簡單
  • 靈活多變
  • 負載均衡
  • 緩存服務
  • 併發更高

Nginx的重要特性

參考:http://nginx.org/en

  • [ ] 可針對靜態資源告訴併發訪問提供數據緩存
  • [ ] 可使用反向代理加速,並且可以讓反向代理數據緩存
  • [ ] 具有負載均衡,節點健康檢查和容錯功能
  • [ ] 支持遠程的FastCGI服務的緩存加速
  • [ ] 支持FastCGI、Uwsgi、SCGI、Memcached的加速和花奴才能
  • [ ] 支持SSL、TLS、SNI
  • [ ] 具有模塊化的架構,包括 gzip壓縮、ranges支持、chunked響應,XSLT、SSI及圖像縮放等功能,在ssl過濾器中,一個包含多個ssl的頁面,如果由FastCGI或反向代理處理,可以被並行處理
  • [ ] 支持基於名字,端口及IP的多虛擬主機站點
  • [ ] 支持keep-alived和pipelind的連接
  • [ ] 可進行簡單方便靈活的配置管理
  • [ ] 支持修改Nginx配置,在代碼上線時平滑重啓不中斷業務的能力
  • [ ] 支持自定義的訪問日誌格式,臨時緩存寫日誌操作,快速的日誌輪詢以及通過rsyslog處理日誌
  • [ ] 可以利用信號控制Nginx進程(如:Nginx升級會用到信號)
  • [ ] 支持3xx-5xx的http狀態碼重定向
  • [ ] 支持rewrite模塊,支持URI重定向以及正則表達式匹配
  • [ ] 支持PUT,DELETE,MKCOL,COPY及MOVE等腳特殊的HTTP請求方法
  • [ ] 支持FLV流和MP4流技術產品的應用
  • [ ] 支持HTTP相應速率限制
  • [ ] 支持同一IP地址的併發連接和請求限制
  • [ ] 支持郵件服務代理

在企業面試也會被問到: Nginx http服務器的特色和優點

  • 支持高併發(三萬併發連接下,nginx線程消耗內存不到200MB)
  • 資源消耗少
  • 可以做反向代理,緩存加速,負載均衡
  • 具備健康檢查功能
  • 支持異步網絡I/O事件處理模型(epoll事件處理模型)
  • 具有Squid等專業緩存的功能

常用的動態服務軟件

  • php:大小型公司都會使用,動態網頁語言PHP程序的解析容器,可以配合apache nginx 解析動態程序

php在apache解析的時候使用的mod_php5.so(module),php在nginx解析的時候使用的是FastCGI守護進程模式提供服務

  • tomcat:中小型企業動態web服務的主流,java程序是通過tomcat來提供訪問
  • IIS 微軟旗下的web服務軟件

Nginx的企業應用

  • 作爲web服務軟件
  • 作爲反向代理/負載均衡軟件
  • 前端業務數據緩存服務(memcache redis mongodb)

nginx的主要應用場景爲 反向代理,和提供web服務

如何正確的選擇web服務器

實際工作中我們需要根據業務的需求來選擇合適的服務軟件,有關web服務建議如下:

  • [ ] 靜態業務:如果是高併發場景,儘量採用Nginx
  • [ ] 動態業務: 理論上採用nginx和apache都可以,但是建議選擇nginx,爲了避免相同業務的服務軟件多樣化,增加額外的維護成本,動態業務可以由nginx做前端代理,根據頁面的元素類型或者目錄,轉發到後端的 tomcat
  • [ ] 既有靜態業務,又有動態業務,推薦採用nginx

如何安裝一個Nginx

安裝nginx的方式多種多樣,常見的分爲3種

  • 通過yum來安裝nginx
  • 通過打包好的rpm包方式安裝
  • 通過源碼編譯安裝

注意下載頁面最好選擇穩定版:http://nginx.org/en/download.html

Nginx服務實踐

這裏我們選擇 nginx-1.16.0 在標籤上右鍵複製下載地址

編譯安裝nginx


#第一步: 下載nginx:
#http://nginx.org/en/download.html 

##注意 nginx版本最好選擇 半年到一年之前的版本,不要選用最新版。
##我這裏選擇1.17的穩定版 

#創建一個目錄存放nginx安裝包 
mkdir -p /server/tools

#安裝包下載:
cd /server/tools
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar xf nginx-1.16.0.tar.gz
cd nginx-1.16.0

#第二步: 解決軟件依賴關係
# openssl-devel -- SSL證書相關依賴包
# pcre-devel  -- 正則相關依賴包
# gd-devel  -- 圖片處理相關依賴包

yum install -y pcre pcre-devel openssl openssl-devel gd-devel  zlib-devel gcc

#第三步: 創建nginx進程相關用戶
useradd -s /sbin/nologin -M www

#第四步:執行預編譯
cd /server/tools/
./configure --prefix=/application/nginx-1.16 --user=www --group=www --with-http_image_filter_module  --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module  

# 第五步:make 編譯
make 
echo $?        #這一步可做可不做,返回值爲0代表正常 爲其他數值爲異常

#第六步: 編譯安裝
make install

#創建軟連接,作用在升級新版本時只需要替換軟連接,而不需要停止服務更換版本
ln -s /application/nginx-1.16 /application/nginx

#優化nginx啓動命令
#添加環境變量:export PATH="$PATH:/application/nginx/sbin"
echo "export PATH="$PATH:/application/nginx/sbin"">>/etc/profile
source /etc/profile

啓動與關閉nginx

上面的環境變量添加完成後,可以直接命令行輸入

nginx 啓動nginx服務

nginx -t 檢查配置文件語法

nginx編譯參數詳解:

(基於1.7.11以上版本)

./configure --help 查看幫助

./config編譯參數 作用 說明 默認
--help 幫助
--prefix=PATH 設置安裝路徑 不指定默認安裝到 : /usr/local/nginx (⭐⭐⭐⭐⭐) 手動指定
--sbin-path=PATH nginx執行命令路徑 Nginx可執行文件安裝路徑。只能安裝時指定,如果沒有指定,默認爲 /sbin/nginx 手動指定
--modules-path=PATH 動態模塊庫 定義一個將安裝的nginx的的動態模塊的目錄。情況默認下使用該prefix/modules目錄 手動指定
--conf-path=PATH 配置文件路徑 設置nginx.conf路徑名 手動指定
--error-log-path=PATH 錯誤日誌 設置錯誤日誌路徑 手動指定
--pid-path=PATH pid文件 pid文件路徑 手動指定
--lock-path=PATH 鎖文件路徑 鎖文件路徑 手動指定
--user=USER 運行的用戶 設置nginx運行worker進程的的用戶 (⭐⭐⭐⭐⭐) 手動指定
--group=GROUP 運行的組 設置nginx運行worker進程的的組 (⭐⭐⭐⭐⭐) 手動指定
--build=NAME ./configure的臨時文件名 configure執行時與編譯期間產生的臨時文件的文件名 手動指定
--builddir=DIR 臨時文件目錄 configure執行時與編譯期間產生的臨時文件放置的目錄,包括產生的Makefile、C源文件、目標文件、可執行文件等 手動指定
--with-select_module 事件處理驅動 使用select module處理事件驅動 開啓
--without-select_module select模塊 不安裝select module 關閉
--with-poll_module poll模塊 使用poll module處理事件驅動 poll的性能與select類似,在大量併發連接下性能都遠不如epoll。默認情況下,Nginx是不安裝poll 開啓
--without-poll_module poll模塊 默認不安裝poll module 不需要指定 關閉
--with-threads 線程池 參考:https://segmentfault.com/a/1190000002924458<br /><br />http://www.ttlsa.com/nginx/thread-pools-boost-performance-9x/<br />在http模塊下配置<br />thread_pool one threads=128 max_queue=65535;<br />在server模塊下調用<br /> aio threads=one;<br />該操作可以使得nginx性能提升9倍,僅限於1.7.11以上版本<br />解釋:<br />thread_pool one threads=128 #128個工作線程<br />max_queue=65535 #最大的隊列任務數量<br />詳細使用辦法將會在下面詳解 開啓
--with-file-aio AIO支持 使用AIO方式處理事件驅動 (IO異步大幅度提升性能,需要內核 2.6.22 以上的版本) 開啓
--with-http_ssl_module SSL模塊 SSL模塊開啓(⭐⭐⭐⭐⭐) 開啓
--with-http_v2_module SSL V2版本 enable ngx_http_v2_module 開啓https2.0(⭐⭐⭐⭐⭐) 開啓
--with-http_realip_module 改變請求頭 允許我們改變客戶端請求頭中客戶端IP地址值(例如,X-Real-IP 或 X-Forwarded-For)<br />在 location 裏頭插入:<br />set_real_ip_from 192.168.2.1;<br />real_ip_header X-Real-IP; 開啓
--with-http_addition_module 響應頭追加內容 用於在響應之前或者之後追加文本內容,比如想在站點底部追加一個js或者css,可以使用這個模塊來實現。默認不開啓,開啓需要重新編譯<br />參考:https://www.cnblogs.com/52fhy/p/10226651.html 開啓
--with-http_xslt_module=dynamic 創建一個modules的子目錄,用來存放動態加載模塊共享對象的二進制文件,默認的路徑爲/usr/local/nginx/modules. 開啓
--with-http_image_filter_module 圖片處理模塊,圖片縮放 用法:<br />location ~ /simg/..jpg$ { <br />#proxy_pass http://10.11.11.11; <br />#rewrite "/simg/(..jpg)$" /img/$1 break ; image_filter resize 100 100; <br />error_page 415 = /empty; <br />}<br />這樣訪問/simg/目錄下的圖片,都會按照高度最高100並且寬度最高100按照原圖比例進行截取出來,並輸出給瀏覽器。當然也可以開啓重寫去讀取本機另一個目錄下源文件;如果不在一臺機器上就可以開啓proxy_pass,並加上重寫即可。<br />安裝此模塊需要先安裝依賴:<br />yum -y install gd-devel 開啓
--with-http_image_filter_module 圖像處理過濾模塊,開啓圖片壓縮版本必須高於 0.7.54 動態加載圖片模塊(⭐⭐⭐⭐⭐) 開啓
--with-http_geoip_module 解析客戶端IP 創建變量,使用預編譯的MaxMind數據庫解析客戶端IP地址,得到變量值。(不建議啓用) 開啓
--with-http_geoip_module=dynamic 解析客戶端IP 功能如上,不建議使用 開啓
--with-http_sub_module 替換響應 該ngx_http_sub_module模塊是一個過濾器,通過將一個指定的字符串替換爲另一個字符串來修改響應。該模塊不是默認生成的,它應該使用--with-http_sub_module 配置參數啓用 。<br />參考:https://www.cnblogs.com/heruiguo/p/9076239.html 開啓
--with-http_dav_module 啓用對WebDav協議的支持 允許ngx_http_dav_module模塊(mod_dav)<br />爲文件和目錄指定權限,限制不同類型的用戶對於頁面有不同的操作權限 開啓
--with-http_flv_module 啓動對flv媒體支持 以構建爲閃存視頻(FLV)提供文件僞流式服務器端的請立即獲取iTunes中的 ngx_http_flv_module模塊。該模塊不是默認生成的 開啓
--with-http_mp4_module 啓動對MP4媒體支持 可以構建爲MP4提供文件僞流式服務器端的請立即獲取iTunes中的 ngx_http_mp4_module模塊。該模塊不是默認生成的。 開啓
--with-http_gunzip_module 壓縮 對於不支持gzip編碼的客戶,該模塊用於爲客戶解壓縮預壓縮內容 開啓
--with-http_gzip_static_module 靜態壓縮 <br />gzip 壓縮爲.gz 響應給客戶端<br />案例:<br />location ~* .(json)$ {<br/> gzip_static on;<br/> gzip_proxied expired no-cache no-store private auth; } 開啓
--with-http_random_index_module 隨機默認主頁 就是用戶訪問時,給一個隨機的主頁 開啓
--with-http_secure_link_module 下載防盜鏈 允許通過密鑰保護指定的文件。<br />參考:http://www.ttlsa.com/nginx/nginx-modules-secure_link/ 開啓
--with-http_degradation_module 內存不足提示 允許當剩餘內存較低時對某些位置的請求返回 204 或 404 狀態碼 開啓
--with-http_slice_module 一個分割請求轉換成子請求,每個返回一定範圍內響應的濾波器。該過濾器提供了更有效的大響應緩存。 所述ngx_http_slice_module模塊(1.9.8)是一個分割請求轉換成子請求,每個返回一定範圍內響應的濾波器。該過濾器提供了更有效的大響應緩存。<br />新版本特性:https://cloud.tencent.com/developer/section/1259383 開啓
--with-http_stub_status_module 運行狀態監控 需要編譯安裝後纔可以使用<br />curl 127.0.0.1/nginx_status 檢查狀態 開啓
--without-http_charset_module 用於對網頁重新編碼 將指定的字符集添加到“Content-Type”響應標題字段。如果此字符集與source_charset僞指令中指定的字符集不同,則執行轉換<br />參考連接:https://cloud.tencent.com/developer/section/1259007 禁用
--without-http_gzip_module 用於壓縮的響應緩衝區<br />gzip”方法壓縮響應的過濾器。這通常有助於將傳輸數據的大小減少一半甚至更多。 開啓方式:<br />gzip on; <br />gzip_min_length 1000; <br />gzip_proxied expired no-cache no-store private auth;<br /> gzip_types text/plain application/xml;<br /><br />參考連接:https://cloud.tencent.com/developer/section/1259169 關閉
--without-http_ssi_module 禁用ngx_http_ssi_module支持 禁用ngx_http_ssi_module支持(該模塊提供了一個在輸入端處理處理服務器包含文件(SSI)的過濾器,目前支持SSI命令的列表是不完整的) 關閉
--without-http_userid_module 啓用或禁用設置Cookie並記錄收到的Cookie 默認即編譯進入Nginx的HTTP模塊<br />實例:<br />userid on; <br />userid_name uid; <br />userid_domain example.com; userid_path /; <br />userid_expires 365d; <br />userid_p3p 'policyref="/w3c/p3p.xml", CP="CUR ADM OUR NOR STA NID"'; 關閉
--without-http_access_module 訪問控制 location / {<br/>deny 192.168.1.1;<br/>allow 192.168.1.0/24;<br/>allow 10.1.1.0/16;<br/>allow 2001:0db8::/32;<br/>deny all;<br/>} 關閉
--without-http_auth_basic_module 用戶認證(默認禁止) 參考文檔:<br />https://blog.csdn.net/guyue35/article/details/53906843 關閉
--without-http_mirror_module 流量鏡像 默認禁止該模塊 關閉
--without-http_autoindex_module 自動爲沒有索引頁面的目錄生成文件列表。 默認禁止 關閉
--without-http_geo_module 禁ngx_http_geo_module 禁用ngx_http_geo_module支持 關閉
--with-http_stub_status_module 啓動監控模塊(服務狀態查看模塊) 爲監控提供的狀態模塊 關閉
--with-http_stub_status_module 禁用 ngx_http_charset_module 禁用 ngx_http_charset_module模塊 關閉
--without-http_charset_module 模塊爲響應頭的“Content-Type”添加指定的字符集。 此外,這個模塊可以把數據從一個字符集轉換爲另一個字符集,但也有以下限制:<br />只能單向轉換,即從服務端到客戶端, 只有單字節字符集能被轉換 或者單字節字符集和UTF-8之間的互相轉換。 語法: charset charset<br />默認值: charset off;<br />上下文: http, server, location, if in location<br />相關幫助:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_charset_module.html 關閉
--without-http_gzip_module gzip”方法壓縮響應的過濾器。這通常有助於將傳輸數據的大小減少一半甚至更多。 例子:<br />gzip on; <br />gzip_min_length 1000; <br />gzip_proxied expired no-cache no-store private auth; <br />gzip_types text/plain application/xml; 關閉
--without-http_ssi_module ngx_http_ssi_module模塊是一個過濾器,用於處理通過它傳遞的響應中的SSI(服務器端包含)命令。目前,支持的SSI命令列表不完整 ssi on; # 開啓SSI支持<br/>ssi_silent_errors on; # 默認爲off,設置爲on則在處理SSI文件出錯時不輸出錯誤信息<br/>ssi_types text/html; # 需要支持的shtml 默認是 text/html 關閉
--without-http_userid_module 模塊是一個過濾器,用於處理通過它傳遞的響應中的SSI(服務器端包含)命令。目前,支持的SSI命令列表不完整。 發行用戶身份標識cookie。 關閉
--without-http_map_module 爲地圖提供的 禁用Map模塊,該模塊允許你聲明map區段 關閉
--without-http_split_clients_module 客戶端訪問用戶的劃分 ngx_http_split_clients_module支持,該模塊用於基於用戶ip地址、報頭、cookies劃分用戶。 禁用
--without-http_referer_module 基於RefererHTTP請求頭對請求進行過濾。主要用於防盜鏈 location ~ ..(gif|jpg|jpeg|png|bmp|swf)$ { <br />valid_referers none blocked .91linux.org 91linux.org server_names ~.google. ~.baidu.;<br /> if ($invalid_referer) { <br /> return 403; <br /> #rewrite ^/ https://chenleilei.net/attachment/20190508/b931ccc969f9426fbaab17f8a0101246.jpg; } } 禁用
--without-http_rewrite_module 默認安裝,未配置啓用 僞靜態模塊 開啓
--without-http_proxy_module 默認安裝,未配置啓用 默認安裝 開啓
--without-http_fastcgi_module 默認安裝,未配置啓用 解析php請求資源<br />location ~ .php$ {<br/> root html;<br/> fastcgi_pass 127.0.0.1:9000;<br/> fastcgi_index index.php;<br/> fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;<br/> include fastcgi_params;<br/>} 開啓
--without-http_uwsgi_module 默認安裝,未配置啓用 支持uWSGI協議 開啓
--without-http_scgi_module 默認安裝,未配置啓用 支持scgi協議 開啓
--without-http_split_clients_module 默認安裝,未配置啓用 適用於A / B測試的變量,也稱爲分割測試。<br />參考文檔:https://cloud.tencent.com/developer/section/1259388 開啓
--without-http_referer_module 防盜鏈 參考文檔:https://www.centos.bz/2017/09/nginx-ngx_http_referer_module-hotlink-protect/ 開啓
--without-http_rewrite_module 地址重寫 用於域名重定向 開啓
--without-http_proxy_module 模塊允許將請求傳遞給另一臺服務器 用於反向代理 開啓
--without-http_grpc_module URI重寫 添加這個參數重寫功能會被關閉
--without-http_memcached_module 啓用對memcached的支持 啓用memcached <br />nginx+php+memcached方法:https://cloud.tencent.com/developer/article/1434652
--without-http_limit_conn_module 併發連接控制模塊 配置啓用 不啓用
--without-http_limit_req_module 限定客戶端創建連接的頻率。 配置啓用 不啓用
--without-http_empty_gif_module 產生單像素透明的gif圖片 配置啓用 不啓用
--without-http_browser_module 解析HTTP請求頭中的”User-Agent“ 的值。 配置啓用 不啓用
--without-http_upstream_hash_module 反向代理:<br />upstream backend { server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp/backend3; server backup1.example.com:8080 backup; server backup2.example.com:8080 backup; } server { location / { proxy_pass http://backend; } } 配置啓用 不啓用
--without-http_upstream_ip_hash_module 反向代理提供ip hash算法模塊 配置啓用 不啓用
--without-http_upstream_least_conn_module 決定最少連接數被啓用 配置啓用 不啓用
--without-http_upstream_random_module 隨機連接負載 配置啓用 不啓用
--without-http_upstream_keepalive_module 保持上游連接放入緩存<br />大於keepalive xx; 數值會被關閉 配置啓用 不啓用
--without-http_upstream_zone_module 參考:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#zone<br />這個定義*name**size*其保持組的配置和運行時被工作者進程之間共享狀態中的共享存儲器區。幾個組可能共享同一個區域 配置啓用 不啓用
--with-http_perl_module 模塊用於在Perl中實現位置和變量處理程序,並將Perl調用插入到SSI中。<br />該模塊不是默認生成的,它應該使用--with-http_perl_module配置參數啓用 默認不啓用配置啓用 不啓用
--with-perl_modules_path=PATH Perl處理程序<br />參考:https://cloud.tencent.com/developer/section/1259240 默認不啓用配置啓用 不啓用
--with-perl=PATH perl模塊解析 默認不啓用配置啓用
--http-log-path=PATH access_log日誌格式定義模塊<br />log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; access_log /spool/logs/nginx-access.log compression buffer=32k; 默認啓用<br />參考連接:https://cloud.tencent.com/developer/section/1259213 不啓用
--http-client-body-temp-path=PATH 設置http客戶機請求主體臨時文件的路徑 如:<br />–http-client-body-temp-path=/opt/websuite/nginx/temp/client 不啓用
--http-proxy-temp-path=PATH 這個指令的工作原理類似於client_body_temp_path,它指定一個位置來緩衝對文件系統的大型代理請求。 如:--http-proxy-temp-path=``/var/tmp/nginx/proxy/<br />參考:https://www.cnblogs.com/bluestorm/p/4574688.html 不啓用
--http-fastcgi-temp-path=PATH 定義用於存儲臨時文件的目錄,其中包含從FastCGI服務器接收的數據<br />如:fastcgi_temp_path / spool/nginx/fastcgi_temp 1 2; 參考:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_fastcgi_module.html#fastcgi_temp_path 不啓用
--http-uwsgi-temp-path=PATH 設置的臨時文件的目錄都放在同一個文件系統上 升級httpv2 不啓用
--http-scgi-temp-path=PATH 用於存儲從SCGI服務器接收到的數據的臨時文件。指定目錄下最多可以使用三級子目錄層次結構 如:scgi_temp_path /spool/nginx/scgi_temp 1 2; 不啓用
--without-http disable HTTP server 禁用http server功能 不啓用
--with-mail 啓用POP3、IMAP4、SMTP代理模塊 啓用郵件代理 不啓用
--with-mail=dynamic 啓用POP3、IMAP4、SMTP動態代理模塊 啓用動態郵件代理 不啓用
--with-mail_ssl_module 啓用郵件SSL 啓用郵件SSL 不啓用
--without-mail_pop3_module 啓用郵件pop3 啓用郵件pop3 不啓用
--without-mail_imap_module 啓用郵件imap 啓用郵件imap 不啓用
--without-mail_smtp_module 啓用郵件smtp 啓用郵件smpt 不啓用
--with-stream 啓用TCP/UDP代理模塊 啓用TCP/UDP代理模塊 不啓用
--with-stream=dynamic 啓用TCP/UDP動態代理模塊 啓用TCP/UDP動態代理模塊
--with-stream_ssl_module 啓用ngx流ssl模塊 啓用ngx流ssl模塊 不啓用
--with-stream_realip_module 啓用ngx流realip模塊 啓用ngx流realip模塊 不啓用
--with-stream_geoip_module 啓用ngx流地理ip模塊 啓用ngx流地理ip模塊 不啓用
--with-stream_geoip_module=dynamic 啓用動態ngx流地理ip模塊 啓用動態ngx流地理ip模塊 不啓用
--with-stream_ssl_preread_module 啓用ngx流ssl預讀模塊 啓用ngx流ssl預讀模塊 不啓用
--without-stream_limit_conn_module 用於限制每個定義的鍵連接的數量,尤其是,從一個單一的IP地址的連接的數量。 如:limit_conn_zone $binary_remote_addr zone=addr:10m;<br /> server { ... limit_conn addr 1; <br />} 不啓用
--without-stream_access_module 允許限制訪問某些客戶端地址 如:<br />server { . <br />deny 192.168.1.1; <br />allow 192.168.1.0/24; <br />allow 10.1.1.0/16; <br /> allow 2001:0db8::/32; <br />deny all; <br />} 不啓用
--without-stream_geo_module 創建客戶端的源IP地址值的變量。 參考:https://cloud.tencent.com/developer/section/1259605 不啓用
--without-stream_map_module 設置size地圖變量散列表的最大值 參考:https://cloud.tencent.com/developer/section/1259623 不啓用
--without-stream_split_clients_module 分割測試變量 創建一個適合於A / B測試 參考:https://cloud.tencent.com/developer/section/1259654 不啓用
--without-stream_return_module 允許發送一個指定的值給客戶端,然後關閉連接 server { <br />listen 12345; <br />return $time_iso8601;<br /> } 不啓用
--without-stream_upstream_hash_module 允許使服務器定期健康檢查在一組。服務器組必須駐留在共享內存中,<br />如果健康檢查失敗,服務器將被視爲不健康。如果爲同一組服務器定義了多個運行狀況檢查,則任何檢查的單個故障都會導致相應的服務器被認爲不健康。客戶端連接不會以“檢查”狀態傳遞給不健康的服務器和服務器。 參考:https://cloud.tencent.com/developer/section/1259680 不啓用
--without-stream_upstream_least_conn_module 指定組應使用負載平衡方法,其中將連接傳遞到具有最少活動連接的服務器,同時考慮服務器的權重。如果有多個這樣的服務器,則使用加權循環平衡方法依次嘗試它們 參考:http://nginx.org/en/docs/stream/ngx_stream_upstream_module.html?&_ga=2.182164519.1037785303.1522656538-1142065036.1522656538#least_conn 不啓用
--without-stream_upstream_random_module 指定組應使用負載平衡方法,其中將連接傳遞給隨機選擇的服務器,同時考慮服務器的權重。 參考:http://nginx.im/ngx/stream/ngx_stream_upstream_module.html#random 不啓用
--with-google_perftools_module 該模塊專供nginx開發人員使用 https://cloud.tencent.com/developer/section/1258973 不啓用
--add-module=PATH 安裝第三方模塊 –add-module=/第三方模塊目錄 不啓用
--add-dynamic-module=PATH 需要增加第三方模塊,使用參數--add-dynamic-module=即可 參考:http://www.ttlsa.com/nginx/nginx-dynamic-modules/ 不啓用
--with-cc=PATH 設置C編譯器路徑 設置C編譯器路徑 不啓用
--with-cpp=PATH 設置C預處理器路徑名 設置C預處理器路徑名 不啓用
--with-cc-opt=OPTIONS 設置額外的C編譯器選項 設置額外的C編譯器選項 不啓用
--with-ld-opt=OPTIONS 設置其他鏈接器選項 設置其他鏈接器選項 不啓用
--with-cpu-opt=CPU 爲指定的CPU生成有效的值 爲特定的 CPU 編譯,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64<br />要確定CPU類型,可以通過如下命令::<br />grep "model name" /proc/cpuinfo 不啓用
--with-openssl=DIR 設定OpenSSL庫文件路徑 不指定默認安裝 不啓用
--with-openssl-opt=OPTIONS 設置OpenSSL庫源的路徑 爲OpenSSL構建設置其他選項 不啓用
--with-debug 啓用調試日誌 啓用調試日誌 不啓用

3

  --help                             print this message   

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory

  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module

  --with-threads                     enable thread pool support

  --with-file-aio                    enable file AIO support

  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module
  --with-http_flv_module             enable ngx_http_flv_module
  --with-http_mp4_module             enable ngx_http_mp4_module
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  --with-http_random_index_module    enable ngx_http_random_index_module
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  --with-http_degradation_module     enable ngx_http_degradation_module
  --with-http_slice_module           enable ngx_http_slice_module
  --with-http_stub_status_module     enable ngx_http_stub_status_module

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module
  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
  --without-http_mirror_module       disable ngx_http_mirror_module
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  --without-http_geo_module          disable ngx_http_geo_module
  --without-http_map_module          disable ngx_http_map_module
  --without-http_split_clients_module disable ngx_http_split_clients_module
  --without-http_referer_module      disable ngx_http_referer_module
  --without-http_rewrite_module      disable ngx_http_rewrite_module
  --without-http_proxy_module        disable ngx_http_proxy_module
  --without-http_fastcgi_module      disable ngx_http_fastcgi_module
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  --without-http_scgi_module         disable ngx_http_scgi_module
  --without-http_grpc_module         disable ngx_http_grpc_module
  --without-http_memcached_module    disable ngx_http_memcached_module
  --without-http_limit_conn_module   disable ngx_http_limit_conn_module
  --without-http_limit_req_module    disable ngx_http_limit_req_module
  --without-http_empty_gif_module    disable ngx_http_empty_gif_module
  --without-http_browser_module      disable ngx_http_browser_module
  --without-http_upstream_hash_module
                                     disable ngx_http_upstream_hash_module
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_random_module
                                     disable ngx_http_upstream_random_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
  --without-http_upstream_zone_module
                                     disable ngx_http_upstream_zone_module

  --with-http_perl_module            enable ngx_http_perl_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-perl_modules_path=PATH      set Perl modules path
  --with-perl=PATH                   set perl binary pathname

  --http-log-path=PATH               set http access log pathname
  --http-client-body-temp-path=PATH  set path to store
                                     http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files

  --without-http                     disable HTTP server
  --without-http-cache               disable HTTP cache

  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  --without-mail_imap_module         disable ngx_mail_imap_module
  --without-mail_smtp_module         disable ngx_mail_smtp_module

  --with-stream                      enable TCP/UDP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_realip_module        enable ngx_stream_realip_module
  --with-stream_geoip_module         enable ngx_stream_geoip_module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_module
  --without-stream_geo_module        disable ngx_stream_geo_module
  --without-stream_map_module        disable ngx_stream_map_module
  --without-stream_split_clients_module
                                     disable ngx_stream_split_clients_module
  --without-stream_return_module     disable ngx_stream_return_module
  --without-stream_upstream_hash_module
                                     disable ngx_stream_upstream_hash_module
  --without-stream_upstream_least_conn_module
                                     disable ngx_stream_upstream_least_conn_module
  --without-stream_upstream_random_module
                                     disable ngx_stream_upstream_random_module
  --without-stream_upstream_zone_module
                                     disable ngx_stream_upstream_zone_module

  --with-google_perftools_module     enable ngx_google_perftools_module
  --with-cpp_test_module             enable ngx_cpp_test_module

  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module

  --with-compat                      dynamic modules compatibility

  --with-cc=PATH                     set C compiler pathname
  --with-cpp=PATH                    set C preprocessor pathname
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-cpu-opt=CPU                 build for the specified CPU, valid values:
                                     pentium, pentiumpro, pentium3, pentium4,
                                     athlon, opteron, sparc32, sparc64, ppc64

  --without-pcre                     disable PCRE library usage
  --with-pcre                        force PCRE library usage
  --with-pcre=DIR                    set path to PCRE library sources
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  --with-pcre-jit                    build PCRE with JIT compilation support

  --with-zlib=DIR                    set path to zlib library sources
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro

  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging

編譯參數:

./configure --prefix=/application/nginx-1.16 --user=www --group=www --with-http_image_filter_module  --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module

編譯錯誤列表:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

需要安裝pcre包
yum install -y pcre pcre-devel openssl openssl-devel

/configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

因爲添加了圖片處理模塊,還需要安裝相關支持
yum -y install gd-devel

訪問測試:

Nginx服務實踐

nginx語法檢查啓動與重載

nginx語法檢查

nginx -t

nginx: the configuration file /application/nginx-1.16/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16/conf/nginx.conf test is successful

出現上面兩行代表成功

其他結果均爲錯誤或者警告

nginx 停止與啓動

nginx -s stop 停止nginx

nginx -s start 啓動nginx

nginx -s reload 重載配置文件

Nginx文件精簡化啓動

在面對多站點的配置的時候,每個站點配置都在 nginx.conf 文件中,這樣會導致nginx文件擁擠,配置站點時出錯概率增大,在刪除配置的時候容易產生誤刪其他站點的可能性,爲了避免這個情況,我們可以使用多站點配置文件,每個站點都是一個獨立的配置文件,這樣更方便管理,同時提升安全性

nginx默認配置文件名爲 nginx.conf.default

過濾出默認配置 egrep -v "#|^$" /application/nginx/conf/nginx.conf.default

我將 過濾出來的新配置追加到了nginx.conf中

egrep -v "#|^$" /application/nginx/conf/nginx.conf.default >/application/nginx/conf/nginx.conf

worker_processes  1;                #------- main 核心區塊
error_log logs/error.log;           #------- 定義錯誤日誌
pid      logs/nginx.pid;            #------- 定義進程pid文件

events {
    worker_connections  1024;       #------- 核心功能模塊
    use epoll;                      #------- 使用epoll事件處理模型
}

http {                              # http 模塊開始
    include       mime.types;       # 定義支持的媒體類型文件,如果這個文件中沒有某類型文件,則不支持
    default_type  application/octet-stream;   # 默認類型
    sendfile        on;             # sendfile文件系統優化
    keepalive_timeout  65;          # 響應超時時間

    server {     --------------------------------------|------# server 區塊開始
        listen       80;                               |
        server_name  localhost;                        |
        location / {                                   |
            root   html;                               |
            index  index.html index.htm;               |
        }                                              |
        error_page   500 502 503 504  /50x.html;       |
        location = /50x.html {                         |
            root   html;                               |
           }                                           |
            }    --------------------------------------|------# server 區塊結束
}                                   # http模塊結束

創建新站點

在 http 模塊下添加新配置文件

如何添加新配置文件?

新的配置文件可以通過nginx.conf.default文件過濾出來,取出server塊 由於末尾還會少一個 } 符號,還得繼續添加 } 新配置文件正常可用

## 查看取出內容:
[root@leilei conf]# egrep -v "#|^$" /application/nginx/conf/nginx.conf.default | sed -n '10,16p'
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    這裏可以明顯看處取出的文件中缺少一個 } 我們等會兒添加,現在我們講這個配置文件追加爲新站點配置文件
    如:www.conf

爲了確保我們新站點配置文件統一,所以在創建新的配置文件前,需要創建一個新的文件夾,用於存放站點配置文件

## 創建站點配置文件目錄
mkdir /application/nginx/conf/extra -p

## 取出站點配置文件 追加到新的站點名 配置文件中, 如 新站點名叫  www.leilei.net
[root@leilei conf]# egrep -v "#|^$" /application/nginx/conf/nginx.conf.default | sed -n '10,16p' >/application/nginx/conf/extra/www.chenleilei.net.conf

## 查看一下我們新增的配置文件
[root@leilei conf]# cat /application/nginx/conf/extra/www.chenleilei.net.conf
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
#因爲這裏還缺少 一個 } 符號 現在我們必須追加進去,否則等下會報錯
## 追加缺少的 } 到配置文件
[root@leilei conf]# echo "}" >>/application/nginx/conf/extra/www.chenleilei.net.conf
[root@leilei conf]# cat /application/nginx/conf/extra/www.chenleilei.net.conf
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
}

# 現在站點目錄已經完成配置,我們現在只需要在主配置文件中添加我們的新站點配置文件即可
# 同時
新增語句: include /application/nginx/conf/extra/www.chenleilei.net.conf;

新的 nginx.conf內容:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include /application/nginx/conf/extra/www.chenleilei.net.conf; 
    }

#配置完成後,我們去看看新的站點文件,是否還缺少配置

[root@leilei extra]# vim www.chenleilei.net.conf

  server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;         ## 這裏是我們的網站根目錄設置,我們也可以創建個目錄設置爲網站目錄
            index  index.html index.htm;
        }
} 

#創建網站目錄
[root@leilei]# cd /application/nginx/html/
[root@leilei html]# mkdir www.chenleilei.net

#創建一個文件用於我們待會兒的測試頁
[root@leilei html]# cd www.chenleilei.net/
[root@leilei www.chenleilei.net]# echo "chenleilei.net" >./index.html

#配置完成後 進行語法檢查: nginx -t
nginx: the configuration file /application/nginx-1.16/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16/conf/nginx.conf test is successful

看到出現 is ok  successful  就正確配置了

# 別忘了,在新網站配置文件中指定網站目錄
[root@leilei conf]# vim extra/www.chenleilei.net.conf 

    server {
        listen       80;
        server_name  10.0.0.20;                #指定域名,如果測試環境請寫IP地址
        location / {
            root   html/www.chenleilei.net;    #指定網站目錄       
            index  index.html index.htm;
        }
          error_page    /50x.html;            #錯誤頁面的優雅顯示
}

#配置完成重載配置文件,進行頁面訪問測試
使用: nginx 或 nginx -s reload 重載配置

測試訪問

Nginx服務實踐

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章