Posted by bianbian on 2007-04-05 04:40
本文Tags: nginx, X-Accel-Redirect, 控制
有时你可能需要实现控制下载:即将下载文件的请求转发到某脚本, 然后由这脚本决定怎么做:发送这个文件给用户,出现决绝访问页,或着其他的事。在lighttpd服务器里可以通过从脚本传回X-Sendfile头实现;而Nginx是通过使用X-Accel-Redirect头实现的。在这篇文章里我会尽量简捷地描述在php和rails里如何使用这一特性。
假设你使用Apache运行PHP或Rails产生动态内容,而用Nginx作为前台反向代理(bianbian注:反向代理又称为服务器加速(Server accelerate),原理是将用户的请求转发到目标服务器,然后将结果转发给用户。好处有很多:保护目标服务器安全、负载均衡容易实现、有点类似防火墙;坏处我认为就是要传递用户的IP的时候多了些步骤)。你就达到了两个目标:
- 因为Nginx服务器会改善所有对动态内容的缓慢请求,能节省服务器的资源(细节正在
这里). (bianbian注:凭我对Nginx的理解,这个就是Nginx会缓存客户端的请求,等全部发送完毕了才一起转发给后台脚本,比如在上载文件的时候。好处是减少后台脚本等待的时间,确实对性能有一定改善;坏处就是在脚本里时时显示上载进度的功能是不可能实现了[当然,以后Nginx如果自己开放这个进度API也是可能的,不过也不是脚本级的,好在显示进度的功能不常用])
- 你能对静态文件的下载做出控制.(bianbian注:后面的一大段都是说这个啦!)
在这里,假设网站位于 /var/www 目录,而一些静态文件(类似电影、歌曲、或其他)位于 /var/www/files 目录。Apache监听8080端口。
首先,让我们看一看nginx配置:
Read the rest of this entry »
标签:
nginx,
X-Accel-Redirect,
控制遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in Linux, Technology | 4 Comments »
Posted by bianbian on 2007-04-05 07:11
本文Tags: nginx, 反向代理
Nginx [engine x] is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server written by Igor Sysoev. It has been running on many heavily loaded Russian sites for more than two years.
俄罗斯人写的,我总觉得俄罗斯都是高手。。。。再仔细看了一下(所有能看懂的文档),基于事件的,对小文件的读写性能很好,也更适合做反向代理(nginx 》Squid 》Apache),更适合BBS的情况。所以决定装这个。
http://sysoev.ru/nginx/download.html
俄语,不要紧,最上面那个就是最新的。
下载解压安装:
tar zxf nginx-x.x.x.tar.gz
cd nginx-x.x.x
./configure --prefix=/opt/nginx --with-http_realip_module
make
make install
这样配置文件在/opt/nginx/conf/nginx.conf
cd /opt/nginx
vim conf/nginx.conf
配置文件格式有点像lighttpd,其实你熟悉JavaScript或JSON的话应该很容易看懂。
我修改的内容:
反向代理:
location / {
proxy_pass http://localhost:8000/;
proxy_set_header X-Real-IP $remote_addr;
}
#禁止下载所有.开头的文件名,如 .DIR
location ~ /\..+ {
deny all;
}
标签:
nginx,
反向代理遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in Linux, Technology | 2 Comments »
Posted by bianbian on 2006-10-30 07:45
本文Tags: apache, 反向代理, 模块
除了用端口映射(当然,要在转发数据包前传递客户端IP地址给内网服务器)来实现反向透明代理以外,利用apache的mod_proxy也是一种办法。而后一种办法更方便的地方是一个80端口可以给很多域名使用(即虚拟主机)——当然前一种也是可以,只要根据不同域名把数据包转发到不同内网服务器而已,我只是懒得写而已;因为工作量显然是直接用apache来得少。。。。
我贴出用后一种办法实现以前lilybbs.net公网代理的代码;之所以要自己写apache的模块,是因为直接用mod_proxy是不能传递客户端的IP地址给真正的内网服务器的。我原本打算是插入自定义的一个HTTP头来实现IP传递,这样一般是没有问题;但是当客户端本身访问网络经过Squid代理时,Squid会删除掉非标准的HTTP头,真是FT。所以我采用的方法是通过”Accept-Language”头传递客户端IP地址,”Accept-Language”是用来标示客户端接受的页面语言顺序(中文?英文?之类),在非国际化的网站中并没有很大的用处。
apache的模块编写其实很简单,大部分都是架子工程,直接贴过来就可以。
代码如下(代码经过lilybbs.net公网访问测试OK,不过现在学校网络中心停止了lilybbs.net的公网出口)。
-
- #include "httpd.h"
- #include "http_config.h"
- #include "http_core.h"
- #include "http_log.h"
- #include "http_protocol.h"
- #include "http_vhost.h"
- #include "apr_strings.h"
-
- module AP_MODULE_DECLARE_DATA lilybbsip_module;
-
-
- typedef struct {
- int enable;
- } lilybbsip_server_cfg;
-
- static void *lilybbsip_create_server_cfg(apr_pool_t *p, server_rec *s) {
- lilybbsip_server_cfg *cfg = (lilybbsip_server_cfg *)apr_pcalloc(p, sizeof(lilybbsip_server_cfg));
- if (!cfg)
- return NULL;
-
- cfg->enable = 0;
-
- return (void *)cfg;
- }
-
-
- static const char *lilybbsip_enable(cmd_parms *cmd, void *dummy, int flag) {
- lilybbsip_server_cfg *cfg = (lilybbsip_server_cfg *)ap_get_module_config(cmd->server->module_config, &lilybbsip_module);
- cfg->enable = flag;
- return NULL;
- }
-
- static const command_rec lilybbsip_cmds[] =
- {
- AP_INIT_FLAG(
- "LilybbsipEnable",
- lilybbsip_enable,
- NULL,
- RSRC_CONF,
- "Enable mod_lilybbsip"
- ),
- { NULL }
- };
-
- static int ip_lilybbsing_handler(request_rec *r) {
-
- lilybbsip_server_cfg *cfg = (lilybbsip_server_cfg *)ap_get_module_config(r->server->module_config, &lilybbsip_module);
- if (!cfg->enable)
- return DECLINED;
-
- //通过Accept-Language传递IP
- apr_table_set(r->headers_in, "Accept-Language", r->connection->remote_ip);
-
- return DECLINED;
- }
-
-
- static void register_hooks(apr_pool_t *p)
- {
- ap_hook_post_read_request(ip_lilybbsing_handler, NULL, NULL, APR_HOOK_MIDDLE);
- }
-
- module AP_MODULE_DECLARE_DATA lilybbsip_module =
- {
- STANDARD20_MODULE_STUFF,
- NULL,
- NULL,
- lilybbsip_create_server_cfg,
- NULL,
- lilybbsip_cmds,
- register_hooks,
- };
使用的时候在vhost(虚拟主机)内设定:
LilybbsipEnable On
ProxyRequests Off
ProxyPass / http://bbs.nju.edu.cn
标签:
apache,
反向代理,
模块遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in C/C++, Linux, Technology | 2 Comments »
Posted by bianbian on 2006-09-27 05:43
本文Tags: slackware
网站上好像还没有及时更新,ftp上已经release了:
ftp://ftp.slackware.com/pub/slackware/slackware-current/ANNOUNCE.11_0
Announcing Slackware Linux 11!
The only Slackware release more than a year in the making, this
edition of Slackware combines Slackware’s legendary simplicity,
stability, and security with some of the latest advances in Linux
technology. Expect no less than the best Slackware yet.
Among the many program updates and distribution enhancements, you’ll
find two of the most advanced desktop environments available today:
Xfce 4.2.3.2, a fast and lightweight but visually appealing and easy
to use desktop environment, and KDE 3.5.4, the latest version of the
award-winning K Desktop Environment.
Read the rest of this entry »
标签:
slackware遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in Linux, Technology | 5 Comments »