---
title: "nginx安装及负载均衡配置"
author: "beyond429"
date: "2012-11-29T10:35:08Z"
tags: []
categories: ["开源软件"]
slug: "nginx"
draft: false
lastmod: "2012-11-29T10:35:08Z"
id: 1738058
url: "https://beyond429.iteye.com/blog/1738058"
---

原文地址： http://www.micmiu.com/architecture/cluster/nginx-install-lb/

 

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

- Nginx 官网：     [http://nginx.org/](http://nginx.org/)
- Nginx 中文wiki：[http://wiki.nginx.org/NginxChs](http://wiki.nginx.org/NginxChs)
- Nginx 中文网站：[http://www.howtocn.org/nginx](http://www.howtocn.org/nginx)

\[一]、安装步骤

1\. 安装PCRE library

安装nginx前需要确保系统中已经安装PCRE包,PCRE library是HTTP Rewrite模块（即是url静态化的包）。

PCRE library 详细可查阅其官网： [http://www.pcre.org](http://www.pcre.org/) 。

目前最新版本是8.12，本文附件提供下载：[pcre-8.12.tar.gz](http://www.micmiu.com/wp-content/uploads/2012/03/pcre-8.12.tar.gz)

`1` `#tar -zxvf pcre-8.12.tar.gz`

`2` `#cd pcre-8.12.tar.gz`

`3` `#./configure`

`4` `#make`

`5` `#make install`

常规标准的安装步骤。

2\. 安装nginx

截止目前最新版本为1.0.5 ，可去官网下载，附件提供下载:[nginx-1.0.5.tar.gz](http://www.micmiu.com/wp-content/uploads/2012/03/nginx-1.0.5.tar.gz)

`1` `#tar -zxvf nginx-1.0.5.tar.gz`

`2` `#cd nginx-1.0.5`

`3` `#./configure`

`4` `#make`

`5` `#make install`

**Tips:** 有关nginx的编译选项可参见：[nginx编译选项详解](http://www.micmiu.com/enterprise-app/server/nginx-configure/)（ http://www.micmiu.com/enterprise-app/server/nginx-configure/）

\[二]、负载均衡配置示例

   1. 测试环境的参数：

- 192.168.8.183 centos5        nginx1.0.5
- 192.168.8.184 ubuntu10.10 tomcat-6.0.29
- 192.168.8.185 ubuntu10.10 tomcat-6.0.29

**ps：** tomcat的配置启动这里就不再详述了.

2\. 修改nginx的配置文件：/usr/local/nginx/conf/nginx.conf

`1` `upstream my-web-server {`

`2` `server 192.168.8.184:8088;`

`3` `server 192.168.8.185:8088;`

`4` `}`

`5`  

`6` `server {`

`7` `listen       8088;`

`8` `server_name  localhost;`

`9`  

`10` `#charset koi8-r;`

`11`  

`12` `#access_log  logs/host.access.log  main;`

`13`  

`14` `location / {`

`15` `proxy_pass         http://my-web-server;`

`16` `proxy_set_header   Host             $host;`

`17` `proxy_set_header   X-Real-IP        $remote_addr;`

`18` `proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;`

`19` `}`

`20`  

`21` `#error_page  404              /404.html;`

`22`  

`23` `error_page   500 502 503 504  /50x.html;`

`24` `location = /50x.html {`

`25` `root   html;`

`26` `}`

`27`  

`28` `}`

 **Tips：**

1\. 注意配置文件中的参数对应关系：upstream: my-back-server 和 proxy\_pass:  http://my-back-server ;

2\. upstream 中的server可设置多个类似：server 192.168.8.184:8088;

如果是80端口可以不用加端口号，直接写成 server 192.168.8.184;即可

\[三]、测试

1.修改tomcat下webapp/ROOT/index.html，增加特定标识以便测试。

2.浏览器打开： http://192.168.8.183:8088 刷新页面，一起正常会交替显示如下信息：

[![](http://www.micmiu.com/wp-content/uploads/2012/03/nginx-lb-01.png "nginx-lb-01")](http://www.micmiu.com/wp-content/uploads/2012/03/nginx-lb-01.png)

[![](http://www.micmiu.com/wp-content/uploads/2012/03/nginx-lb-02.png "nginx-lb-02")](http://www.micmiu.com/wp-content/uploads/2012/03/nginx-lb-02.png)

3\. 如果停掉其中某一台tomcat，再次访问刷新时，只显示一个tomcat的信息.

到此nginx安装及负载均衡配置演示结束。