CentOS 6.5 下Nginx服务的安装与配置

参考网站:

http://www.cnblogs.com/zhuhongbao/archive/2013/06/04/3118061.html

http://www.cnblogs.com/jilianggqq/p/4141641.html

http://www.360doc.com/content/16/0804/14/35569207_580737016.shtml

Nginx安装部署

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

一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。

安装配置之前,手动关闭所需使用虚拟机的iptables与selinux服务,命令如下

service iptables stop

setenforce 0

1         Nginx的安装

Nginx安装服务器IP:192.168.42.2

1.1         选定源码目录

选定目录 /usr/local/

cd /usr/local/

1.2         安装pcre库

cd /usr/local/

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

tar -zxvf pcre-8.21.tar.gz

cd pcre-8.21

./configure

make

make install

1.3         安装zlib库

cd /usr/local/

wget http://zlib.net/zlib-1.2.8.tar.gz

tar -xvf zlib-1.2.8.tar.gz

cd zlib-1.2.8

./configure

make

make install

1.4         安装ssl

cd /usr/local/

wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz

tar -zxvf openssl-1.0.1c.tar.gz

cd openssl-1.0.1c

./config

make

make install

1.5         安装nginx

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:

 

cd /usr/local/

wget http://nginx.org/download/nginx-1.2.8.tar.gz

tar -zxvf nginx-1.2.8.tar.gz

cd nginx-1.2.8 

./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre-8.21

--with-zlib=/usr/local/zlib-1.2.7

make

make install

--with-pcre=/usr/src/pcre-8.21指的是pcre-8.21 的源码路径。

--with-zlib=/usr/src/zlib-1.2.7指的是zlib-1.2.7 的源码路径。

1.6         启动

确保系统的 80 端口没被其他程序占用,

/usr/local/nginx/sbin/nginx(启动)

/usr/local/nginx/sbin/nginx -s reload(重启)启动后才能执行重启操作。

/usr/local/nginx/sbin/nginx -s stop(关闭)

检查是否启动成功:

netstat -ano|grep 80 有结果输入说明启动成功

1.7          测试

打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。界面如下所示:

 

2         Tomcat的安装和配置

Tomcat安装服务器IP:192.168.42.4

2.1         JDK的安装与配置

安装之前检查下是否已经安装了openJDK,如果已安装,建议用yum remove 卸载掉。

[root@xldrooto ~]# rpm -qa | grep java

下载合适版本的JDK,网址如下:

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

新建java文件夹

[root@xldrooto ~]# mkdir /usr/java

将下载的JDK通过Xftp传输到java目录下

进入java目录:

[root@xldrooto ~]# cd /usr/java/

[root@xldrooto java]# ls

jdk-8u101-linux-x64.gz

[root@xldrooto java]# tar -zxvf jdk-8u101-linux-x64.gz

通过以上步骤,jdk就已经全部安装完成了。下面,就是环境变量的配置。

 

编辑/etc/profile

[root@xldrooto ~]# vim /etc/profile

在底部添加如下内容:

JAVA_HOME=/usr/java/jdk1.8.0_101
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
export PATH JAVA_HOME CLASSPATH

 

以上,环境变量配置完成。需要注意的是,PATH在配置的时候,一定要把$JAVA_HOME/bin放在前面,不然使用java命令时,系统会找到以前的java,再不往下找了。这样java这个可执行文件运行的目录其实不在$JAVA_HOME/bin下,而在其它目录下,会造成很大的问题。

还要注意,以前其它教程写的CLASSPATH=$JAVA_HOME/lib.tools.jar,不知道以前的版本是怎么样的,现在的版本是没有这样的jar包的。

最后使用source /etc/profile让profile文件立即生效。

[root@xldrooto ~]# source /etc/profile

 

查看所安装的JDK的版本:

[root@xldrooto ~]# java -version

java version "1.8.0_101"

