CentOS 6.5 下Nginx服務的安裝與配置

參考網站:javascript

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

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

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

Nginx安裝部署java

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發佈。node

通常咱們都須要先裝pcre, zlib,前者爲了重寫rewrite,後者爲了gzip壓縮。linux

安裝配置以前,手動關閉所需使用虛擬機的iptables與selinux服務,命令以下nginx

service iptables stopweb

setenforce 0apache

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服務器傳入

       }

     }

}

相關文章
相關標籤/搜索