negix代理apache实现

来源:互联网 发布:腾讯程序员平均年龄 编辑:程序博客网 时间:2024/05/22 00:13

前段时间架设了freebsd+apache+php+mysql,不知为何系统总是遭受来至一个ip的大量访问,因为该服务器的内存较小,只有512M,其他配置都是高配,我猜想是否是大访问量导致死机,而且查看apache的错误日志大部分来至这个ip以及其他ip对该域名的以前网站的访问,因为现在的网站是用php写的,错误日志大部分是访问asp的文件,于是我采用性能比apache更优越的Nginx做前端挡掉那些不怀好意访问asp而增大访问量的请求,具体操作如下:

FreeBSD 7.3

Apache-2.2.14-5[ 1.3.42(Unix)]

PHP-5.2.12

MySQL-5.0.90

一.Freebsd通过port安装nginx

Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。

Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括新浪博客、新浪播客、网易新闻等门户网站频道,六间房、56.com等视频分享网站,Discuz!官方论坛、水木社区 等知名论坛,豆瓣、YUPOO相册、海内SNS、迅雷在线等新兴Web 2.0网站。

现在就就来学习在freebsd7.0中安装及配置nginx,这里只是把apache换成nginx,其他php等都不变:

一、         安装nginx 0.7.65

为了让nginx能够代理apache,要先确保apache安装rpaf_module 模块

mod_rpaf 是一个 Apache 的模块,利用它可以给 Apache 的后端应用提供客户端真实的IP地址。

使用方法:

# if DSO load module first:

[root@bsd01 ~]#cd /usr/ports/www/mod_rpaf2/

[root@bsd01 ~]#make install cleanBSD

OK,为了安装后的测试,我也把apache先停了,以免冲突。

[root@bsd01 ~]#cd /usr/ports

[root@bsd01 ports]#make search key=^nginx

Port:   nginx-0.7.65

Path:   /usr/ports/www/nginx

Info:   Robust and small WWW server

Maint:  osa@FreeBSD.org

B-deps: pcre-8.00

R-deps: pcre-8.00

WWW:    http://sysoev.ru/nginx/

Port:   nginx-devel-0.8.34

Path:   /usr/ports/www/nginx-devel

Info:   Robust and small WWW server

Maint:  osa@FreeBSD.org

B-deps: pcre-8.00

R-deps: pcre-8.00

WWW:    http://sysoev.ru/nginx/

freebsd上有2个,一个0.7.65(这里查询显示nginx-0.7.65)是稳定版,0.8.34是最新的开发版,所以这里安装0.7.65。

[root@bsd01 ports]#cd www/nginx

[root@bsd01 nginx]#make install clean

接着出来“make config”的菜单,选择需要的组件即可:

Options for nginx 0.7.65

─────────────────────────────────────

[ ] DEBUG Enable nginx debugging

[ ] GOOGLE_PERFTOOLS Enable google perftools module

[X] HTTP_MODULE Enable HTTP module

[X] HTTP_ADDITION_MODULE Enable http_addition module

[X] HTTP_DAV_MODULE Enable http_webdav module

[X] HTTP_FLV_MODULE Enable http_flv module

[X] HTTP_PERL_MODULE Enable http_perl module

[X] HTTP_REALIP_MODULE Enable http_realip module

[X] HTTP_REWRITE_MODULE Enable http_rewrite module

[X] HTTP_SSL_MODULE Enable http_ssl module

[X] HTTP_STATUS_MODULE Enable http_stub_status module

[X] HTTP_SUB_MODULE Enable http_sub module

[ ] MAIL_MODULE Enable IMAP4/POP3/SMTP proxy module

[ ] MAIL_IMAP_MODULE Enable IMAP4 proxy module

[ ] MAIL_POP3_MODULE Enable POP3 proxy module

[ ] MAIL_SMTP_MODULE Enable SMTP proxy module

[ ] MAIL_SSL_MODULE Enable mail_ssl module

[X] WWW Enable html sample files

