反向代理原理反向代理服务器配置解决访问加速

简介: 基本原理: 用户A始终认为它访问的是原始服务器B而不是代理服务器Z,但实用际上反向代理服务器接受用户A的应答,从原始资源服务器B中取得用户A的需求资源,然后发送给用户A。由于防火墙的作用,只允许代理服务器Z访问原始资源服务器B。尽管在这个虚拟的环境下,防火墙和反向代理的共同作用保护了原始资源服务器B,但用户A并不知情。 ps:简单的说,用户A所请求的和响应全由代理服务器Z和真实的服务器

基本原理:

用户A始终认为它访问的是原始服务器B而不是代理服务器Z,但实用际上反向代理服务器接受用户A的应答,从原始资源服务器B中取得用户A的需求资源,然后发送给用户A。由于防火墙的作用,只允许代理服务器Z访问原始资源服务器B。尽管在这个虚拟的环境下,防火墙和反向代理的共同作用保护了原始资源服务器B,但用户A并不知情。

ps:简单的说,用户A所请求的和响应全由代理服务器Z和真实的服务器B做了代理工作



解决使用单线程下nginx反向代理服务器配置(网络资料提供参考,原文:http://www.jb51.net/article/38046.htm):

linux下通过Nginx反向代理和proxy_cache缓存搭建CDN服务器加快Web访问速度的配置方法

问题场景:
移动用户访问web服务器www.osyunwei.com很慢
解决办法:
1、在移动机房放置一台nginx反向代理服务器
2、通过域名DNS智能解析,所有移动用户访问www.osyunwei.com时解析到nginx反向代理服务器
3、nginx反向代理服务器与web服务器之间采用专线连接
说明:
1、web服务器
线路:电信
IP:192.168.21.129
域名:www.osyunwei.com
2、nginx反向代理服务器
线路:移动
系统:CentOS 6.2
IP:192.168.21.164
vi /etc/hosts #编辑,在文件最后添加下面一行
192.168.21.129 www.osyunwei.com
3、客户端
线路:移动
系统:Windows 7
IP:192.168.21.130
C:\Windows\System32\drivers\etc\hosts #用记事本打开,在文件最后添加下面一行
192.168.21.164 www.osyunwei.com


###################以下操作在nginx反向代理服务器上配置###################


1、关闭SELinux
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq 保存,关闭。
shutdown -r now重启系统
2、开启防火墙80端口
vi /etc/sysconfig/iptables
添加下面的内容
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables restart #重启防火墙使配置生效
3、安装编译工具
yum install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl
4 、系统约定
软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字
5、下载软件
cd /usr/local/src #进入目录
(一)、下载nginx(目前稳定版)
wget http://nginx.org/download/nginx-1.0.12.tar.gz
(二)、下载pcre (支持nginx伪静态)
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
(二)、下载ngx_cache_purge(清除指定URL缓存)
wget http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz
6、安装pcre
cd /usr/local/src
mkdir /usr/local/pcre #创建安装目录
tar zxvf pcre-8.21.tar.gz
cd pcre-8.21
./configure --prefix=/usr/local/pcre #配置
make
make install
7、安装 nginx
groupadd www #添加www组
useradd -g www www -s /bin/false #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统
cd /usr/local/src
tar zxvf ngx_cache_purge-1.5.tar.gz
tar zxvf nginx-1.0.12.tar.gz
cd nginx-1.0.12
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.21 --add-module=../ngx_cache_purge-1.5
注意:--with-pcre=/usr/local/src/pcre-8.21指向的是源码包解压的路径,而不是安装的路径,否则会报错
make #编译
make install #安装
/usr/local/nginx/sbin/nginx #启动nginx
chown www.www -R /usr/local/nginx/html #设置目录所有者
chmod 700 -R /usr/local/nginx/html #设置目录权限
vi /etc/rc.d/init.d/nginx # 设置nginx开启启动,编辑启动文件添加下面内容
=======================================================
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start

stop)
stop

