Jul 07

前几天弄了一个免费的VPS来玩,128M的基本的配置,用来安装其他的软件由于优化不会,可能不能很好的运行,正好有一个博客是建在OpenShift上的,可以用Nginx来做一下反向代理,我Ping了一下,速度不是很快,仅限于可以访问,在配置完成后在电脑上实际测试,速度还是可以的,有总比没有强的。

现在VPS上用apt-get安装Nginx,然后在下载源文件进行编译安装,这样做的好处是在编译的时候不用指定某些路径,实际上我就不会指定路径,路径不对的话很容易造成在系统里不能正常启动。安装上面的方法相当于将原有的安装文件稍稍更新了一下而已。具体步骤如下:

1.下载zlib

wget http://zlib.net/zlib-1.2.7.tar.gz
tar -zxvf zlib-1.2.7.tar.gz

2.下载pcre

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
tar -zxvf pcre-8.30.tar.gz

3.下载nginx

wget http://nginx.org/download/nginx-1.2.2.tar.gz
tar -zxvf nginx-1.2.2.tar.gz

4.安装nginx

cd nginx-1.2.2
./configure --with-http_sub_module --with-pcre=../pcre-8.30 --with-zlib=../zlib-1.2.7
make
make install

5.配置nginx.conf

    #proxy_set_header Host $host;
    #proxy_set_header X-Real-IP $remote_addr;
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 10m;
    client_body_buffer_size 128k;
    client_header_buffer_size 64k;
    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
    proxy_buffer_size 16k;
    proxy_buffers 32 16k;
    proxy_busy_buffers_size 64k;

server {
    listen   94.249.183.187:80; ## listen for ipv4
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6
    server_name  rh.chunchu.org;
location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Accept-Encoding "";
    proxy_pass http://redhat.chunchu.org;
    sub_filter redhat.chunchu.org rh.chunchu.org;
    sub_filter_once off;
    proxy_redirect off;
}
}

用vi打开/etc/nginx/文件夹的下文件nginx.conf,在http代码后面输入上述代码后保存推出。

6.重启nginx
以root身份输入命令:

/etc/init.d/nginx restart

7.另附Google GHS反向代理的配置文件
在nginx.conf文件中添加代码:

upstream ghs {
    ip_hash;
    server ghs.google.com;
    server 72.14.203.121;
    server 72.14.207.121;
    server 74.125.43.121;
    server 74.125.47.121;
    server 74.125.53.121;
    server 74.125.77.121;
    server 74.125.93.121;
    server 74.125.95.121;
    server 74.125.113.121;
    server 216.239.32.21;
    server 216.239.34.21;
    server 216.239.36.21;
    server 216.239.38.21;
}

server {
    listen   94.249.243.220:80; ## listen for ipv4
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6
    server_name  ghs.chunchu.org;
location / { 
    proxy_redirect off;
    #proxy_set_header Host $host;
    proxy_pass http://ghs;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

然后重启nginx即可。

8.注意事项
(1).新安装nginx后,启动nginx一般是不成功的,因为Apache将80端口占用了,须将占用80端口的进程全部Kill,然后再启动nginx就行了。

(2).在编译nginx时,不要忘记添加–with-http_sub_module参数,因为添加这个参数时,反向代理的网站的主页是正常的,但是点击链接后会跳转到原网站的地址,添加–with-http_sub_module参数,然后按照上述示例修改就可以解决这个问题。

(3).我曾试图将网页的反向代理和Google GHS的反向代理合二为一,但是实践了一次都不能成功,都只能是其中的一种成功,二者不能并存,不知奥是我设置的问题还是二者根本就不能同时使用。

(4).在添加proxy_pass http://redhat.chunchu.org;时,如果网址后面没有“/”则表示是相对链接,有”/”则表示是绝对链接,这个有时候也会出现问题。另外要仔细检查配置文件,不要丢失了最后的分号“;”。

Leave a Reply