1、功能
功能1:負載均衡:指的是應用不用的調度算法乍恐,將請求轉發(fā)給不同的服務器誉碴。它主要解決并發(fā)的時候請求數(shù)量多多場景幸斥,能提高服務器性能,解決網絡擁堵的問題
功能2:反向代理氛雪,代理的是服務端【正向代理代理的是客戶端】。當網站服務端訪問量太多的時候,我們需要增加服務器樊展。我們在用戶和真實服務器之間增加一個代理服務器呻纹。用戶請求由代理服務器轉發(fā)給真實的應用服務器。這塊的代理服務器我們用的就是nginx专缠,它能處理50000個鏈接(包括活躍鏈接和非活躍鏈接)雷酪。而diango處理的并發(fā)量是2000
功能3:可以作為靜態(tài)資源服務器:存放資源js,ccs,圖片等。通常我們訪問頁面胡時候需要加載靜態(tài)資源和動態(tài)資源涝婉。動態(tài)資源指的是需要在通過業(yè)務服務器在后臺查詢數(shù)據庫哥力,靜態(tài)資源指的椒那些圖片、css文件等墩弯。中小網站我們可以把靜態(tài)文件放到nginx服務器上吩跋,提高網站響應速度。動態(tài)資源則繼續(xù)通過nginx轉發(fā)訪問業(yè)務服務器
niginx的優(yōu)點:
跨平臺渔工,配置簡單锌钮,可移植
內存消耗小
非阻塞高并發(fā)連接
內置的健康檢查:出現(xiàn)服務器宕機時,會做健康檢查引矩,將請求轉發(fā)到正常的節(jié)點
master/worker結構:一個master進程梁丘,生成一個或者多個worker進程
事件驅動:通信機制采用epoll模型
nginx的高可用
大型網站采用主從部署,部署兩個niginx服務器旺韭,當主節(jié)點掛掉或者主節(jié)點的nginx進程或者keepalived掛掉后氛谜,從節(jié)點上的keepalived會檢測到并會接管原先master節(jié)點【主節(jié)點】的網絡功能,這種方式來實現(xiàn)Nginx的高可用区端。
【注】keepalived用來保證集群高可用值漫,防止單點故障。對外體現(xiàn)出來的效果是织盼,兩臺niginx服務器設置一個虛擬機IP惭嚣,通過這個虛擬IP可以訪問到兩個服務器,這兩個服務器一個是主節(jié)點悔政,一個是從節(jié)點晚吞。當檢測到主節(jié)點故障。從升主谋国,主變從
niginx性能問題
2槽地、nginx的配置文件詳解
#user nobody;? ? ? ? 定義nginx運行時候的用戶和用戶組
worker_processes? 1;? ????定義nginx工作的時候拉起多少workers,一般與CPU數(shù)量一致
#error_log? logs/error.log;? ?
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
#pid? ? ? ? logs/nginx.pid; ????????nginx對應的進程ID
events {
? ? worker_connections? 1024;? ?一個nginx進程可以打開的最大文件描述符數(shù)目
}
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;? ? ? 直接往外轉發(fā)圖片捌蚊,不經過用戶空間
? ? #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;
? ? #? ? }
? ? #}
}