reload)
reload

restart)
stop
start
;;

status)
status $prog
RETVAL=$?

*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
=======================================================
:wq!保存退出
chmod 775 /etc/rc.d/init.d/nginx #赋予文件执行权限
chkconfig nginx on #设置开机启动
/etc/rc.d/init.d/nginx restart
service nginx restart
8、配置nginx
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.confbak #备份nginx配置文件
(一)、设置nginx运行账号
vi /usr/local/nginx/conf/nginx.conf #编辑
找到user nobody;修改为
user www www; #在第一行
(二)、禁止nginx空主机头
vi /usr/local/nginx/conf/nginx.conf #编辑
找到server,在上面一行添加如下内容:
##############################
server {
listen 80 default;
server_name _;
location / {
root html;
return 404;
}
location ~ /.ht {
deny all;
}
}
##############################
/etc/rc.d/init.d/nginx restart #重启nginx
这样设置之后,空主机头访问会直接跳转到nginx404错误页面。
(三)、添加nginx虚拟主机包含文件
cd /usr/local/nginx/conf/ #进入nginx安装目录
mkdir vhost #建立虚拟目录
vi /usr/local/nginx/conf/nginx.conf #编辑
找到上一步添加的代码,在最后添加如下内容:
include vhost/*.conf;
例如:
##############################
server {
listen 80 default;
server_name _;
location / {
root html;
return 404;
}
location ~ /.ht {
deny all;
}
}
include vhost/*.conf;
##############################
(四)、添加proxy_cache参数配置包含文件
cd /usr/local/nginx/conf/ #进入目录
touch proxy.conf #建立文件
vi /usr/local/nginx/conf/nginx.conf #编辑
找到http { 在下面添加一行
include proxy.conf;
(五)、添加被代理服务器列表包含文件
cd /usr/local/nginx/conf/ #进入目录
touch mysvrhost.conf #建立文件
vi /usr/local/nginx/conf/nginx.conf #编辑
找到上一步添加的代码,在下面添加一行
include mysvrhost.conf;
(六)、设置nginx全局参数
vi /usr/local/nginx/conf/nginx.conf #编辑 
worker_processes 2; # 工作进程数,为CPU的核心数或者两倍
events
{
use epoll; #增加
worker_connections 65535; #修改为65535,最大连接数。
}
#############以下代码在http { 部分增加与修改##############
server_names_hash_bucket_size 128; #增加
client_header_buffer_size 32k; #增加
large_client_header_buffers 4 32k; #增加
client_max_body_size 300m; #增加
tcp_nopush on; #修改为on
keepalive_timeout 60; #修改为60
tcp_nodelay on; #增加
server_tokens off; #增加,不显示nginx版本信息
gzip on; #修改为on
gzip_min_length 1k; #增加
gzip_buffers 4 16k; #增加
gzip_http_version 1.1; #增加
gzip_comp_level 2; #增加
gzip_types text/plain application/x-javascript text/css application/xml; #增加
gzip_vary on; #增加
(七)、设置proxy_cache参数配置
cd /home #进入目录
mkdir -p /home/proxy_temp_dir #proxy_temp_dir与proxy_cache_dir这两个文件夹必须在同一个分区
mkdir -p /home/proxy_cache_dir #proxy_cache_dir与proxy_temp_dir这两个文件夹必须在同一个分区
chown www.www -R proxy_cache_dir proxy_temp_dir #设置目录所有者
chmod -R 777 proxy_cache_dir proxy_temp_dir #设置目录权限
系统运维 www.osyunwei.com 温馨提醒:qihang01原创内容©版权所有,转载请注明出处及原文链
cd /usr/local/nginx/conf/ #进入目录
vi proxy.conf #编辑,添加以下代码
proxy_temp_path /home/proxy_temp_dir; #指定临时文件目录
proxy_cache_path /home/proxy_cache_dir levels=1:2 keys_zone=cache_one:50m inactive=1d max_size=1g;
#设置Web缓存区名称为cache_one,内存缓存为50MB,自动清除1天内没有被访问的文件,硬盘缓存为1GB。
client_body_buffer_size 512k; #增加缓冲区代理缓冲客户端请求的最大字节数
proxy_connect_timeout 60; #增加连接后端服务器超时时间
proxy_read_timeout 60; #增加后端服务器响应请求超时时间
proxy_send_timeout 60; #增加后端服务器发送数据超时时间
proxy_buffer_size 32k; #增加代理请求缓存区大小
proxy_buffers 4 64k; #增加
proxy_busy_buffers_size 128k; #增加系统繁忙时可申请的proxy_buffers大小
proxy_temp_file_write_size 128k; #增加proxy缓存临时文件的大小
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; #增加故障转移,如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。proxy_cache cache_one; #增加使用web缓存区cache_one
(八)、设置被代理服务器文件列表
cd /usr/local/nginx/conf/ #进入目录
vi mysvrhost.conf #编辑,添加以下代码
upstream osyunweihost {
server 192.168.21.129:80 weight=1 max_fails=2 fail_timeout=30s;
}
(九)、新建虚拟主机配置文件
cd /usr/local/nginx/conf/vhost #进入虚拟主机目录
touch www.osyunwei.com.conf #建立虚拟主机配置文件
vi www.osyunwei.com.conf #编辑

server {
listen 80;
server_name www.osyunwei.com osyunwei.com;

location /
{
proxy_pass http://osyunweihost;
proxy_cache_key $host$uri$is_args$args; #增加设置web缓存的key值,nginx根据key值md5哈希存储缓存
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_cache_valid 200 304 12h;
expires 2d;
}
location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$ #列出的扩展名文件不缓存。

{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://osyunweihost;
}
access_log off;
}

location ~ /purge(/.*) #用于清除缓存
{
allow 127.0.0.1;
allow 192.168.21.0/24; #设置只允许指定的IP或IP段才可以清除URL缓存。
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
###################以上操作在nginx反向代理服务器上配置###################
9、ngx_cache_pure清除缓存模块使用说明
说明:根据配置只允许192.168.21.0/24 IP段的主机才可以清除URL缓存,现在我使用的客户机IP是:192.168.21.130,有权限清除URL缓存。

1、浏览图片文件:http://www.osyunwei.com/images/nopic.gif

2、清除这个文件缓存:http://www.osyunwei.com/purge/images/nopic.gif

提示:Successful purge,缓存文件清除成功,如果这个文件没有被缓存过,则提示:404 Not Found

备注:
1、purge是ngx_cache_pure 模块指令
2、images/nopic.gif 是要清除的缓存文件URL路径

至此,使用Nginx反向代理和proxy_cache缓存功能配置CDN服务器教程结束。

附件:

1、nginx配置文件/usr/local/nginx/conf/nginx.conf

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
user www www;
worker_processes 2;
# error_log logs/error.log;
# error_log logs/error.log notice;
# error_log logs/error.log info;
#pid logs/nginx.pid;
 
events {
use epoll;
worker_connections 65535;
}
 
http {
include proxy.conf;
include mysvrhost.conf;
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;
 
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
 
#keepalive_timeout 0;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
 
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
 
server {
listen 80 default ;
server_name _;
location / {
root html;
return 404;
}
location ~ /.ht {
deny all;
}
}
include vhost/*.conf;
}

2、被代理服务器列表文件/usr/local/nginx/conf/mysvrhost.conf

?
1
2
3
upstream osyunweihost {
server 192.168.21.129:80 weight=1 max_fails=2 fail_timeout=30s;
}

3、proxy_cache参数配置文件/usr/local/nginx/conf/proxy.conf

?
1
2
3
4
5
6
7
8
9
10
11
12
proxy_temp_path /home/proxy_temp_dir;
proxy_cache_path /home/proxy_cache_dir levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;
client_body_buffer_size 512k;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_cache cache_one;

4、虚拟主机配置文件/usr/local/nginx/conf/vhost/www.osyunwei.com.conf

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
server {
listen 80;
server_name www.osyunwei.com osyunwei.com;
location /
{
proxy_pass http: //osyunweihost;
proxy_cache_key $host$uri$is_args$args ;
proxy_set_header Host $host ;
proxy_set_header X-Forwarded-For $remote_addr ;
proxy_cache_valid 200 304 12h;
expires 2d;
}
 
location ~ /purge(/.*)
{
allow 127.0.0.1;
allow 192.168.21.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args ;
}
 
location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$
{
proxy_set_header Host $host ;
proxy_set_header X-Forwarded-For $remote_addr ;
proxy_pass http: //osyunweihost;
}
access_log off;
}

扩展阅读:
#################################################################
nginx修改版本等信息
vi /usr/local/src/nginx-1.0.12/src/core/nginx.h #编译前编辑
#define nginx_version
#define NGINX_VERSION
#define NGINX_VER
#define NGINX_VAR
修改上面的信息,即可更改nginx显示版本。
vi /usr/local/src/http/ngx_http_special_response.c #编译前编辑
static u_char ngx_http_error_full_tail[] =
static u_char ngx_http_error_tail[] =
修改上面的信息为你自己的。
#################################################################


 


其他解决方案:
抛弃Nginx使用nodejs做反向代理服务器

 时下不少场景,都是申请一个 VPS 主机来托管运行 Web 项目的,小弟我也不例外,购买了一个小型的 Win 03 VPS 使用着。在使用的过程中,面临一个问题,就是同一类型的服务端环境还好——但如果是一个 PHP、一个 ASP、 一个 JSP 的三种类型的服务端项目并存着,该怎么分配唯一的 80 端口呢?因为商业 WWW 网站的话,往往只能占用 80  端口,——当然,如果只是做服务的话,如接口之类的,使用其他端口就不会与 80 端口冲突了。许多开发者都会面临到 80 端口这个问题,并且实际情况会受到成本的限制。因为单独为一个项目就买一个 VPS,也不太经济、不太合算,管理起来也不方便。于是,我们就应该好好考虑一下,怎么在提供一个 80 端口的情况下,分发到多种服务端那里去,让不同的主机执行各自的 Web 项目。
亲,那这项需求我们说可以实现吗?是的,这并不是什么“神奇的技术”,也不是什么复杂的技术。不知你是否有了解,网络服务中的“反向代理(Reverse Proxy)”,其中的一个功能就是可以完成端口的分发的。我们不妨以域名为路由分发:凡是 AA.com 域名请求的,分发到 PHP 82 端口执行;凡是 BB.com 域名请求的,分发到 ASP 83 端口执行;…… 如此类推。当然这里的端口只说说明用而已,您可以任意配置,反正就是从 80 端口接收回来的请求,先作一次处理,进而分发。反向代理,通俗地讲,就是左手转右手而已。
每当提起反向代理器,人们通常一想到的就是 Nginx,但是今天我们暂时忽略大名鼎鼎的 Nginx,采用同样也是使用单线程、事件循环的服务端小弟——Nodejs 来达成。首先 Node 采用 JS 作服务端编程,而不是 Nginx 写配置或 Lua,比较符合我的味口,其次自己对 Node 也比较熟悉,配置各方面什么的更为顺手。
完成该项功能的是 node-http-proxy 包。下载、安装请键入:
?
1
npm install http-proxy
安装完毕后,新建一个 proxy.js 文件,输入:

var http = require( 'http' ), httpProxy = require( 'http-proxy' );
 
// 新建一个代理 Proxy Server 对象
var proxy = httpProxy.createProxyServer({});
 
// 捕获异常
proxy.on( 'error' , function (err, req, res) {
  res.writeHead(500, {
  'Content-Type' : 'text/plain'
  });
  res.end( 'Something went wrong. And we are reporting a custom error message.' );
});
 
// 另外新建一个 HTTP 80 端口的服务器,也就是常规 Node 创建 HTTP 服务器的方法。
// 在每次请求中,调用 proxy.web(req, res config) 方法进行请求分发Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = require( 'http' ).createServer( function (req, res) {
  // You can define here your custom logic to handle the request
  // and then proxy the request.
  var host = req.url;
  host = url.parse(host); host = host.host;
  
  console.log( "host:" + req.headers.host);
  console.log( "client ip:" + (req.headers[ 'x-forwarded-for' ] || req.connection.remoteAddress));
  
  proxy.web(req, res, { target: 'http://localhost:8080' });
});
 
console.log( "listening on port 80" )
server.listen(80);

若说使用代理服务器的代价,可能就是会比不用消耗多的资源,消耗多的 CPU 运算罢了。

使用问题:不能指定文件夹 proxy.web(req, res, { target: 'http://jb51.net:81/foo/' });


目录
相关文章
|
5天前
|
缓存 负载均衡 安全
反向代理服务器如何提升信息安全
反向代理服务器如何提升信息安全
|
1月前
|
网络协议 Shell 网络安全
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
164 0
|
1月前
|
弹性计算 缓存 测试技术
阿里云ECS云服务器2核4G能支持多少人同时访问?2核4G5M并发量评测
阿里云ECS云服务器2核4G能支持多少人同时访问?2核4G5M并发量评测,2核4G服务器并发数性能测试,阿小云账号下的2核4G服务器支持20人同时在线访问,然而应用不同、类型不同、程序效率不同实际并发数也不同,2核4G服务器的在线访问人数取决于多个变量因素
|
1月前
|
JavaScript 前端开发 应用服务中间件
通过域名的方式访问服务器里的资源
通过域名的方式访问服务器里的资源
24 0
|
1月前
|
弹性计算 小程序 开发者
阿里云服务器性能测评:25M带宽阿里云云服务器支持多少人访问?
在深入探讨25M带宽云服务器的性能时,我们首先要明确一个核心概念:带宽与服务器能够支持的同时访问量之间存在着直接的关联。那么,大家可能会好奇,带宽为25M的云服务器究竟能够支持多少用户同时访问呢?
127 0
|
1月前
|
存储 网络协议 安全
如何搭建外网可访问的Serv-U FTP服务器,轻松远程共享文件!
如何搭建外网可访问的Serv-U FTP服务器,轻松远程共享文件!
|
1月前
|
Windows
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
57 2
|
1月前
|
弹性计算 缓存 测试技术
2核4g服务器能支持多少人访问?阿里云2核4G服务器并发数测试
2核4g服务器能支持多少人访问?阿里云2核4G服务器并发数测试,2核4G服务器并发数性能测试,阿小云账号下的2核4G服务器支持20人同时在线访问,然而应用不同、类型不同、程序效率不同实际并发数也不同,2核4G服务器的在线访问人数取决于多个变量因素
|
1月前
|
弹性计算 缓存 测试技术
阿里云2核4g服务器能支持多少人访问?多少钱?
阿里云2核4g服务器能支持多少人访问?多少钱?阿里云2核4g服务器能支持多少人访问?2核4G服务器并发数性能测试,阿小云账号下的2核4G服务器支持20人同时在线访问,然而应用不同、类型不同、程序效率不同实际并发数也不同,2核4G服务器的在线访问人数取决于多个变量因素
|
1月前
|
弹性计算 缓存 测试技术
云服务器2核4G能支持多少人同时访问?2核4G5M并发量评测!
阿里云2核4g服务器能支持多少人访问?2核4G服务器并发数性能测试,阿小云账号下的2核4G服务器支持20人同时在线访问,然而应用不同、类型不同、程序效率不同实际并发数也不同,2核4G服务器的在线访问人数取决于多个变量