nginx常用功能
1、http代理,反向代理:作為web服務器最常用的功能之一,尤其是反向代理。
這里我給來2張圖,對正向代理與反響代理做個詮釋,具體細節,大家可以翻閱下資料。
nginx在做反向代理時,提供性能穩定,并且能夠提供配置靈活的轉發功能。nginx可以根據不同的正則匹配,采取不同的轉發策略,比如圖片文件結尾的走文件服務器,動態頁面走web服務器,只要你正則寫的沒問題,又有相對應的服務器解決方案,你就可以隨心所欲的玩。并且nginx對返回結果進行錯誤頁跳轉,異常判斷等。如果被分發的服務器存在異常,他可以將請求重新轉發給另外一臺服務器,然后自動去除異常服務器。
2、負載均衡
nginx提供的負載均衡策略有2種:內置策略和擴展策略。內置策略為輪詢,加權輪詢,ip hash。擴展策略,就天馬行空,只有你想不到的沒有他做不到的啦,你可以參照所有的負載均衡算法,給他一一找出來做下實現。
上3個圖,理解這三種負載均衡算法的實現
ip hash算法,對客戶端請求的ip進行hash操作,然后根據hash結果將同一個客戶端ip的請求分發給同一臺服務器進行處理,可以解決session不共享的問題。
3、web緩存
nginx可以對不同的文件做不同的緩存處理,配置靈活,并且支持fastcgi_cache,主要用于對fastcgi的動態程序進行緩存。配合著第三方的ngx_cache_purge,對制定的url緩存內容可以的進行增刪管理。
4、nginx相關地址
源碼:
官網:
nginx配置文件結構
如果你下載好啦,你的安裝文件,不妨打開conf文件夾的nginx.conf文件,nginx服務器的基礎配置,默認的配置也存放在此。
在nginx.conf的注釋符號位#
nginx文件的結構,這個對剛入門的同學,可以多看兩眼。
默認的config
#user?nobody; worker_processes?1; #error_log?logs/error.log; #error_log?logs/error.log?notice; #error_log?logs/error.log?info; #pid????logs/nginx.pid; events?{ ??worker_connections?1024; } http?{ ??include????mime.types; ??default_type?application/octet-stream; ??#log_format?main?'$remote_addr?-?$remote_user?[$time_local]?"$request"?' ??#?????????'$status?$body_bytes_sent?"$http_referer"?' ??#?????????'"$http_user_agent"?"$http_x_forwarded_for"'; ??#access_log?logs/access.log?main; ??sendfile????on; ??#tcp_nopush???on; ??#keepalive_timeout?0; ??keepalive_timeout?65; ??#gzip?on; ??server?{ ????listen????80; ????server_name?localhost; ????#charset?koi8-r; ????#access_log?logs/host.access.log?main; ????location?/?{ ??????root??html; ??????index?index.html?index.htm; ????} ????#error_page?404???????/404.html; ????#?redirect?server?error?pages?to?the?static?page?/50x.html ????# ????error_page??500?502?503?504?/50x.html; ????location?=?/50x.html?{ ??????root??html; ????} ????#?proxy?the?php?scripts?to?apache?listening?on?127.0.0.1:80 ????# ????#location?~?.php$?{ ????#??proxy_pass??http://127.0.0.1; ????#} ????#?pass?the?php?scripts?to?fastcgi?server?listening?on?127.0.0.1:9000 ????# ????#location?~?.php$?{ ????#??root??????html; ????#??fastcgi_pass??127.0.0.1:9000; ????#??fastcgi_index?index.php; ????#??fastcgi_param?script_filename?/scripts$fastcgi_script_name; ????#??include????fastcgi_params; ????#} ????#?deny?access?to?.htaccess?files,?if?apache's?document?root ????#?concurs?with?nginx's?one ????# ????#location?~?/.ht?{ ????#??deny?all; ????#} ??} ??#?another?virtual?host?using?mix?of?ip-,?name-,?and?port-based?configuration ??# ??#server?{ ??#??listen????8000; ??#??listen????somename:8080; ??#??server_name?somename?alias?another.alias; ??#??location?/?{ ??#????root??html; ??#????index?index.html?index.htm; ??#??} ??#} ??#?https?server ??# ??#server?{ ??#??listen????443?ssl; ??#??server_name?localhost; ??#??ssl_certificate???cert.pem; ??#??ssl_certificate_key?cert.key; ??#??ssl_session_cache??shared:ssl:1m; ??#??ssl_session_timeout?5m; ??#??ssl_ciphers?high:!anull:!md5; ??#??ssl_prefer_server_ciphers?on; ??#??location?/?{ ??#????root??html; ??#????index?index.html?index.htm; ??#??} ??#} }
nginx文件結構
...???????#全局塊 events?{?????#events塊 ??... } http???#http塊 { ??...??#http全局塊 ??server????#server塊 ??{? ????...????#server全局塊 ????location?[pattern]??#location塊 ????{ ??????... ????} ????location?[pattern]? ????{ ??????... ????} ??} ??server ??{ ???... ??} ??...???#http全局塊 }
1、全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日志存放路徑,配置文件引入,允許生成worker process數等。
2、events塊:配置影響nginx服務器或與用戶的網絡連接。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啟多個網絡連接序列化等。
3、http塊:可以嵌套多個server,配置代理,緩存,日志定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日志自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等。
4、server塊:配置虛擬主機的相關參數,一個http中可以有多個server。
5、location塊:配置請求的路由,以及各種頁面的處理情況。
下面給大家上一個配置文件,作為理解,同時也配入我搭建的一臺測試機中,給大家示例。
###########?每個指令必須有分號結束。################# #user?administrator?administrators;?#配置用戶或者組,默認為nobody?nobody。 #worker_processes?2;?#允許生成的進程數,默認為1 #pid?/nginx/pid/nginx.pid;??#指定nginx進程運行文件存放地址 error_log?log/error.log?debug;?#制定日志路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg events?{ ??accept_mutex?on;??#設置網路連接序列化,防止驚群現象發生,默認為on ??multi_accept?on;?#設置一個進程是否同時接受多個網絡連接,默認為off ??#use?epoll;???#事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport ??worker_connections?1024;??#最大連接數,默認為512 } http?{ ??include????mime.types;??#文件擴展名與文件類型映射表 ??default_type?application/octet-stream;?#默認文件類型,默認為text/plain ??#access_log?off;?#取消服務日志?? ??log_format?myformat?'$remote_addr–$remote_user?[$time_local]?$request?$status?$body_bytes_sent?$http_referer?$http_user_agent?$http_x_forwarded_for';?#自定義格式 ??access_log?log/access.log?myformat;?#combined為日志格式的默認值 ??sendfile?on;??#允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。 ??sendfile_max_chunk?100k;?#每個進程每次調用傳輸數量不能大于設定的值,默認為0,即不設上限。 ??keepalive_timeout?65;?#連接超時時間,默認為75s,可以在http,server,location塊。 ??upstream?mysvr?{?? ???server?127.0.0.1:7878; ???server?192.168.10.121:3333?backup;?#熱備 ??} ??error_page?404?https://www.baidu.com;?#錯誤頁 ??server?{ ????keepalive_requests?120;?#單連接請求上限次數。 ????listen????4545;??#監聽端口 ????server_name?127.0.0.1;??#監聽地址???? ????location?~*^.+$?{????#請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。 ??????#root?path;?#根目錄 ??????#index?vv.txt;?#設置默認頁 ??????proxy_pass?http://mysvr;?#請求轉向mysvr?定義的服務器列表 ??????deny?127.0.0.1;?#拒絕的ip ??????allow?172.18.5.54;?#允許的ip?????? ????}? ??} }
上面是nginx的基本配置,需要注意的有以下幾點:
1、1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址; 2.$remote_user :用來記錄客戶端用戶名稱; 3.$time_local : 用來記錄訪問時間與時區;4.$request : 用來記錄請求的url與http協議;
? 5.$status : 用來記錄請求狀態;成功是200, 6.$body_bytes_s ent :記錄發送給客戶端文件主體內容大小;7.$http_referer :用來記錄從那個頁面鏈接訪問過來的; 8.$http_user_agent :記錄客戶端瀏覽器的相關信息;
2、驚群現象:一個網路連接到來,多個睡眠的進程被同事叫醒,但只有一個進程能獲得鏈接,這樣會影響系統性能。
3、每個指令必須有分號結束。