出自:https://www.cnblogs.com/knowledgesea/p/5175711.htmlphp
Nginx是lgor Sysoev爲俄羅斯訪問量第二的rambler.ru站點設計開發的。從2004年發佈至今,憑藉開源的力量,已經接近成熟與完善。html
Nginx功能豐富,可做爲HTTP服務器,也可做爲反向代理服務器,郵件服務器。支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能。而且支持不少第三方的模塊擴展。nginx
Nginx的穩定性、功能集、示例配置文件和低系統資源的消耗讓他後來居上,在全球活躍的網站中有12.18%的使用比率,大約爲2220萬個網站。web
牛逼吹的差很少啦,若是你還不過癮,你能夠百度百科或者一些書上找到這樣的誇耀,比比皆是。算法
一、Http代理,反向代理:做爲web服務器最經常使用的功能之一,尤爲是反向代理。後端
這裏我給來2張圖,對正向代理與反響代理作個詮釋,具體細節,你們能夠翻閱下資料。瀏覽器
Nginx在作反向代理時,提供性能穩定,而且可以提供配置靈活的轉發功能。Nginx能夠根據不一樣的正則匹配,採起不一樣的轉發策略,好比圖片文件結尾的走文件服務器,動態頁面走web服務器,只要你正則寫的沒問題,又有相對應的服務器解決方案,你就能夠爲所欲爲的玩。而且Nginx對返回結果進行錯誤頁跳轉,異常判斷等。若是被分發的服務器存在異常,他能夠將請求從新轉發給另一臺服務器,而後自動去除異常服務器。緩存
二、負載均衡服務器
Nginx提供的負載均衡策略有2種:內置策略和擴展策略。內置策略爲輪詢,加權輪詢,Ip hash。擴展策略,就天馬行空,只有你想不到的沒有他作不到的啦,你能夠參照全部的負載均衡算法,給他一一找出來作下實現。網絡
上3個圖,理解這三種負載均衡算法的實現
Ip hash算法,對客戶端請求的ip進行hash操做,而後根據hash結果將同一個客戶端ip的請求分發給同一臺服務器進行處理,能夠解決session不共享的問題。
三、web緩存
Nginx能夠對不一樣的文件作不一樣的緩存處理,配置靈活,而且支持FastCGI_Cache,主要用於對FastCGI的動態程序進行緩存。配合着第三方的ngx_cache_purge,對制定的URL緩存內容能夠的進行增刪管理。
四、Nginx相關地址
源碼:https://trac.nginx.org/nginx/browser
官網:http://www.nginx.org/
若是你下載好啦,你的安裝文件,不妨打開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全局塊
}
一、全局塊:配置影響nginx全局的指令。通常有運行nginx服務器的用戶組,nginx進程pid存放路徑,日誌存放路徑,配置文件引入,容許生成worker process數等。
二、events塊:配置影響nginx服務器或與用戶的網絡鏈接。有每一個進程的最大鏈接數,選取哪一種事件驅動模型處理鏈接請求,是否容許同時接受多個網路鏈接,開啓多個網絡鏈接序列化等。
三、http塊:能夠嵌套多個server,配置代理,緩存,日誌定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日誌自定義,是否使用sendfile傳輸文件,鏈接超時時間,單鏈接請求數等。
四、server塊:配置虛擬主機的相關參數,一個http中能夠有多個server。
五、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塊。
#負載均衡-輪詢(默認)、指定權重、ip_hash 、fair(第三方)、url_hash(第三方)、
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.$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 :記錄客戶端瀏覽器的相關信息;
二、驚羣現象:一個網路鏈接到來,多個睡眠的進程被同事叫醒,但只有一個進程能得到連接,這樣會影響系統性能。
三、每一個指令必須有分號結束。
nginx的upstream目前支持的5種方式的分配
一、輪詢(默認)
每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。
upstream backserver {
server 192.168.0.10;
server 192.168.0.20;
}
二、指定權重
指定輪詢概率,weight和訪問比率成正比,用於後端服務器性能不均的狀況。
upstream backserver {
server 192.168.0.10 weight=10;
server 192.168.0.20 weight=10;
}
三、IP綁定 ip_hash (鵬雲留學正在使用此模式)
每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。
upstream backserver {
ip_hash;
server 192.168.0.10:88;
server 192.168.0.20:80;
}
四、fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream backserver {
server server1;
server server2;
fair;
}
五、url_hash(第三方)
按訪問url的hash結果來分配請求,使每一個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
在須要使用負載均衡的server中增長
proxy_pass http://backserver/;
upstream backserver{
ip_hash;
server 127.0.0.1:9090 down; (down 表示當前的server暫時不參與負載)
server 127.0.0.1:8080 weight=2; (weight 默認爲1.weight越大,負載的權重就越大)
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup; (其它全部的非backup機器down或者忙的時候,請求backup機器)
}
max_fails :容許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
fail_timeout:max_fails次失敗後,暫停的時間
補充說明:
https請求配置
#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 {
#zzc tianjia
client_max_body_size 20m;
#zzc tainjia
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;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
upstream backserver {
ip_hash;
server 192.168.1.200:8090;
server 192.168.1.189:8090;
}
server {
listen 8899;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /{
root /opt/htmldata;
index index.html index.htm;
}
}
server { listen 443 ssl; server_name localhost; ssl on; index index.html index.htm; if ($request_method !~* GET|POST|HEAD) { return 403; } valid_referers none blocked server_names *.jffw.cn test.jffw.*; if ($invalid_referer) { return 403; } ssl_certificate /usr/local/sslkey/ssl.crt; ssl_certificate_key /usr/local/sslkey/ssl.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; # 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 /{ proxy_pass http://backserver/; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header Host $host; proxy_set_header Remote_Addr $remote_addr; proxy_set_header X-Real-IP $remote_addr; } }
}