微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx编译安装与apache动静分离共存设置及负载均衡设置

原文链接http://www.cnblogs.com/Peacha/p/4673492.html

Nginx对于静态文件强大的响应能力一定程度上弥补了apache环境的不足,使用Nginx做反向代理,一部分为了提高静态文件的相应能力,另 外可以使用Nginx负载均衡来搭2至3台的apache服务器,这样在相当长的时间段内解决了初建团队的业务蓬勃发展所遇到的服务器资源问题.

1,安装依赖.     yum -y install wget zip unzip zlib zlib-devel pcre pcre-devel  pcre* openssl openssl-devel perl perl-devel  perl-ExtUtils-Embed.   2,下载源码包       wget http://nginx.org/download/nginx-1.6.1.tar.gz   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
esac

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;
}

#将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;
}

11,配置Nginx负载均衡

  vi /etc/Nginx/Nginx.conf在http节点里添加:

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

keepalive_timeout 120;

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;
}

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指向源码包,而不是安装后包,  

转载于:https://www.cnblogs.com/Peacha/p/4673492.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