Java(TM) SE Runtime Environment (build 1.8.0_101-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

 

看能否使用javac命令:

[root@xldrooto ~]# javac

Usage: javac <options> <source files>

where possible options include:

  -g                         Generate all debugging info

  -g:none                    Generate no debugging info

  -g:{lines,vars,source}     Generate only some debugging info

  -nowarn                    Generate no warnings

  -verbose                   Output messages about what the compiler is doing

  -deprecation               Output source locations where deprecated APIs are used

  -classpath <path>          Specify where to find user class files and annotation processors

  -cp <path>                 Specify where to find user class files and annotation processors

  -sourcepath <path>         Specify where to find input source files

  -bootclasspath <path>      Override location of bootstrap class files

  -extdirs <dirs>            Override location of installed extensions

  -endorseddirs <dirs>       Override location of endorsed standards path

  -proc:{none,only}          Control whether annotation processing and/or compilation is done.

  -processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process

  -processorpath <path>      Specify where to find annotation processors

  -parameters                Generate metadata for reflection on method parameters

  -d <directory>             Specify where to place generated class files

  -s <directory>             Specify where to place generated source files

  -h <directory>             Specify where to place generated native header files

  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files

  -encoding <encoding>       Specify character encoding used by source files

  -source <release>          Provide source compatibility with specified release

  -target <release>          Generate class files for specific VM version

  -profile <profile>         Check that API used is available in the specified profile

  -version                   Version information

  -help                      Print a synopsis of standard options

  -Akey[=value]              Options to pass to annotation processors

  -X                         Print a synopsis of nonstandard options

  -J<flag>                   Pass <flag> directly to the runtime system

  -Werror                    Terminate compilation if warnings occur

  @<filename>                Read options and filenames from file

没有报-bash: javac: command not found错误,说明JDK安装配置成功。

2.2         安装两个或多个Tomcat

Tomcat服务器:

             192.168.42.4:8080

             192.168.42.4:8081

将下载的Tomcat源码包通过Xftp传输到/usr/local目录下

进入/usr/local目录下:

[root@xldrooto ~]# cd /usr/local/

[root@xldrooto local]# tar -zxvf apache-tomcat-7.0.70.tar.gz

[root@xldrooto local]# mv apache-tomcat-7.0.70 Tomcat7-1

[root@xldrooto local]# cp –r Tomcat7-1 Tomcat7-2 //需要安装几个Tomcat就复制几份

第一个Tomcat7-1里面的不必动,这里我们只修要修改第二个之后的配置文件;

[root@xldrooto local]# vim /usr/local/Tomcat7-2/conf/server.xml

修改下列对应部分内容

<Server port="18005" shutdown="SHUTDOWN">#关闭端口

<Connector port="8081" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="18443" />#Web端口

<Connector port="8019" protocol="AJP/1.3" redirectPort="18443" />#监听端口

 

修改Toncat的显示界面:

[root@xldrooto ~]# vim /usr/local/Tomcat7-1/webapps/ROOT/index.jsp

改为如下内容:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>欢迎界面</title>

</head>

<body>

  welcome to Tomcat:8080!<br />

</body>

</html>

 

[root@xldrooto ~]# vim /usr/local/Tomcat7-1/webapps/ROOT/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Tomcat</title>

</head>

<body>

  welcome to Tomcat:8081<br />

</body>

</html>

修改Tomcat是为了测试时切换操作明显。

当在网页上输入192.168.42.4:8080出现下述界面:

 

当在网页上输入192.168.42.4:8081时出现下述界面:

 

表示Tomcat安装成功。

3         Nginx实现负载均衡

nginx服务器IP:192.168.42.2

                192.168.42.3(设置同192.168.42.2)

Tomcat服务器IP:192.168.42.4

在192.168.42.2上面安装nginx,在192.168.42.4上面安装两个tomcat并设置不同的端口,为了能看出区别,修改tomcat的默认页面的内容,这些内容在已在前面章节完成。

本节主要讲解的是在nginx代理服务器上做的配置,过程如下:

[root@xldwhj ~]# vim /usr/local/nginx/conf/nginx.conf

#运行用户

user root;

#启动进程,通常设置成和cpu的数量相等

worker_processes  1;

#全局错误日志及PID文件

error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#工作模式及连接数上限

events {

    #use epoll;#epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是linux2.6

#以上内核,可以大大提高nginx的性能

#单个后台worker process进程的最大并发链接数

worker_connections  1024;

# multi_accept on;

}

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

    #设定mime类型,类型由mime.type文件定

    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"';

#设定日志格式

access_log  logs/access.log;

#sendfile指令指定nginx是否调用sendfile函数(zero copy 方式)来输出文件,

#对于普通应用,必须设为on,如果用来进行下载等应用磁盘IO重负载应用,可设置#为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.

    sendfile        on;

#tcp_nopush     on;

#连接超时时间

    #keepalive_timeout  0;

keepalive_timeout  65;

#开启gzip压缩

#gzip  on;

#gzip_disable "MSIE [1-6]\.(?!.*SV1)";

#设定请求缓冲
#client_header_buffer_size   1k;

#large_client_header_buffers  4 4k;

    include     conf/app1.conf;  //在conf目录下新建conf文件夹,包含app1.conf

}

 

