一、正向代理
正向代理,意思是一個位於客戶端和原始服務器(origin server)之間的服務器,為了從原始服務器取得內容,客戶端向代理發送一個請求並指定目標(原始服務器),然后代理向原始服務器轉交請求並將獲得的內容返回給客戶端。客戶端才能使用正向代理。
使用正向代理服務器的作用:
1.1 訪問本無法訪問的服務器B。翻牆、VPN技術的應用
1.2 加速訪問服務器。代理服務器高帶寬,可以加速訪問服務器B
1.3 緩存
1.4 客戶端訪問權限
1.5 隱藏訪問者的行蹤
二、反向代理
客戶端是無感知代理的存在的,反向代理對外都是透明的,訪問者者並不知道自己訪問的是一個代理。因為客戶端不需要任何配置就可以訪問。
反向代理(Reverse Proxy)實際運行方式是指以代理服務器來接受internet上的連接請求,然后將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現為一個服務器。
反向代理的作用:
1.1 保證內網的安全,隱藏原始服務器
1.2 負載均衡。反向代理多個服務器
2.1 反向代理的配置
user nginx; #使用者名稱 worker_processes 1; #進程數,一般設置和cup內核數一樣 #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 www.myvick.cn; #域名 #charset koi8-r; #access_log logs/host.access.log main; #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 $document_root$fastcgi_script_name; include fastcgi_params; } location / { index index.html index.php #默認訪問的文件 root /usr/local/nginx/html #訪問文件的目錄 #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP proxy_set_header Host $host; #默認情況下反向代理是不會轉發請求中的Host頭部的。如果需要轉發,那么必須加上配置 proxy_set_header X-Real-IP $remote_addr; #獲取web服務器端獲得用戶的真實ip proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #配置后,web服務器端通過request.getAttribute("X-Forwarded-For") #獲得的將會是客戶端ip和第一台nginx的ip proxy_method POST #那么客戶端發來的GET請求在轉發時方法名也會改為POST client_max_body_size 10m; #允許客戶端請求的最大單文件字節數 client_body_buffer_size 128k; #緩沖區代理緩沖用戶端請求的最大字節數 proxy_connect_timeout 300; #nginx跟后端服務器連接超時時間(代理連接超時) proxy_send_timeout 300; #后端服務器數據回傳時間(代理發送超時) proxy_read_timeout 300; #連接成功后,后端服務器響應時間(代理接收超時) proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小 proxy_buffers 4 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置 proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳 #禁用緩存 proxy_buffering off; #設置反向代理的地址 proxy_pass http://192.168.1.1; ··} # 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; # } #} }
3.負載均衡
nginx 的 upstream默認是以輪詢的方式實現負載均衡,這種方式中,每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
另外一種方式是ip_hash:每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
upstream myvic{ #ip_hash; server 192.168.1.251 weight=1 fail_timeout=20s; server 192.168.1.252 weight=2 fail_timeout=20s;; server 192.168.1.247 weight=3 fail_timeout=20s;;#可以給服務器加權重 } server { listen 80; server_name www.myvick.cn; location / { #反向代理的地址 proxy_pass http://myvic; } }
第二種配置:ip_hash輪詢方法,不可給服務器加權重
upstream myvic { server 192.168.196.130 fail_timeout=20s; server 192.168.196.132 fail_timeout=20s; ip_hash; } server { listen 80; server_name www.myvick.cn; index index.html index.htm index.php; location / { proxy_pass http://myvic; proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header; #當其中一台返回錯誤碼404,500...等錯誤時,
#可以分配到下一台服務器程序繼續處理,提高平台訪問成功率,多可運用於前台程序負載,設置proxy_next_upstream
proxy_next_upstream off;#關閉請求下一個服務器
include proxy.conf;
}
} 方法二 nginx負載均衡基於ip_hash實現session粘帖
4.Nginx服務器的rewrite功能
地址轉發后客戶端瀏覽器地址欄中的地址顯示是不變的,而地址重寫后地址欄中的地址會變成正確的地址。
在一次地址轉發過程中只會產生一次網絡請求,而一次地址重寫產生兩次請求。
地址轉發一般發生在同一站點項目內,而地址重寫則沒有限制。
地址轉發到的頁面可以不用全路徑名表示,而地址重寫到的頁面必須使用完全的路徑名表示。
地址轉發過程中,可以將客戶端請求的request范圍內的屬性傳遞給新的頁面,但地址重寫不可以。
地址轉發的速度比地址重寫的速度快。
rewrite指令:通過正則表達式的匹配來改變URI,可以同時存在一個或多個指令,按照順序依次對URI進行匹配
redirect:將重寫后的URI返回給客戶端,狀態碼為302,指明是臨時重定向URI,主要用在replacement變量不是以http或https開頭的情況下。
三、透明代理
透明代理的意思是客戶端根本不需要知道有代理服務器的存在,它改編你的request fields(報文),並會傳送真實IP。注意,加密的透明代理則是屬於匿名代理,意思是不用設置使用代理了。
參考文檔:http://blief.blog.51cto.com/6170059/1739178
http://www.cnblogs.com/jacktang/p/3669115.html
