1.nginx反向代理
在講訴具體的配置之前,先說下正向代理與反向代理的區別。
正向代理:是一個位於客戶端和原始服務器(origin server)之間的服務器,為了從原始服務器取得內容,客戶端向代理發送一個請求並指定目標(原始服務器),然后代理向原始服務器轉交請求並將獲得的內容返回給客戶端。客戶端才能使用正向代理,而且必須要進行一些特別的設置才能使用正向代理。。正向代理是一種最終用戶知道並主動使用的代理方式。
正向代理的典型用途是為在防火牆內的局域網客戶端提供訪問Internet的途徑。正向代理還可以使用緩沖特性減少網絡使用率。正向代理允許客戶端通過它訪問任意網站並且隱藏客戶端自身,因此你必須采取安全措施以確保僅為經過授權的客戶端提供服務。
反向代理:是指以代理服務器來接受internet上的連接請求,然后將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現為一個反向代理服務器。反向代理對用戶來說是透明的,用戶是感知不到的。
反向代理的典型用途是將防火牆后面的服務器提供給Internet用戶訪問。反向代理還可以為后端的多台服務器提供負載平衡,或為后端較慢的服務器提供緩沖服務。
修改配置文件
/usr/local/nginx/conf/nginx.conf
user www www; worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; include /usr/local/nginx/conf/vhosts/proxy.conf; sendfile on; keepalive_timeout 65; gzip on; client_max_body_size 50m; #緩沖區代理緩沖用戶端請求的最大字節數,可以理解為保存到本地再傳給用戶 client_body_buffer_size 256k; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; proxy_connect_timeout 300s; #nginx跟后端服務器連接超時時間(代理連接超時) proxy_read_timeout 300s; #連接成功后,后端服務器響應時間(代理接收超時) proxy_send_timeout 300s; proxy_buffer_size 64k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小 proxy_buffers 4 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置 proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳遞請求,而不緩沖到磁盤 proxy_ignore_client_abort on; #不允許代理端主動關閉連接 server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
編輯反向代理服務器配置文件:
/usr/local/nginx/conf/vhosts/proxy.conf
server { listen 80; server_name jerishi1.com; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://192.168.10.38:3000; } access_log logs/jerishi1.com_access.log; } server { listen 80; server_name jerishi2.com; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://192.168.10.40:80; } access_log logs/jerishi2.com_access.log; }
負載均衡設置
upstream monitor_server { server 192.168.0.1:80; server 192.168.0.2:80; } server { listen 80; server_name nagios.xxx123.tk; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://monitor_server; } access_log logs/nagios.jerishi.com_access.log; }
2.ngnix重啟
檢查配置
$:/usr/local/nginx-1.5.1/sbin/nginx -t
重啟nginx
$:/usr/local/nginx-1.5.1/sbin/nginx -s reload
關閉nginx
$:/usr/local/nginx-1.5.1/sbin/nginx -s stop
3.Location處理邏輯
location匹配命令
- ~ #波浪線表示執行一個正則匹配,區分大小寫
- ~* #表示執行一個正則匹配,不區分大小寫
- ^~ #^~表示普通字符匹配,如果該選項匹配,只匹配該選項,不匹配別的選項,一般用來匹配目錄
- = #進行普通字符精確匹配
- @ #”@” 定義一個命名的 location,使用在內部定向時,例如 error_page, try_files
代碼示例:
location = / { # 只匹配"/". [ configuration A ] } location / { # 匹配任何請求,因為所有請求都是以"/"開始 # 但是更長字符匹配或者正則表達式匹配會優先匹配 [ configuration B ] } location ^~ /images/ { # 匹配任何以 /images/ 開始的請求,並停止匹配 其它location [ configuration C ] } location ~* .(gif|jpg|jpeg)$ { # 匹配以 gif, jpg, or jpeg結尾的請求. # 但是所有 /images/ 目錄的請求將由 [Configuration C]處理. [ configuration D ] }
匹配流程:
1. 用uri測試所有的prefix string;
2. Uri精確匹配到=定義的loacation,使用這個location,停止搜索;
3. 匹配最長prefix string,如果這個最長prefix string帶有^~修飾符,使用這個location,停止搜索,否則:
4. 存儲這個最長匹配;
5. 然后匹配正則表達;
6. 匹配到第一條正則表達式,使用這個location,停止搜索;
7. 沒有匹配到正則表達式,使用#4步存儲的prefix string的location。
4.nginx root&alias比較
root
語法:root path
默認值:root html
配置段:http、server、location、if
root會根據完整的URI請求來映射,也就是/path/uri.
示例:
location ~ ^/qcloud/ { root /data/release/www.qcloud.com; autoindex on; auth_basic "Restricted"; auth_basic_user_file passwd/weblogs; }
如果一個請求的URI是/qcloud/www.buy.qcloud.com/main.js時,web服務器將會返回/data/release/www.qcloud.com/qcloud/www.buy.qcloud.com/main.js的文件。
alias
語法:alias path
配置段:location
alias會把location后面配置的路徑丟棄掉,把當前匹配到的目錄指向到指定的目錄。
示例:
location ~ ^/qcloud/ { alias /data/release/www.qcloud.com; autoindex on; auth_basic "Restricted"; auth_basic_user_file passwd/weblogs; }
如果一個請求的URI是/qcloud/www.buy.qcloud.com/user.js時,web服務器將會返回/data/release/www.qcloud.com/www.buy.qcloud.com/main.js的文件。