tab到OK,开始安装。

安装完后,配置目录位于/usr/local/etc/nginx内,这个是freebsd的规则了,闭着眼睛都知道。主配置文件就是nginx.conf。

由于前面已经安装了php这些,这里就不用安装了,只要把php配置到nginx上就可以了。

二.配置nginx.conf

[root@bsd01 ports]#vi  /usr/local/etc/nginx/nginx.conf

user www;   #www组用户

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

   worker_connections  1024;

}

http {

   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;

   sendfile        on;

   #tcp_nopush     on;

   #keepalive_timeout  0;

   keepalive_timeout  65;

   #gzip  on;

   server {

       listen       80;

       server_name  xx.xx.gov.cn;        # xx.xx.gov.cn;你的网站域名

       charset gbk;

       client_max_body_size 100m;       #nginx限制了上传文件大小,这里修改限制(具#体参考我博客的另一篇关于ngnix修改上#传文件的文章)

       #access_log  logs/host.access.log  main;

       location / {

           root   /home/www/rd;                             #网站根目录

           index  index.php index.html index.htm;    #添加index.php

       }

       #error_page  404              /404.html;

       # redirect server error pages to the static page /50x.html

       #

       error_page   500 502 503 504  /50x.html;

       location = /50x.html {

           root   /usr/local/www/nginx-dist;

       }

       # proxy the PHP scripts to Apache listening on 127.0.0.1:80

       #

       #location ~ /.php$ {

       #    proxy_pass   http://127.0.0.1;

       #}

       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

       #

       location ~ /.php$ {             #打开前面

       #    root           html;

       #    fastcgi_pass   127.0.0.1:9000;

       #    fastcgi_index  index.php;

       #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

       #    include        fastcgi_params;

          proxy_pass http://xx.xx.gov.cn:81;      #代理设置(要代理的网站域名,81端口)

          proxy_set_header   Host             $host;

          proxy_set_header   X-Real-IP        $remote_addr;

          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

       }                           #打开后面

       # deny access to .htaccess files, if Apache's document root

       # concurs with nginx's one

       #

       location ~ //.ht {              #打开

           deny  all;             #打开

       }                         #打开

   }

   # another virtual host using mix of IP-, name-, and port-based configuration

   #

   #server {

   #    listen       8000;

   #    listen       somename:8080;

   #    server_name  somename  alias  another.alias;

   #    location / {

   #        root   html;

   #        index  index.html index.htm;

   #    }

   #}

   # HTTPS server

   #

   #server {

   #    listen       443;

   #    server_name  localhost;

   #    ssl                  on;

   #    ssl_certificate      cert.pem;

   #    ssl_certificate_key  cert.key;

   #    ssl_session_timeout  5m;

   #    ssl_protocols  SSLv2 SSLv3 TLSv1;

   #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

   #    ssl_prefer_server_ciphers   on;

   #    location / {

   #        root   html;

   #        index  index.html index.htm;

   #    }

   #}

}

保存退出

[root@bsd01 ports]#:wq!

三.修改apache httpd.conf

[root@bsd01 ports]#vi /usr/local/etc/apache22/httpd.conf

1.加载rpaf_module 模块

LoadModule rpaf_module        libexec/apache22/mod_rpaf.so

RPAFenable On

RPAFsethostname On

RPAFproxy_ips 222.92.129.136

RPAFheader X-Forwarded-For

2.将里面的所有80端口改为81

四.修改httpd-vhosts.conf

[root@bsd01 ports]#vi /usr/local/etc/apache22/extra/httpd-vhosts.conf

将里面的所有80改为81

五.将nginx设为开机自启动

[root@bsd01 ports]#vi /etc/rc.cpnf

添加nginx_enable="YES"

六.启动apache,nginx

[root@bsd01 ports]# /usr/local/etc/rc.d/apache22 start

[root@bsd01 ports]# /usr/local/etc/rc.d/nginx start

打开你的域名访问成功!大功告成!哈哈~