[TOC]php
本文檔版權歸屬:陳雷雷,僅限學習交流css
QQ:370460470 blog:www.chenleilei.nethtml
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做爲核心的服務java
2015年排名:linux
2017年排名:nginx
2019年排名web
經過兩張圖咱們能夠清晰發現,apache份額逐步降低,而nginx則穩步上升,因而可知,nginx的發展前景面試
兩年時間兩大龍頭Nginx服務與apache服務佔有率差距從 11%差距縮減到 5%,足以見得Nginx的優秀正則表達式
下面咱們將從多方入手,帶你學習掌握Nginx
- 配置簡單
- 靈活多變
- 負載均衡
- 緩存服務
- 併發更高
- [ ] 可針對靜態資源告訴併發訪問提供數據緩存
- [ ] 可以使用反向代理加速,而且可讓反向代理數據緩存
- [ ] 具備負載均衡,節點健康檢查和容錯功能
- [ ] 支持遠程的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服務軟件
- 做爲web服務軟件
- 做爲反向代理/負載均衡軟件
- 前端業務數據緩存服務(memcache redis mongodb)
nginx的主要應用場景爲 反向代理,和提供web服務
實際工做中咱們須要根據業務的需求來選擇合適的服務軟件,有關web服務建議以下:
- [ ] 靜態業務:若是是高併發場景,儘可能採用Nginx
- [ ] 動態業務: 理論上採用nginx和apache均可以,可是建議選擇nginx,爲了不相同業務的服務軟件多樣化,增長額外的維護成本,動態業務能夠由nginx作前端代理,根據頁面的元素類型或者目錄,轉發到後端的 tomcat
- [ ] 既有靜態業務,又有動態業務,推薦採用nginx
安裝nginx的方式多種多樣,常見的分爲3種
- 經過yum來安裝nginx
- 經過打包好的rpm包方式安裝
- 經過源碼編譯安裝
注意下載頁面最好選擇穩定版:http://nginx.org/en/download.html
這裏咱們選擇 nginx-1.16.0 在標籤上右鍵複製下載地址
#第一步: 下載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 -t 檢查配置文件語法
(基於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 />參考:http://www.javashuo.com/article/p-ejfdljzs-bm.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 />參考:http://www.javashuo.com/article/p-tnksqdlx-du.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 />參考:http://www.javashuo.com/article/p-bmiixvlj-bq.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 | 啓用POP三、IMAP四、SMTP代理模塊 | 啓用郵件代理 | 不啓用 |
--with-mail=dynamic | 啓用POP三、IMAP四、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 -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 -s stop 中止nginx
nginx -s start 啓動nginx
nginx -s reload 重載配置文件
在面對多站點的配置的時候,每一個站點配置都在 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 重載配置