首先要开启模块mod_proxy, httpd.conf:
Listen 80
Listen 81
NameVirtualHost *:80
<VirtualHost *:80>
ServerName b.com
DocumentRoot /b.com
</VirtualHost>
NameVirtualHost *:81
<VirtualHost *:81>
ServerName b.com
DocumentRoot /b2.com
</VirtualHost>
很久之前的笔记了,现在整理出来, 这里采用的是Apache + Mongrel Cluster, 其实shitou个人还是喜欢Lighttpd + FastCGI搭配的说,因为其他原因只能现在跑这样的环境啦- -||
软件:
ruby-1.8.7-p72.tar.gz
rubygems-1.3.0.tgz
ruby-zlib-0.6.0.tar.gz
mysql-ruby-2.8.tar.gz
安装:
安装ruby
#apt-get install gcc libc6-dev make tmake autogen automake \
build-essential indent intltool
#tar zxvf ruby-1.8.7-p72.tar.gz
#cd ruby-1.8.7-p72
##开启最后一行的zlib支持
#vi ext/Setup
#./configure --prefix=/usr/local/ruby
#make; make install
##把/usr/local/ruby/bin加入环境变量PATH中, 并写入文件/root/.profile中
#apt-get install zlib1g-dev
#cd ext/zlib
#ruby extconf.rb;
#make; make install
安装rubygems
#tar zxvf rubygems-1.3.0.tgz
#cd rubygems-1.3.0
#ruby setup.rb
安装rails
#gem install rails –version 2.0.2
支持rails console
#apt-get install libreadline5-dev
#cd ruby-1.8.7-p72/ext/readline
#ruby extconf.rb
#make; make install
在不安装SSL的话rails应用启动会报错
#apt-get install libssl-dev
#cd ruby-1.8.7-p72/ext/openssl
#ruby extconf.rb
#make; make install
安装mysql
#apt-get install mysql-server mysql-client libmysqlclient15-dev
安装ruby的mysql的C接口
#tar zxvf mysql-ruby-2.8.tar.gz
#cd mysql-ruby-2.8
#ruby extconf.rb –with-mysql-include=/usr/include/mysql –with-mysql-lib=/usr/lib
#make; make install
安装mongrel
#gem install mongrel mongrel_cluster
Apache, PHP的安装
#apt-get install apache2
#apt-get install php5 php5-mysql libapache2-mod-php5 php-pear
mongrel的配置
/railsapp/config/mongrel_cluster.yml:
cwd: /var/www/rails/cal_point
port: 3000
environment: production
group: www-data
user: www-data
address: 127.0.0.1
pid_file: log/mongrel.pid
servers: 3
Apache开启Proxy的相关模块, 配置
/etc/apache2/httpd.conf:
<Proxy balancer://myproxycluster>
#loadfactor is the weight of the server, range 1-100, the bigger the stronger
BalancerMember http://127.0.0.1:3000 loadfactor=10
BalancerMember http://127.0.0.1:3001 loadfactor=10
BalancerMember http://127.0.0.1:3002 loadfactor=10
</Proxy>
ProxyPass / balancer://myproxycluster/
ProxyPassReverse / balancer://myproxycluster/
启动mongrel cluster:
进去rails项目的根目录:
#mongrel_rails cluster::start
注意: 启动前要保正log文件夹的权限为Apache运行用户(在debian中一般是www-data)可写的
在Lighttpd的wiki上看大一篇很好的文档,里面也有troubleshooting, 转过来了,本来是想找Lighttpd防webspider的资料的,
根据下面的说明使用$HTTP["useragent"]这个变量可以达到效果
#!rst
==================
Configuration File
==================
------------
Module: core
------------
.. meta::
:keywords: lighttpd, configuration
.. contents:: Table of Contents
Description
===========
Basic Syntax
------------
A BNF like notation: ::
option : NAME = VALUE
merge : NAME += VALUE
NAME : modulename.key
VALUE : ( | | | | VALUE [ + VALUE ]*)
: "text"
: digit*
: ( "enable" | "disable" )
: "(" [ "=>" ] [, [ "=>" ] ]* ")"
INCLUDE : "include" VALUE
INCLUDE_SHELL : "include_shell" STRING_VALUE
Example
-------
::
# default document-root
server.document-root = "/var/www/example.org/pages/"
# TCP port
server.port = 80
# selecting modules
server.modules = ( "mod_access", "mod_rewrite" )
# variables, computed when config is read.
var.mymodule = "foo"
server.modules += ( "mod_" + var.mymodule )
# var.PID is initialised to the pid of lighttpd before config is parsed
# include, relative to dirname of main config file
include "mime.types.conf"
# read configuration from output of a command
include_shell "/usr/local/bin/confmimetype /etc/mime.types"
Conditional Configuration
=========================
Most options can be configured conditionally by using the following syntax
(including nesting).
::
{
...
{
... nesting: match only when parent match
}
}
else {
... the "else if" block
}
where is one of one of the following:
$HTTP["cookie"]
match on cookie
$HTTP["host"]
match on host
$HTTP["useragent"]
match on useragent
$HTTP["referer"]
match on referer
$HTTP["url"]
match on url. If there are nested blocks, this must be the most inner block.
$HTTP["querystring"]
match on querystring, eg, after the ? in this type url: index.php?module=images..
$HTTP["remoteip"]
match on the remote IP or a remote Network (Warning: doesn't work with IPv6 enabled)
$HTTP["scheme"] (Introduced in version 1.4.19)
match on the scheme used by the incoming connection. This is either "http" or "https".
$SERVER["socket"]
match on socket. Value must be on the format "ip:port" where ip is an IP address and
port a port number, or ":port" to match port only. Only equal match (==) is supported.
It also binds the daemon to this socket. Use this if you want to do IP/port-based virtual hosts.
$PHYSICAL["path"] (Introduced in version 1.5.0)
match on the mapped physical path of the file / cgi script to be served.
$PHYSICAL["existing-path"] (Introduced in version 1.5.0)
match on the mapped physical path of the file / cgi script to be served
only if such a file exists on the local filesystem.
is one of:
==
string equal match
!=
string not equal match
=~
perl style regular expression match
!~
perl style regular expression not match
and is either a quoted ("") literal string or regular expression.
Example
-------
::
# disable directory-listings for /download/*
dir-listing.activate = "enable"
$HTTP["url"] =~ "^/download/" {
dir-listing.activate = "disable"
}
# handle virtual hosting
# map all domains of a top-level-domain to a single document-root
$HTTP["host"] =~ "(^|\.)example\.org$" {
server.document-root = "/var/www/htdocs/example.org/pages/"
}
# multiple sockets
$SERVER["socket"] == "127.0.0.1:81" {
server.document-root = "..."
}
$SERVER["socket"] == "127.0.0.1:443" {
ssl.pemfile = "/var/www/certs/localhost.pem"
ssl.engine = "enable"
server.document-root = "/var/www/htdocs/secure.example.org/pages/"
}
# deny access for all googlebot
$HTTP["useragent"] =~ "Google" {
url.access-deny = ( "" )
}
# deny access for all image stealers (anti-hotlinking for images)
$HTTP["referer"] !~ "^($|http://www\.example\.org)" {
url.access-deny = ( ".jpg", ".jpeg", ".png" )
}
# deny the access to www.example.org to all user which
# are not in the 10.0.0.0/8 network
$HTTP["host"] == "www.example.org" {
$HTTP["remoteip"] != "10.0.0.0/8" {
url.access-deny = ( "" )
}
}
# Allow only 200.19.1.5 and 210.45.2.7 to
# have access to www.example.org/admin/
$HTTP["host"] == "www.example.org" {
#!~ is a perl style regular expression not match
$HTTP["remoteip"] !~ "^(200\.19\.1\.5|210\.45\.2\.7)$" {
$HTTP["url"] =~ "^/admin/" {
url.access-deny = ( "" )
}
}
}
Troubleshooting
---------------
If you're not running on the default port, $HTTP["host"] will have the port appended to it,
so regular expressions ending in "$" (without allowing for a port) won't match.
To match with or without a port, change "(^|\\.)example\\.org$" to "(^|\\.)example\\.org(\\:[0-9]*)?$"
advanced usage
==============
check the blog:
http://blog.lighttpd.net/articles/2005/05/07/advanced-configuration-in-up-upcoming-1-4-x
Using variables
===============
You can set your own variables in the configuration to simplify your config.
::
var.basedir = "/home/www/servers/"
$HTTP["host"] == "www.example.org" {
server.name = "www.example.org"
include "incl-base.conf"
}
in incl-base.conf:
server.document-root = basedir + server.name + "/pages/"
accesslog.filename = basedir + server.name + "/logs/access.log"
You can also use environment variables or the default variables var.PID and
var.CWD: ::
var.basedir = env.LIGHTTPDBASE
$HTTP["host"] == "www.example.org" {
server.name = "www.example.org"
include "incl-base.conf"
include "incl-fastcgi.conf"
}
in incl-fastcgi.conf:
fastcgi.server = ( ... => ((
"socket" => basedir + server.name + "/tmp/fastcgi-" + PID + ".sock"
)) )
Or like the lighttpd script for rails does: ::
var.basedir = var.CWD
server.document-root = basedir + "/public/"
Some useful things that can NOT be done in lighttpd config (you need to
create a script in a real programming language and than use include_shell): ::
# testing if a variable has been set is NOT possible
var.not_sure_if_it_exists == undefined { ... set to default value ... }
# removing from arrays is NOT possible
server.modules -= ( "mod_idontwantyou" )
Global context
==============
::
global {
...
}
You don't need it in the main configuration file. But you might have
difficulty setting a server wide configuration inside a included-file from
conditionals.
Example
-------
::
in lighttpd.conf:
server.modules = ()
$HTTP["host"] == "www.example.org" {
include "incl-php.conf"
}
in incl-php.conf:
global {
server.modules += ("mod_fastcgi")
static-file.exclude-extensions += (".php")
}
fastcgi.server = "..."
Options
=======
`All Configuration Options`__
.. __: http://trac.lighttpd.net/trac/wiki/Docs%3AConfigurationOptions
Etag
在HTTP1.1中有介绍,主要的作用就是在(css file, image, javascript file)文件后面添加一个唯一的参数(相当于查询参数字符串),Etag有服务器端生成,并且随着文件的改变而改变,这样浏览器端就会只重新请求获取Etag发生变化的文件,减少浏览器端数据的流量,加快浏览器的反应速度,重要的是减轻服务器端的压力,所以服务器端Etag的实现就比较重要了。
下面分别说下在Apache和Lighttpd中怎样配置Etag
Apache Etag
在Apache中设置Etag的支持比较简单,只用在含有静态文件的目录中建立一个文件.htaccess, 里面加入:
FileETag MTime Size
这样就行了,详细的可以参考Apache的文档页:Documentation
Lighttpd Etag
在Lighttpd中设置Etag支持:
etag.use-inode: 是否使用inode作为Etag
etag.use-mtime: 是否使用文件修改时间作为Etag
etag.use-size: 是否使用文件大小作为Etag
static-file.etags: 是否启用Etag的功能
第四个参数肯定是要enable的, 前面三个就看实际的需要来选吧,推荐使用修改时间
Expires
过期时间是HTTP响应(response)的头部控制信息,浏览器会在指定过期时间内使用本地缓存,不用重新加载,对应的缓存对象主要是静态文件,如css,image, javascript; 可以在服务器端进行全局设置,也可以应用端进行设置,这里我会说下服务器端的设置
Apache
首先要确定Apache是否支持了mod_expired模块
然后在Apache的配置文件中加入:
ExpiresActive on
ExpiresDefault "access plus 1 year"
可以看出,配置时可以指定设置Expire对象的文件类型,以及过期时间,这里是一年后
Lighttpd
Lighttpd设置expire也要先查看是否支持了mod_expire模块,查看vlighttpd.conf文件中
是否开启了该模块
然后就可以设置了:
下面的设置是让URI中所有images目录下的文件1小时后过期;
expire.url = ( "/images/" => "access 1 hours" )下面是让作用于images目录及其子目录的文件;
$HTTP["url"] =~ "^/images/" {
expire.url = ( "" => "access 1 hours" )
}也可以指定文件的类型;
$HTTP["url"] =~ "\.(jpg|gif|png|css|js)$" {
expire.url = ( "" => "access 1 hours" )
}
其他
设置较长的过期时间后如果想要客户端重新下载文件怎么办呢,比如当你修改了javascript或者
是css文件后,这时可以改变文件的文件名,最合适的做法是给文件加上一个版本号,
比如main_1.0.1.js, 这也是yahoo的做法。
当服务器的环境是cluster时,Etag的使用可能就要考虑的更多了,因为每个服务器生成的Etag
不一样,所以最终的浏览器每次都检测到文件的Etag不一样而去重新请求,这里就不多说了,
注意到这个问题就行了,好了,说完了
原文链接:原文
由于rails的逐渐流行,各种的web服务器也逐渐开始流行起来,搜集点nginx的资料:
nginx的wiki: http://wiki.codemongers.com/NginxChs
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。
因 为 Nginx 的文档大部分是俄文的,本维基的目的是帮助使用中文世界的用户安装、配置和交换有关 Nginx 的使用经验。大部分文档源自Aleksandar Lazic 的工作。有关本维基本身的问题请直接询问Cliff Wells 。
其他翻译请点击
官方说明: http://nginx.net/
Basic HTTP features:
Mail proxy server features:
Tested OS and platforms:
Architecture and scalability:
Other HTTP features:
从网上的信息看nginx的性能还是非常强大,官方介绍里的特性有很多也是非常吸引人的,
rewrite, proxy的特性就不说了,还支持FLV streaming,做视频网站非常不错,可以结合Lighttpd.
其他的负载均衡能力好象也很强悍,据说超过了Pound,Apache mod_proxy_balancer.
另外静态文件的传输能力也超过了Lighttpd.不过好象国内使用的人还不是很多,在CU上搜了下没什么相关帖子
等有时间了做下实验,看看咋样~
这有几篇不错的文档,先把地址放这,做实验时用:)
https://calomel.org/nginx.html
http://en.wikipedia.org/wiki/Nginx
http://docs.planetargon.com/Nginx_Configuration/
http://blog.kovyrin.net/2006/04/17/typical-nginx-configurations/
作者:北南南北
来自:LinuxSir.Org
摘要:在服务器管理中,经常遇到DDOS攻击WWW服务器。对于Apache(httpd)服务器来说,防御DDOS是通过mod_evasive来实现的。本文将介绍mod_evasive模块的安装、配置;
目录
+++++++++++++++++++++++++++++++++++++++++++
正文
+++++++++++++++++++++++++++++++++++++++++++
1、 mod_evasive 介绍;
mod_evasive 是Apache(httpd)服务器的防DDOS的一个模块。对于WEB服务器来说,是目前比较好的一个防护DDOS攻击的扩展模块。虽然并不能完全防御 DDOS攻击,但在一定条件下,还是起到缓服Apache(httpd)服务器的压力。如果配合iptables、硬件防火墙等防火墙设备配合使用,可能 有更好的效果。可惜LinuxSir.Org 并没有硬件防火墙,所以是否这种组合效果有更好的效果,我没办法验证。
mod_evasive 的官方地址: http://www.zdziarski.com/projects/mod_evasive
2、 mod_evasive 的安装和配置;
2.1 mod_evasive 的下载地址;
2.2 mod_evasive 的安装;
安装 mod_evasive 之前,你要用安装Apache(httpd)服务器软件包,还要安装httpd-devel或 apache-dev。在Slackware 12.0中,安装httpd软件即可;
对于Apache 1.x 请用下面的编译方法;
对于Apache 2.x 可以用下面的办法;
注:apxs 用于编译模块工具;如果是用系统自带的软件包,一般位于/usr/sbin目录。如果您是自己编译安装Apache(httpd)的,你应该自己来指定路径;
我 们然后修改/etc/ld.so.conf 文件,把编译出来的动态模块的所在位置指定在 ld.so.conf中;比如我用的是Aapche 2.x ,编译完成后,模块mod_evasive20.so 安装到了 /usr/lib/httpd/modules 目录中;那我们就要把 这个目录写入到ld.so.conf中。
注: 具体要与你的系统环境为准,不要照搬照抄,如果你对Linux不太熟的话;
2.3 mod_evasive 的配置;
在编译安装完成后,会自动插入一行到Apache 配置文件中,对于Apache 2.x 版本中,应该在其配置文件中有类似下面的一行;
对于Apache 1.x来说,也应该差不多,大体只是路径不同罢了;
然后我们再修改 Apache 的配置文件,配置文件名为httpd.conf;
在Apache v1.x 版本中,要加入;
在Apache v2.x加入;
如果您不知道把这些插入到哪,用下面的办法做也是可以的;
在/etc目录中创建一个文件,比如mod_evasive.conf;
然后把根据自己的Apache版本来加入相应的内容;
接着我们再修改 httpd.conf ,在最后一行加入
修改完成后,我们要重启Apache服务器;
比如在Slackware 12.0中,Apache 2.x的重启,我们要用到
在Redhat、Fededora、Debian、Ubuntu、CentOS中的Apache,可以用;
大体上差不多是这样的……
2.4 对mod_evasive测试验证 ;
防DDOS的模块做好后,我们可以要验证,可以用Apache 自带的ab工具,系统默认安装在/usr/sbin目录中;比如;
注:上面的例子的意思是,如果您的服务器是google的WEB服务器,我们要发送数据请求包,总共1000个,每次并发50个;
另外一个测试工具就是mod_evasive的解压包的目录中,有个test.pl ,你可以修改IP地址,然后用
是不是有效果,请根据 ab工具或 测试脚本出来的结果来查看;
因为我们编译mod_evasive时,用的是默认配置,所以日志被存放在/tmp目录中。如果有DDOS攻击,会在/tmp产生日志。日志的文件是以 dos-开头的;
3、mod_evasive 的高级配置;
如果想更改一些适合自己的参数,有些必要的参数,并不是通过配置文件修改就一下起作用的,我们要修改源码包中的 mod_evasive.c(Apache 1.x用之) 或 mod_evasive20.c (Apache 2.x用之);
比如我们改改其中的数字,根据英文很容易理解。比如修改日志存放目录,就把/tmp改成别的目录。如果您不知道放在哪好,还是用默认的吧;
如果您在这里更改了参数,不要忘记修改Apache 配置文件中关于mod_evasive 的参数;
如果您想加入一些其它的参数,请查阅源码包中的README,里面有详细说明,大多来说没太大的必要……
这个文件相当重要,如果您想更改某些设置,就要修改这个文件……
4、关于本文;
mod_evasive 还是有点用的,安装也不费力气。如果你需要的时候,会想到这个模块的 …… mod_evasive 是否有更高级的用法,也不是在这篇文章里能说的清楚的。如果不懂怎么办?请查看源码包中的README。
本文权当做软件说明 ……
北南 记之
From LinuxSir.Org
5、参考文档;
参考 mod_evasive 源码包中的README ……
