---
title: "nginx安装部署"
author: "no_bao"
date: "2014-02-11T13:34:23Z"
tags: []
categories: ["开源软件"]
slug: "nginx"
draft: false
lastmod: "2014-02-11T15:01:23Z"
id: 2014492
url: "https://it586.iteye.com/blog/2014492"
---

## **解压pcre**

tar zxf pcre-8.30.tar.gz

cd pcre-8.30/

./configure

make && make install

 

安装pcre库是为了兼容nginx rewrite

 

## **解压openssl**

tar zxf openssl-1.0.0a.tar.gz

 

## **解压nginx**

useradd nginx -M -s /sbin/nologin

添加用户（不允许登录）

 

tar -zxf nginx-1.1.1.tar.gz

cd nginx-1.1.1

./configure --user=nginx --prefix=/myappsoft/nginx-1.1.1 --with-openssl=../openssl-1.0.0a/ --with-http\_stub\_status\_module --with-http\_ssl\_module --with-http\_realip\_module

make && make install

 

## **检查nginx语法**

\[root@localLinuxMySql nginx-1.1.1]# /myappsoft/nginx-1.1.1/sbin/nginx -t

nginx: the configuration file /myappsoft/nginx-1.1.1/conf/nginx.conf syntax is ok

nginx: configuration file /myappsoft/nginx-1.1.1/conf/nginx.conf test is successful

 

## **设置全局变量及开机启动**

#增加软链接，方便升级

\[root@localLinuxMySql nginx-1.1.1]# ln -s /myappsoft/nginx-1.1.1 /myappsoft/nginx 

#查看软链接是否成功，如果闪动就没成功

\[root@localLinuxMySql nginx-1.1.1]# ll -d /myappsoft/nginx

lrwxrwxrwx 1 root root 23 Feb 10 19:05 /myappsoft/nginx -&gt; /myappsoft/nginx-1.1.1

#将Nginx命令加入系统全局变量,启动nginx

\[root@localLinuxMySql nginx-1.1.1]# echo 'export PATH=$PATH:/myappsoft/nginx/sbin' &gt;&gt;/etc/profile

#使全局变量修改生效

\[root@localLinuxMySql nginx-1.1.1]# source /etc/profile

#加入开机自启动

\[root@localLinuxMySql nginx-1.1.1]# echo '/myappsoft/nginx/sbin/nginx' &gt;&gt; /etc/rc.local

 

## **配置内网服务器指向及负载均衡**

#找到/myappsoft/nginx-1.1.1/conf目录下nginx.conf文件，修改以下内容

upstream webserver1 {

server 192.168.11.12:8091 weight=3;

}

upstream webserver2 {

server 192.168.0.12:8092 weight=3

}

upstream webserver3 {

server 192.168.0.12:8092 weight=3

server 192.168.0.13:8092 weight=3

}

server {

listen 8080;

server\_name 192.168.1.195;

location /testapp {

proxy\_pass http://webserver1;

}

location /testapp2 {

proxy\_pass http://webserver2;

}

location /testapp3 {

proxy\_pass http://webserver3;

}

}

参数解释：

upstream webserver { } 定义真实服务器组

server {} 定义一个服务配置

listen 8080 监听8080端口

server\_name 监听的地址或IP

location / 匹配server\_name后的url或IP

proxy\_pass 代理参数，后接upstream定义的服务器组

 

 

 

 

 

 

## **启动/重启nginx**

#启动nginx

\[root@localLinuxMySql nginx-1.1.1]# nginx

#重启nginx

\[root@localLinuxMySql nginx-1.1.1]# nginx -s reload

## 查看监听端口

 

\[root@localLinuxMySql nginx-1.1.1]# netstat -plnt |grep 8080

tcp        0      0 0.0.0.0:8080                  0.0.0.0:*                   LISTEN      32716/nginx: master

 

 

 

## 测试访问

访问：http://192.168.1.195:8080/testapp 实际是映射地址：http://192.168.11.12:8091/testapp

访问：http://192.168.1.195:8080/testapp2 实际是映射地址：http://192.168.0.12:8092/testapp2

 

testapp3 可以映射两台服务器地址 实现负载均衡