序言
Nginx是lgor Sysoev為俄羅斯訪問量第二的rambler.ru站點(diǎn)設(shè)計(jì)開發(fā)的。從2004年發(fā)布至今,憑借開源的力量,已經(jīng)接近成熟與完善。
Nginx功能豐富,可作為HTTP服務(wù)器,也可作為反向代理服務(wù)器,郵件服務(wù)器。支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能。并且支持很多第三方的模塊擴(kuò)展。
Nginx的穩(wěn)定性、功能集、示例配置文件和低系統(tǒng)資源的消耗讓他后來居上,在全球活躍的網(wǎng)站中有12.18%的使用比率,大約為2220萬個(gè)網(wǎng)站。
牛逼吹的差不多啦,如果你還不過癮,你可以百度百科或者一些書上找到這樣的夸耀,比比皆是。
Nginx常用功能
1、Http代理,反向代理:作為web服務(wù)器最常用的功能之一,尤其是反向代理。
這里我給來2張圖,對(duì)正向代理與反響代理做個(gè)詮釋,具體細(xì)節(jié),大家可以翻閱下資料。

Nginx在做反向代理時(shí),提供性能穩(wěn)定,并且能夠提供配置靈活的轉(zhuǎn)發(fā)功能。Nginx可以根據(jù)不同的正則匹配,采取不同的轉(zhuǎn)發(fā)策略,比如圖片文件結(jié)尾的走文件服務(wù)器,動(dòng)態(tài)頁面走web服務(wù)器,只要你正則寫的沒問題,又有相對(duì)應(yīng)的服務(wù)器解決方案,你就可以隨心所欲的玩。并且Nginx對(duì)返回結(jié)果進(jìn)行錯(cuò)誤頁跳轉(zhuǎn),異常判斷等。如果被分發(fā)的服務(wù)器存在異常,他可以將請(qǐng)求重新轉(zhuǎn)發(fā)給另外一臺(tái)服務(wù)器,然后自動(dòng)去除異常服務(wù)器。
2、負(fù)載均衡
Nginx提供的負(fù)載均衡策略有2種:內(nèi)置策略和擴(kuò)展策略。內(nèi)置策略為輪詢,加權(quán)輪詢,Ip hash。擴(kuò)展策略,就天馬行空,只有你想不到的沒有他做不到的啦,你可以參照所有的負(fù)載均衡算法,給他一一找出來做下實(shí)現(xiàn)。
上3個(gè)圖,理解這三種負(fù)載均衡算法的實(shí)現(xiàn)

Ip hash算法,對(duì)客戶端請(qǐng)求的ip進(jìn)行hash操作,然后根據(jù)hash結(jié)果將同一個(gè)客戶端ip的請(qǐng)求分發(fā)給同一臺(tái)服務(wù)器進(jìn)行處理,可以解決session不共享的問題。

3、web緩存
Nginx可以對(duì)不同的文件做不同的緩存處理,配置靈活,并且支持FastCGI_Cache,主要用于對(duì)FastCGI的動(dòng)態(tài)程序進(jìn)行緩存。配合著第三方的ngx_cache_purge,對(duì)制定的URL緩存內(nèi)容可以的進(jìn)行增刪管理。
4、Nginx相關(guān)地址
源碼:https://trac.nginx.org/nginx/browser
官網(wǎng):http://www.nginx.org/
Nginx配置文件結(jié)構(gòu)
如果你下載好啦,你的安裝文件,不妨打開conf文件夾的nginx.conf文件,Nginx服務(wù)器的基礎(chǔ)配置,默認(rèn)的配置也存放在此。
在nginx.conf的注釋符號(hào)位#
nginx文件的結(jié)構(gòu),這個(gè)對(duì)剛?cè)腴T的同學(xué),可以多看兩眼。
默認(rèn)的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; # } #}}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注