序言
Nginx是lgor Sysoev為俄羅斯訪問量第二的rambler.ru站點設計開發的。從2004年發布至今,憑借開源的力量,已經接近成熟與完善。
Nginx功能豐富,可作為HTTP服務器,也可作為反向代理服務器,郵件服務器。支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能。并且支持很多第三方的模塊擴展。
Nginx的穩定性、功能集、示例配置文件和低系統資源的消耗讓他后來居上,在全球活躍的網站中有12.18%的使用比率,大約為2220萬個網站。
牛逼吹的差不多啦,如果你還不過癮,你可以百度百科或者一些書上找到這樣的夸耀,比比皆是。
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相關地址
源碼:https://trac.nginx.org/nginx/browser
官網:http://www.nginx.org/
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; # } #}}
新聞熱點
疑難解答