Posts tagged ‘nginx’

Windows及Linux下nginx反向代理的配置

网上nginx解释:

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

1、Windows下nginx反向代理的配置

最早nginx没有windows版本,后期才增加的。本次案例的具体如下图,在内网放置一台nginx反向代理服务器,代理内网的两个站点http://10.10.10.88http://10.10.10.89。具体配置如下:

proxy11

首先安装完win2003操作系统,将外网口配置为192.168.5.2/24,默认网关192.168.5.1;将内网口IP配置为10.10.10.1,默认网关不配置。

再去nginx download上下载最新版本,目前最新版本为1.6.0,下载后为一个ZIP压缩包,将压缩包解压至D盘根目录下

proxy12

最后进入D盘的nginx-1.6.0下的conf文件夹,用记事本打开nginx.conf,在最后面添加如下文本:

server { listen 80; server_name   www.test1.com; location / { proxy_pass http://10.10.10.88:80; }}
server { listen 80; server_name   www.test2.com; location / { proxy_pass http://10.10.10.89:80; }}

添加完成后,使用DOS窗口,如下图,切换至D盘nginx目录下,使用start nginx启动nginx,并用tasklist /fi "imagename eq nginx.exe"命令查看是否正在运行,如下图有两行信息即为启动着。这时,就可以在外网使用www.test1.comwww.test2.com域名来访问内网的这两台WEB服务器。

proxy13

nginx for windows下常用命令:

nginx start                                //启动nginx
nginx -s stop                              //停止nginx
nginx -s reload                            //重新加载配置文件
nginx -s quit                              //退出nginx
tasklist /fi "imagename eq nginx.exe"      //查看nginx进程状态

 

2、Linux下nginx反向代理的配置

拓扑和Windows版的一样,只是将代理服务器的操作系统改成了RedHat5.4 64位,如下图所示

proxy14

具体配置如下:

首先完成RedHat5.4 64位操作系统的安装,安装时,选择最大化安装,完成后,开始下载三个软件包,如下:

1. gzip 模块需要zlib库 (下载: http://www.zlib.net/)
2. rewrite 模块需要pcre库(下载: http://www.pcre.org/)
3. nginx for linux(下载: http://nginx.org/en/download.html)

下载后通过SecureFX首先将zlib-1.2.8.tar.gz、pcre-8.35.tar.gz、nginx-1.6.0.tar.gz上传至/tmp目录中。然后进入终端开始安装这三个软件包:

(1)zlib软件包安装
[root@localhost tmp]# tar -zxvf zlib-1.2.8.tar.gz
[root@localhost tmp]# cd zlib-1.2.8
[root@localhost zlib-1.2.8]# ./configure
[root@localhost zlib-1.2.8]# make
[root@localhost zlib-1.2.8]# make install

(2)pcre软件包安装
[root@localhost tmp]# tar -zxvf pcre-8.35.tar.gz
[root@localhost tmp]# cd pcre-8.35
[root@localhost pcre-8.35]# ./configure
[root@localhost pcre-8.35]# make
[root@localhost pcre-8.35]# make install

(3)nginx软件包安装
[root@localhost tmp]# tar -zxvf nginx-1.6.0.tar.gz
[root@localhost tmp]# cd nginx-1.6.0
[root@localhost nginx-1.6.0]# ./configure –prefix=/usr/local/nginx –with-http_ssl_module –with-pcre=../pcre-8.35 –with-zlib=../zlib-1.2.8
[root@localhost nginx-1.6.0]# make
[root@localhost nginx-1.6.0]# make install

完成安装后开始编辑nginx.conf

[root@localhost nginx-1.6.0]# cd /usr/local/nginx/conf
[root@localhost nginx-1.6.0]# vi nginx.conf

在最后添加如下文本:
server { listen 80; server_name   www.test1.com; location / { proxy_pass http://10.10.10.88:80; }}
server { listen 80; server_name   www.test2.com; location / { proxy_pass http://10.10.10.89:80; }}

启动nginx:
[root@localhost /]# /usr/local/nginx/sbin/nginx &
[1] 8845
[root@localhost sbin]#
[1]+  Done                    /usr/local/nginx/sbin/nginx

查看nginx进程是否正在运行:
[root@localhost sbin]# ps -ef | grep nginx
root      8846     1  0 19:41 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    8847  8846  0 19:41 ?        00:00:00 nginx: worker process     
root      8849  3339  0 19:41 pts/1    00:00:00 grep nginx
[root@localhost sbin]#

完成后,就可以在互联网使用www.test1.comwww.test2.com来测试代理是否正常。