因最近买了欧洲的大盘鸡来做妹子图网站,硬盘是够大了,不过速度很慢,于是买了个香港的小鸡来做反代,因只是做反代,就没有必要安装lamp或者lnmp套件了,安装个nginx就解决了。本文使用的系统为centos 10 64位。

安装环境依赖

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

安装PCRE,使nginx支持Rewrite功能

cd /usr/local/src/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install

安装nginx

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
make && make install

设置缓存目录

mkdir /home/cache/path -p
mkdir /home/cache/temp -p

配置nginx配置文件

先配置nginx运行用户www

/usr/sbin/groupadd www 
/usr/sbin/useradd -g www www

然后编辑配置文件

vi /usr/local/webserver/nginx/conf/nginx.conf

将里面内容替换为下列内容

user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
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';
client_body_buffer_size  512k;
proxy_connect_timeout    5;
proxy_read_timeout       60;
proxy_send_timeout       5;
proxy_buffer_size        16k;
proxy_buffers            4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path   /home/cache/temp;
proxy_cache_path  /home/cache/path levels=1:2 keys_zone=cache_one:100m inactive=7d max_size=5g;
#上面这一行:100m只最大占用内存,7d指7天未使用删除,5g指硬盘最大使用量。
#charset gb2312;

  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;

  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on; 
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

  #limit_zone crawler $binary_remote_addr 10m;
 #下面是server虚拟主机的配置
server
{
listen          80;
server_name     localhost;
#localhost可以更改为域名
location / {
proxy_cache cache_one;
proxy_cache_valid  200 304 3d;
#上一行,3d指缓存三天,如果您的网站更新频繁,可以更改得更短。
proxy_cache_key $host$uri$is_args$args;
proxy_pass          http://www.weibomn.com/; 
#http://www.weibomn.com/更改为源网站
proxy_redirect      off;
proxy_set_header    X-Real-IP       $remote_addr;
proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
expires 10d;
access_log  off;
}
}

}

启动nginx

/usr/local/webserver/nginx/sbin/nginx

在启动前也可以先检查一下配置是否正确

/usr/local/webserver/nginx/sbin/nginx -t

开机自启动nginx

vi /etc/rc.local

在下面增加一行

/usr/local/webserver/nginx/sbin/nginx

最后设置权限

chmod 755 rc.local

就搞定了

现在访问你的反代小鸡,出现的就是源网站的内容了