将需要负载的服务器进行分组,不但便于配置文件的管理,而且也使后续需要添加负载服务器时更加容易。

[root@xldwhj ~]# vim /usr/local/nginx/conf/conf/app1.conf

#设定负载均衡的服务器列表,配置http节点如下:

upstream app1{

        #weigth参数表示权值,权值越高被分配到的几率越大。

        #server 192.168.42.5:80 weight=5;

        server 192.168.42.4:8080;

        server 192.168.42.4:8081;

    }

 

server{

#侦听80端口

listen        80;

server_name   192.168.42.4;

#设定本虚拟主机的访问日志
#access_log logs/www.xx.com.access.log  main;

#默认请求

location /app1 {

#请求转向定义的服务器列表

proxy_pass         http://app1/;

#定义首页索引文件的名称

#index index.php index.html index.htm;

#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP

proxy_set_header   host              $host;

proxy_set_header   X-Real-IP         $remote_addr;

proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;

               }

      }

在浏览器中输入192.168.42.2/app1,点击刷新,实现下述页面交替出现即为成功:

 

4         Nginx+Keepalived实现高可用负载均衡

主NginxIP:192.168.42.2

从NginxIP:192.168.42.3

Tomcat服务器:

192.168.42.4:8080

              192.168.42.4:8081

虚拟IP地址VIP:192.168.42.100

Keepalived的安装参考《CentOS 6.5 下Keepalived服务的安装与配置》

Nginx负载均衡在第3章已经介绍,这里与前面基本相同,主要介绍Keepalived的配置。

4.1         主Nginx的Keepalived配置

配置文件修改如下,具体含义可参考

! Configuration File for keepalived

global_defs {

  router_id NodeA

}

vrrp_instance VI_1 {

     state BACKUP #都修改成BACKUP

     interface eth0#虚拟IP绑定在网卡0下

     virtual_router_id 51

     priority 100#优先级

     advert_int 1

     authentication {

          auth_type PASS

          auth_pass 1111

     }

     virtual_ipaddress {

          192.168.42.100

     }

}

virtual_server 192.168.42.100 80 {

      delay_loop 6

      lb_algo wrr

      lb_kind DR

      nat_mask 255.255.255.0

      persistence_timeout 50

      protocol TCP

      real_server 192.168.42.2 80 {

      weight 1

      notify_down /root/shutdown.sh #检测到Nginx服务down后执行的脚本

      TCP_CHECK {

          connect_timeout 10

          nb_get_retry 3

          connect_port 80

                }

      }

}

 

编写脚本文件shutdowm.sh

[root@xldwhj keepalived]# touch /root/shutdown.sh

[root@xldwhj keepalived]# vim /root/shutdown.sh

#!/bin/bash

service keepalived stop

[root@xldwhj keepalived]# chmod 744 /root/shutdown.sh

4.2         从Nginx的Keepalived的配置

! Configuration File for keepalived

global_defs {

  router_id NodeB

}

vrrp_instance VI_1 {

     state BACKUP #都修改成BACKUP

     interface eth0#虚拟IP绑定在网卡0下

     virtual_router_id 51

     priority 99#优先级

     advert_int 1

     authentication {

          auth_type PASS

          auth_pass 1111

     }

     virtual_ipaddress {

          192.168.42.100

     }

}

virtual_server 192.168.42.100 80 {

      delay_loop 6

      lb_algo wrr

      lb_kind DR

      nat_mask 255.255.255.0

      persistence_timeout 50

      protocol TCP

      real_server 192.168.42.3 80 {

      weight 1

      notify_down /root/shutdown.sh #检测到Nginx服务down后执行的脚本

      TCP_CHECK {

          connect_timeout 10

          nb_get_retry 3

          connect_port 80

                }

      }

}

