在讲诉具体的配置以前,先说下正向代理与反向代理的区别。html
正向代理:是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),而后代理向原始服务器转交请求并将得到的内容返回给客户端。客户端才能使用正向代理,并且必需要进行一些特别的设置才能使用正向代理。。正向代理是一种最终用户知道并主动使用的代理方式。 ios
正向代理的典型用途是为在防火墙内的局域网客户端提供访问Internet的途径。正向代理还可使用缓冲特性减小网络使用率。正向代理容许客户端经过它访问任意网站而且隐藏客户端自身,所以你必须采起安全措施以确保仅为通过受权的客户端提供服务。 nginx
反向代理:是指以代理服务器来接受internet上的链接请求,而后将请求转发给内部网络上的服务器,并将从服务器上获得的结果返回给internet上请求链接的客户端,此时代理服务器对外就表现为一个反向代理服务器。反向代理对用户来讲是透明的,用户是感知不到的。 web
反向代理的典型用途是将防火墙后面的服务器提供给Internet用户访问。反向代理还能够为后端的多台服务器提供负载平衡,或为后端较慢的服务器提供缓冲服务。正则表达式
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; } } }
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; }
$:/usr/local/nginx-1.5.1/sbin/nginx -t
$:/usr/local/nginx-1.5.1/sbin/nginx -s reload
$:/usr/local/nginx-1.5.1/sbin/nginx -s stop
代码示例:后端
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。缓存
语法: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 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的文件。