nginx反向代理原理和配置讲解
2015-11-23 17:49
465 查看
最近有打算研读nginx源代码,看到网上介绍nginx可以作为一个反向代理服务器完成负载均衡。所以搜罗了一些关于反向代理服务器的内容,整理综合。
一概述
反向代理(ReverseProxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。当一个代理服务器能够代理外部网络上的主机,访问内部网络时,这种代理服务的方式称为反向代理服务。
图1反向代理服务器的基本原理
二反向代理服务器的工作原理
反向代理服务器通常有两种模型,它可以作为内容服务器的替身,也可以作为内容服务器集群的负载均衡器。
1,作内容服务器的替身
如果您的内容服务器具有必须保持安全的敏感信息,如信用卡号数据库,可在防火墙外部设置一个代理服务器作为内容服务器的替身。当外部客户机尝试访问内容服务器时,会将其送到代理服务器。实际内容位于内容服务器上,在防火墙内部受到安全保护。代理服务器位于防火墙外部,在客户机看来就像是内容服务器。
当客户机向站点提出请求时,请求将转到代理服务器。然后,代理服务器通过防火墙中的特定通路,将客户机的请求发送到内容服务器。内容服务器再通过该通道将结果回传给代理服务器。代理服务器将检索到的信息发送给客户机,好像代理服务器就是实际的内容服务器(参见图2)。如果内容服务器返回错误消息,代理服务器会先行截取该消息并更改标头中列出的任何URL,然后再将消息发送给客户机。如此可防止外部客户机获取内部内容服务器的重定向URL。
这样,代理服务器就在安全数据库和可能的恶意攻击之间提供了又一道屏障。与有权访问整个数据库的情况相对比,就算是侥幸攻击成功,作恶者充其量也仅限于访问单个事务中所涉及的信息。未经授权的用户无法访问到真正的内容服务器,因为防火墙通路只允许代理服务器有权进行访问。
图2反向代理服务器作为内容服务器的替身
可以配置防火墙路由器,使其只允许特定端口上的特定服务器(在本例中为其所分配端口上的代理服务器)有权通过防火墙进行访问,而不允许其他任何机器进出。
2,作为内容服务器的负载均衡器
可以在一个组织内使用多个代理服务器来平衡各Web服务器间的网络负载。在此模型中,可以利用代理服务器的高速缓存特性,创建一个用于负载平衡的服务器池。此时,代理服务器可以位于防火墙的任意一侧。如果Web服务器每天都会接收大量的请求,则可以使用代理服务器分担Web服务器的负载并提高网络访问效率。
对于客户机发往真正服务器的请求,代理服务器起着中间调停者的作用。代理服务器会将所请求的文档存入高速缓存。如果有不止一个代理服务器,DNS可以采用“循环复用法”选择其IP地址,随机地为请求选择路由。客户机每次都使用同一个URL,但请求所采取的路由每次都可能经过不同的代理服务器。
可以使用多个代理服务器来处理对一个高用量内容服务器的请求,这样做的好处是内容服务器可以处理更高的负载,并且比其独自工作时更有效率。在初始启动期间,代理服务器首次从内容服务器检索文档,此后,对内容服务器的请求数会大大下降。
图3反向代理服务器作为负载均衡器
参考内容:
1,百度百科
2,http://www.oracle.com/technetwork/indexes/documentation/index.html
Chapter:Nginx基本操作释疑
1.Nginx的端口修改问题
2.Nginx301重定向的配置
3.Windows下配置Nginx使之支持PHP
4.Linux下配置Nginx使之支持PHP
5.以源码编译的方式安装PHP与php-fpm
6.Nginx多站点配置的一次实践
7.Nginx反向代理的配置
Nginx作为web服务器一个重要的功能就是反向代理。其实我们在前面的一篇文章《Nginx多站点配置的一次实践》里,用的就是Nginx的反向代理,这里简单再提一下。
下面是配置Nginx作为tornado的反向代理的设置:
Nginx反向代理的指令不需要新增额外的模块,默认自带proxy_pass指令,只需要修改配置文件就可以实现反向代理。
再举一个例子吧。比如要配置后端跑apache服务的ip和端口,也就是说,我们的目标是实现通过http://ip:port能访问到你的网站。
只要新建一个vhost.conf,加入如下内容(记得修改ip和域名为你的ip和域名)。修改nginx.conf,添加includequancha.conf到http{}段,reloadnginx就可以了。
Nginx反向代理模板:
这就完成了Nginx反向代理配置。
------使用过的配置
#usernobody;
worker_processes4;
error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pidlogs/nginx.pid;
worker_rlimit_nofile204800;
events{
worker_connections16384;
multi_accepton;
useepoll;
}
http{
includemime.types;
default_typeapplication/octet-stream;
log_formattest166'$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"'
'"[$request_time]""[$upstream_response_time]"'
'"[$connection]""[$connection_requests]"'
'"$http_imei""$http_mobile""$http_type""$http_key""$cookie_sfpay_jsessionid"';
access_loglogs/access.logtest166;
sendfileon;
#tcp_nopushon;
underscores_in_headerson;
keepalive_timeout65;
proxy_connect_timeout120;
proxy_read_timeout120;
proxy_send_timeout60;
proxy_buffer_size16k;
proxy_buffers464k;
proxy_busy_buffers_size128k;
proxy_temp_file_write_size128k;
proxy_temp_path/home/temp_dir;
proxy_cache_path/home/cachelevels=1:2keys_zone=cache_one:200minactive=1dmax_size=30g;
client_header_buffer_size12k;
open_file_cachemax=204800inactive=65s;
open_file_cache_valid30s;
open_file_cache_min_uses1;
gzipon;
gzip_typestext/plainapplication/x-javascripttext/cssapplication/xmltext/javascriptapplication/x-httpd-phpimage/jpegimage/gifimage/pngimage/jpg;
upstreamims-oms{
server1.1.240.31:8001;
}
upstreamanruy-tomcat{
server1.1.231.54:8080;
server1.1.231.55:8080;
keepalive40;
}
upstreamanruy-tomcat{
server1.1.231.84:8080;
server1.1.231.85:8080;
keepalive40;
}
#HTTPserver
#
server{
listen8080;
server_nameanruy01-sit;
location~(etc/passwd|\.php|\.asp|win.ini)${
denyall;
}
location/nginx_status{
stub_statuson;
access_logoff;
}
location/ims/{
proxy_passhttp://ims-oms/ims/;proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerHost$host;
}
location/anruy/{
proxy_passhttp://anruy-tomcat/anruy/remote/interface;proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerHost$host:1443;
proxy_http_version1.1;
proxy_set_headerConnectionkeep-alive;
proxy_set_headerKeep-Alive600;
keepalive_timeout600;
}
location/anruy-front/{
proxy_passhttp://anruy-tomcat/anruy-front/remote/interface;proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerHost$host:1443;
proxy_http_version1.1;
proxy_set_headerConnectionkeep-alive;
proxy_set_headerKeep-Alive600;
keepalive_timeout600;
}
location/{
roothtml;
indexindex.htmlindex.htm;
}
#client_body_temp_path/usr/local/nginx/html/tmp;
#dav_accessgroup:rwall:r;
#indexindex.htmlindex.htm*.jsp;
#proxy_set_headerX-Real-IP$remote_addr;
#client_max_body_size100m;
}
}
一概述
反向代理(ReverseProxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。当一个代理服务器能够代理外部网络上的主机,访问内部网络时,这种代理服务的方式称为反向代理服务。
图1反向代理服务器的基本原理
二反向代理服务器的工作原理
反向代理服务器通常有两种模型,它可以作为内容服务器的替身,也可以作为内容服务器集群的负载均衡器。
1,作内容服务器的替身
如果您的内容服务器具有必须保持安全的敏感信息,如信用卡号数据库,可在防火墙外部设置一个代理服务器作为内容服务器的替身。当外部客户机尝试访问内容服务器时,会将其送到代理服务器。实际内容位于内容服务器上,在防火墙内部受到安全保护。代理服务器位于防火墙外部,在客户机看来就像是内容服务器。
当客户机向站点提出请求时,请求将转到代理服务器。然后,代理服务器通过防火墙中的特定通路,将客户机的请求发送到内容服务器。内容服务器再通过该通道将结果回传给代理服务器。代理服务器将检索到的信息发送给客户机,好像代理服务器就是实际的内容服务器(参见图2)。如果内容服务器返回错误消息,代理服务器会先行截取该消息并更改标头中列出的任何URL,然后再将消息发送给客户机。如此可防止外部客户机获取内部内容服务器的重定向URL。
这样,代理服务器就在安全数据库和可能的恶意攻击之间提供了又一道屏障。与有权访问整个数据库的情况相对比,就算是侥幸攻击成功,作恶者充其量也仅限于访问单个事务中所涉及的信息。未经授权的用户无法访问到真正的内容服务器,因为防火墙通路只允许代理服务器有权进行访问。
图2反向代理服务器作为内容服务器的替身
可以配置防火墙路由器,使其只允许特定端口上的特定服务器(在本例中为其所分配端口上的代理服务器)有权通过防火墙进行访问,而不允许其他任何机器进出。
2,作为内容服务器的负载均衡器
可以在一个组织内使用多个代理服务器来平衡各Web服务器间的网络负载。在此模型中,可以利用代理服务器的高速缓存特性,创建一个用于负载平衡的服务器池。此时,代理服务器可以位于防火墙的任意一侧。如果Web服务器每天都会接收大量的请求,则可以使用代理服务器分担Web服务器的负载并提高网络访问效率。
对于客户机发往真正服务器的请求,代理服务器起着中间调停者的作用。代理服务器会将所请求的文档存入高速缓存。如果有不止一个代理服务器,DNS可以采用“循环复用法”选择其IP地址,随机地为请求选择路由。客户机每次都使用同一个URL,但请求所采取的路由每次都可能经过不同的代理服务器。
可以使用多个代理服务器来处理对一个高用量内容服务器的请求,这样做的好处是内容服务器可以处理更高的负载,并且比其独自工作时更有效率。在初始启动期间,代理服务器首次从内容服务器检索文档,此后,对内容服务器的请求数会大大下降。
图3反向代理服务器作为负载均衡器
参考内容:
1,百度百科
2,
Chapter:Nginx基本操作释疑
1.
2.
3.
4.
5.
6.
7.
Nginx作为web服务器一个重要的功能就是反向代理。其实我们在前面的一篇文章
下面是配置
01 | upstreamtornado{ |
02 | server127.0.0.1:8888; |
03 | } |
04 |
05 | server{ |
06 | listen80; |
07 | root/root/nmapp2_venv; |
08 | indexindex.pyindex.html; |
09 |
10 | server_nameserver; |
11 |
12 | location/{ |
13 | #if(!-e$request_filename){ |
14 | #rewrite^/(.*)$/index.py/$1last; |
15 | #} |
16 | } |
17 |
18 | location~/index\.py{ |
19 | proxy_pass_headerServer; |
20 | proxy_set_headerHost$http_host; |
21 | proxy_set_headerX-Real-IP$remote_addr; |
22 | proxy_set_headerX-Scheme$scheme; |
23 | proxy_passhttp: //tornado; |
24 | } |
25 | } |
再举一个例子吧。比如要配置后端跑apache服务的ip和端口,也就是说,我们的目标是实现通过
只要新建一个vhost.conf,加入如下内容(记得修改ip和域名为你的ip和域名)。修改nginx.conf,添加includequancha.conf到http{}段,reloadnginx就可以了。
Nginx反向代理模板:
01 | ##Basicreverseproxyserver## |
02 | upstreamapachephp{ |
03 | serverip:8080;#Apache |
04 | } |
05 |
06 | ##Startwww.nowamagic.net## |
07 | server{ |
08 | listen80; |
09 | server_namewww.nowamagic.net; |
10 |
11 | access_loglogs/quancha.access. log main; |
12 | error_loglogs/quancha.error. log ; |
13 | roothtml; |
14 | indexindex.htmlindex.htmindex.php; |
15 |
16 | ##sendrequestbacktoapache## |
17 | location/{ |
18 | proxy_passhttp: //apachephp; |
19 |
20 | #ProxySettings |
21 | proxy_redirectoff; |
22 | proxy_set_headerHost$host; |
23 | proxy_set_headerX-Real-IP$remote_addr; |
24 | proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; |
25 | proxy_next_upstreamerrortimeoutinvalid_headerhttp_500http_502http_503http_504; |
26 | proxy_max_temp_file_size0; |
27 | proxy_connect_timeout90; |
28 | proxy_send_timeout90; |
29 | proxy_read_timeout90; |
30 | proxy_buffer_size4k; |
31 | proxy_buffers432k; |
32 | proxy_busy_buffers_size64k; |
33 | proxy_temp_file_write_size64k; |
34 | } |
35 | } |
------使用过的配置
#usernobody;
worker_processes4;
error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pidlogs/nginx.pid;
worker_rlimit_nofile204800;
events{
worker_connections16384;
multi_accepton;
useepoll;
}
http{
includemime.types;
default_typeapplication/octet-stream;
log_formattest166'$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"'
'"[$request_time]""[$upstream_response_time]"'
'"[$connection]""[$connection_requests]"'
'"$http_imei""$http_mobile""$http_type""$http_key""$cookie_sfpay_jsessionid"';
access_loglogs/access.logtest166;
sendfileon;
#tcp_nopushon;
underscores_in_headerson;
keepalive_timeout65;
proxy_connect_timeout120;
proxy_read_timeout120;
proxy_send_timeout60;
proxy_buffer_size16k;
proxy_buffers464k;
proxy_busy_buffers_size128k;
proxy_temp_file_write_size128k;
proxy_temp_path/home/temp_dir;
proxy_cache_path/home/cachelevels=1:2keys_zone=cache_one:200minactive=1dmax_size=30g;
client_header_buffer_size12k;
open_file_cachemax=204800inactive=65s;
open_file_cache_valid30s;
open_file_cache_min_uses1;
gzipon;
gzip_typestext/plainapplication/x-javascripttext/cssapplication/xmltext/javascriptapplication/x-httpd-phpimage/jpegimage/gifimage/pngimage/jpg;
upstreamims-oms{
server1.1.240.31:8001;
}
upstreamanruy-tomcat{
server1.1.231.54:8080;
server1.1.231.55:8080;
keepalive40;
}
upstreamanruy-tomcat{
server1.1.231.84:8080;
server1.1.231.85:8080;
keepalive40;
}
#HTTPserver
#
server{
listen8080;
server_nameanruy01-sit;
location~(etc/passwd|\.php|\.asp|win.ini)${
denyall;
}
location/nginx_status{
stub_statuson;
access_logoff;
}
location/ims/{
proxy_pass
proxy_set_headerHost$host;
}
location/anruy/{
proxy_pass
proxy_set_headerHost$host:1443;
proxy_http_version1.1;
proxy_set_headerConnectionkeep-alive;
proxy_set_headerKeep-Alive600;
keepalive_timeout600;
}
location/anruy-front/{
proxy_pass
proxy_set_headerHost$host:1443;
proxy_http_version1.1;
proxy_set_headerConnectionkeep-alive;
proxy_set_headerKeep-Alive600;
keepalive_timeout600;
}
location/{
roothtml;
indexindex.htmlindex.htm;
}
#client_body_temp_path/usr/local/nginx/html/tmp;
#dav_accessgroup:rwall:r;
#indexindex.htmlindex.htm*.jsp;
#proxy_set_headerX-Real-IP$remote_addr;
#client_max_body_size100m;
}
}
相关文章推荐
- 使用Squid实现appstore应用离线分发下载(支持ios9)
- Nginx反向代理讲解和配置
- Nginx反向代理配置
- nginx做负载均衡反向代理的配置范例
- 反向代理的概念
- SQUID常用命令
- nginx反向代理及高速缓存
- phpstudy 开启apache反向代理
- nginx搭建反向代理配置并测试
- SSH反向代理实现外网访问内网服务器
- Nginx反向代理
- nginx利用lua实现nginx反向代理proxy_store缓存文件自删除
- 正向代理与反向代理
- Squid代理服务
- 正向代理与反向代理
- squid反向代理加速度内网web服务器访问速度
- 使用NGINX反向代理做小偷站
- 使用Apache2服务器设置反向代理
- Nginx反向代理----配置多个server
- 反向代理(一)