脚本文件编写同上节。

4.3         测试

分别从主从服务器上启动nginx与keepalived服务

主Nginx执行下属命令:

[root@xldroot ~]# ip a

[root@xldwhj ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ef:76:a8 brd ff:ff:ff:ff:ff:ff

    inet 192.168.42.2/24 brd 192.168.42.255 scope global eth0

    inet 192.168.42.100/32 scope global eth0

    inet6 fe80::20c:29ff:feef:76a8/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ef:76:b2 brd ff:ff:ff:ff:ff:ff

    inet 192.168.184.128/24 brd 192.168.184.255 scope global eth1

    inet6 fe80::20c:29ff:feef:76b2/64 scope link

       valid_lft forever preferred_lft forever虚拟IP已经自动悬浮于网卡0下

从Nginx执行下述命令:

[root@xldroot ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:53:74:85 brd ff:ff:ff:ff:ff:ff

    inet 192.168.42.3/24 brd 192.168.42.255 scope global eth0

    inet6 fe80::20c:29ff:fe53:7485/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:53:74:8f brd ff:ff:ff:ff:ff:ff

    inet 192.168.184.130/24 brd 192.168.184.255 scope global eth1

    inet6 fe80::20c:29ff:fe53:748f/64 scope link

       valid_lft forever preferred_lft forever

未发现虚拟IP地址。

在网页中输入192.168.42.100/app1,之后点击刷新实现下属界面交替。

 

关闭主Nginx的Nginx服务,主Nginx执行下述命令

[root@xldwhj ~]# /usr/local/nginx/sbin/nginx -s stop

[root@xldwhj ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ef:76:a8 brd ff:ff:ff:ff:ff:ff

    inet 192.168.42.2/24 brd 192.168.42.255 scope global eth0

    inet6 fe80::20c:29ff:feef:76a8/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ef:76:b2 brd ff:ff:ff:ff:ff:ff

    inet 192.168.184.128/24 brd 192.168.184.255 scope global eth1

    inet6 fe80::20c:29ff:feef:76b2/64 scope link

       valid_lft forever preferred_lft forever

虚拟IP已不存在。

在从Nginx上执行下述命令:

[root@xldroot ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:53:74:85 brd ff:ff:ff:ff:ff:ff

    inet 192.168.42.3/24 brd 192.168.42.255 scope global eth0

    inet 192.168.42.100/32 scope global eth0

    inet6 fe80::20c:29ff:fe53:7485/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:53:74:8f brd ff:ff:ff:ff:ff:ff

    inet 192.168.184.130/24 brd 192.168.184.255 scope global eth1

    inet6 fe80::20c:29ff:fe53:748f/64 scope link

       valid_lft forever preferred_lft forever

虚拟IP地址已悬浮于网卡0之下。

在浏览器中输入192.168.42.100/app1实现下述界面交替切换。

 

在主Nginx上重新启动服务:

[root@xldwhj ~]# /usr/local/nginx/sbin/nginx

[root@xldwhj ~]# service keepalived start

Starting keepalived:                                       [  OK  ]

[root@xldwhj ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ef:76:a8 brd ff:ff:ff:ff:ff:ff

    inet 192.168.42.2/24 brd 192.168.42.255 scope global eth0

    inet 192.168.42.100/32 scope global eth0

    inet6 fe80::20c:29ff:feef:76a8/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ef:76:b2 brd ff:ff:ff:ff:ff:ff

    inet 192.168.184.128/24 brd 192.168.184.255 scope global eth1

    inet6 fe80::20c:29ff:feef:76b2/64 scope link

       valid_lft forever preferred_lft forever

可见,虚拟IP地址从新回到主Nginx服务器上。

测试如上所示,则配置成功。

5         Nginx配置文件详细说明

Nginx配置文件详细说明

在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集于网络.

#运行用户

user www-data;   

#启动进程,通常设置成和cpu的数量相等

worker_processes  1;

#全局错误日志及PID文件

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

#工作模式及连接数上限

events {

    use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能

    worker_connections  1024;#单个后台worker process进程的最大并发链接数

    # multi_accept on;

}

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

     #设定mime类型,类型由mime.type文件定义

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    #设定日志格式

    access_log    /var/log/nginx/access.log;

    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,

    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.

    sendfile        on;

    #tcp_nopush     on;

    #连接超时时间

    #keepalive_timeout  0;

    keepalive_timeout  65;

    tcp_nodelay        on;

    #开启gzip压缩

    gzip  on;

    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    #设定请求缓冲

    client_header_buffer_size    1k;

    large_client_header_buffers  4 4k;

    include /etc/nginx/conf.d/*.conf;

    include /etc/nginx/sites-enabled/*;

    #设定负载均衡的服务器列表

     upstream mysvr {

    #weigth参数表示权值,权值越高被分配到的几率越大

    #本机上的Squid开启3128端口

    server 192.168.8.1:3128 weight=5;

    server 192.168.8.2:80  weight=1;

    server 192.168.8.3:80  weight=6;

    }

   server {

    #侦听80端口

        listen       80;

        #定义使用www.xx.com访问

        server_name  www.xx.com;

        #设定本虚拟主机的访问日志

        access_log  logs/www.xx.com.access.log  main;

    #默认请求

    location / {

          root   /root;      #定义服务器的默认网站根目录位置

          index index.php index.html index.htm;   #定义首页索引文件的名称

          fastcgi_pass  www.xx.com;

         fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;

          include /etc/nginx/fastcgi_params;

        }

    # 定义错误提示页面

    error_page   500 502 503 504 /50x.html; 

        location = /50x.html {

        root   /root;

    }

    #静态文件,nginx自己处理

    location ~ ^/(images|javascript|js|css|flash|media|static)/ {

        root /var/www/virtual/htdocs;

        #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。

        expires 30d;

    }

    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.

    location ~ \.php$ {

        root /root;

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;

        include fastcgi_params;

    }

    #设定查看Nginx状态的地址

    location /NginxStatus {

        stub_status            on;

        access_log              on;

        auth_basic              "NginxStatus";

        auth_basic_user_file  conf/htpasswd;

    }

    #禁止访问 .htxxx 文件

    location ~ /\.ht {

        deny all;

    }

    

     }

}

以上是一些基本的配置,使用Nginx最大的好处就是负载均衡

如果要使用负载均衡的话,可以修改配置http节点如下:

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

     #设定mime类型,类型由mime.type文件定义

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    #设定日志格式

    access_log    /var/log/nginx/access.log;

    #省略上文有的一些配置节点

    #

    #设定负载均衡的服务器列表

     upstream mysvr {

    #weigth参数表示权值,权值越高被分配到的几率越大

    server 192.168.8.1x:3128 weight=5;#本机上的Squid开启3128端口

    server 192.168.8.2x:80  weight=1;

    server 192.168.8.3x:80  weight=6;

    }

 

   upstream mysvr2 {

    #weigth参数表示权值,权值越高被分配到的几率越大

    server 192.168.8.x:80  weight=1;

    server 192.168.8.x:80  weight=6;

    }

 

   #第一个虚拟服务器

   server {

    #侦听192.168.8.x的80端口

        listen       80;

        server_name  192.168.8.x;

 

      #对aspx后缀的进行负载均衡请求

    location ~ .*\.aspx$ {

 

         root   /root;      #定义服务器的默认网站根目录位置

          index index.php index.html index.htm;   #定义首页索引文件的名称

 

          proxy_pass  http://mysvr ;#请求转向mysvr 定义的服务器列表

 

          #以下是一些反向代理的配置可删除.

 

          proxy_redirect off;

 

          #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP

          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;  #缓冲区代理缓冲用户端请求的最大字节数,

          proxy_connect_timeout 90;  #nginx跟后端服务器连接超时时间(代理连接超时)

          proxy_send_timeout 90;        #后端服务器数据回传时间(代理发送超时)

          proxy_read_timeout 90;         #连接成功后,后端服务器响应时间(代理接收超时)

          proxy_buffer_size 4k;             #设置代理服务器(nginx)保存用户头信息的缓冲区大小

          proxy_buffers 4 32k;               #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置

          proxy_busy_buffers_size 64k;    #高负荷下缓冲大小(proxy_buffers*2)

          proxy_temp_file_write_size 64k;  #设定缓存文件夹大小,大于这个值,将从upstream服务器传入

       }

     }

}

posted @ 2016-11-07 15:29  xldwhj  阅读(1797)  评论(0编辑  收藏  举报