nginx編譯安裝與apache動靜分離共存設置及負載均衡設置

nginx對於靜態文件強大的響應能力必定程度上彌補了apache環境的不足,使用nginx作反向代理,一部分爲了提升靜態文件的相應能力,另 外能夠使用nginx作負載均衡來搭2至3臺的apache服務器,這樣在至關長的時間段內解決了初建團隊的業務蓬勃發展所遇到的服務器資源問題.php

1,安裝依賴.
    yum -y install wget zip unzip zlib zlib-devel pcre pcre-devel  pcre* openssl openssl-devel perl perl-devel  perl-ExtUtils-Embed.
 
2,下載源碼包
 
3,  解壓
     tar -zxvf nginx-1.6.1.tar.gz 
     cd nginx-1.6.1/
 
4,創建必須目錄
  mkdir /var/tmp/nginx/ /var/log/nginx/ /var/run/nginx/
 
5,編譯安裝
  ./configure    --prefix=/usr   --sbin-path=/usr/sbin/nginx   --conf-path=/etc/nginx/nginx.conf   --error-log-path=/var/log/nginx/error.log   --pid-path=/var/run/nginx.pid    --lock-path=/var/lock/nginx.lock     --http-log-path=/var/log/nginx/access.log   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --with-http_ssl_module   --with-http_flv_module   --with-http_gzip_static_module --with-http_realip_module  --with-http_stub_status_module   --with-http_addition_module --with-http_sub_module  --with-http_dav_module  --with-http_perl_module  --with-ld-opt="-Wl,-E"  --with-mail
  make &&  make install
 
6,爲nginx創建用戶
   /usr/sbin/useradd -c "Nginx user" -s /bin/false -r -d /var/lib/nginx nginx
 
7,創建啓動腳本
  vi /etc/init.d/nginx
  輸入如下內容:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*/1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esaccss

8,設置權限
  chmod 755 /etc/init.d/nginx
 
9,增長到系統服務
  chkconfig --add nginx
  之後的操做能夠使用centos7的systemctl工具了
   systemctl start nginx
   systemctl stop nginx
   systemctl restart nginx
 
10,設置nginx和apache的動靜分離
  編輯apache的配置文件,將Listen改成88端口
  vi /etc/nginx/nginx.conf 在第一個server配置中增長如下行

location / {
root /var/www/html;#apache的網站根目錄
index index.php index.html index.htm;
}html

#將php文件請求分發給後端的apache
location ~ \.php$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:88;
}
#將圖像和靜態文件由nginx處理
location ~ \.*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
root /var/www/html;
expires 15d;
}
#將js文件由nginx處理
location ~ \.*.(js|css)$ {
expires 1h;
}node

11,配置Nginx負載均衡nginx

  vi /etc/nginx/nginx.conf在http節點裏添加:apache

http
{
include mime.types;
default_type application/octet-stream;後端

keepalive_timeout 120;centos

tcp_nodelay on;服務器

upstream server1 {
server 192.168.1.2:80;
server 192.168.1.3:80;
server 192.168.1.4:80;
server 192.168.1.5:80;
}app

upstream server2 {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
}

server
{
listen 80;
server_name server1;

location / {
proxy_pass http://www.zyan.cc;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}

server
{
listen 80;
server_name server2;

location / {
proxy_pass http://blog.zyan.cc;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}

另外:若是編譯nginx有錯誤的時候
    
       the HTTP rewrite module requires the PCRE library.//安裝pcre
                    the HTTP gzip module requires the zlib library//安裝zlib
                    make[1]: *** [/usr/local/pcre/Makefile] Error 127//–with-pcre=DIR指向源碼包,而不是安裝後包,
相關文章
相關標籤/搜索