CentOS6.5环境安装nginx服务器及负载均衡配置操作详解
作者:ASIH 发布时间:2023-11-13 14:55:07
本文实例讲述了CentOS6.5环境安装nginx服务器及负载均衡配置操作。分享给大家供大家参考,具体如下:
1、下载PCRE, 是一个用C语言编写的正则表达式函数库
[root@localhost pcre-8.36]# cd /tmp/download/
[root@localhost download]# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
[root@localhost download]# tar zxvf pcre-8.36.tar.gz
2、下载zlib库
[root@localhost pcre-8.36]# cd /tmp/download/
[root@localhost download]# wget http://ncu.dl.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.gz
[root@localhost download]# tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
4、下载SSL
[root@localhost zlib-1.2.8]# cd ..
[root@localhost download]# wget http://www.openssl.org/source/openssl-1.0.1p.tar.gz
[root@localhost download]# cd openssl-1.0.1c
[root@localhost openssl-1.0.1c]# tar -zxvf openssl-1.0.1c.tar.gz
5、下载nginx
[root@localhost download]# wget http://nginx.org/download/nginx-1.2.8.tar.gz
[root@localhost download]# tar -zxvf nginx-1.2.8.tar.gz
6、安装
[root@localhost download]mv pcre-8.36 /usr/local/
[root@localhost download]mv zlib-1.2.8 /usr/local/
[root@localhost download]mv openssl-1.0.1c /usr/local/
[root@localhost download]mv nginx-1.2.8 /usr/local/
[root@localhost download]cd /usr/local/
[root@localhost local]# cd pcre-8.36
[root@localhost pcre-8.36]# ./configure&&make&&make install
[root@localhost pcre-8.36] cd ../zlib-1.2.8
[root@localhost zlib-1.2.8]# ./configure && make && make install
[root@localhost zlib-1.2.8]# cd ../openssl-1.0.1c
[root@localhost openssl-1.0.1c]# ./config && make && make install
[root@localhost openssl-1.0.1c]# cd ../nginx-1.2.8
[root@localhost nginx-1.2.8]# ./configure --prefix=/usr/local/nginx && make && make install
7、启动nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
8、测试
在确保可以连接到服务器的电脑上,浏览器输入装了nginx的机器的ip地址,会看到Welcome to nginx!的提示说明安装配配置成功了。
9、设置开机自动启动(shell脚本处理)
[root@localhost logs]# vi /etc/init.d/nginx
添加以下shell脚本。
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
保存后,设置权限让所有人可以操作。
[root@localhost ~]# chmod a+x /etc/init.d/nginx
[root@localhost logs]# /etc/init.d/nginx status
nginx (pid 2417 2416) is running...
[root@localhost logs]# /etc/init.d/nginx stop
Stopping nginx: [ OK ]
[root@localhost logs]# /etc/init.d/nginx start
Starting nginx: [ OK ]
[root@localhost logs]# /etc/init.d/nginx status
nginx (pid 2454 2452) is running...
[root@localhost ~]# vi /etc/rc.local
添加:
/etc/init.d/nginx start
重启电脑就会生效。
注意:脚本里面关于启动目录的,如果你的安装地址不一样,要进行修改。
负载均衡配置:
现有两部服务器:
192.168.137.197 (按照以上操作安装有nginx,作为转发机,虚拟机)
192.168.137.33 (无安装nginx,运行有测试用的web工程,虚拟机)
10.10.33.59 (无安装nginx,运行有测试用的web工程,本地电脑地址)
,运行有测试用的web工程
127.0.0.1 web_app
[root@a conf]# vi /usr/local/nginx/conf/nginx.conf
配置如下展示:
worker_processes 8;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errors on;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
###2012-12-19 change nginx logs
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time $remote_addr';
upstream web_app {
server 192.168.137.197:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.137.33:8080 weight=1 max_fails=2 fail_timeout=30s;
server 10.10.33.59:8080 weight=1 max_fails=2 fail_timeout=30s;
}
####chinaapp.sinaapp.com
server {
listen 80;
server_name chinaapp.sinaapp.com;
index index.jsp index.html index.htm;
#发布目录/data/www
root /data/www;
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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://web_app;
expires 3d;
}
}
}
设置host:
[root@a conf]# vi /etc/hosts
添加hosts:
127.0.0.1 web_app
启动nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
启动三部服务器的web应用:
在192.168.137.197执行指令:
[root@a PROGRAM]# curl "http://web_app/index.jsp"
测试结果如下:
常见问题及解决办法:
1、安装PCRE时提示configure: error: You need a C++ compiler for C++ support.
原因是没有安装c++编译器,采用下面的命令安装:
[root@localhost pcre-8.37]# yum install -y gcc gcc-c++
2、启动nginx失败
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
原因是64bit的系统,但是默认取了/usr/local/lib里面的包
检查方法:
[root@localhost nginx]# cd /usr/local/
[root@localhost local]# ls /lib64/ |grep pcre
libpcre.so.0
libpcre.so.0.0.1
[root@localhost local]# ls /lib/ |grep pcre
说明缺失的包在lib64
设置软连接来解决:
[root@localhost local]# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
[root@localhost local]# cd nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3、启动报错:
报错:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
解决办法:sudo fuser -k 80/tcp
端口被占用,关闭占用端口
希望本文所述对大家CentOS服务器配置有所帮助。
来源:https://blog.csdn.net/hanzheng260561728/article/details/51884108


猜你喜欢
- 前言相信有少许人包括我曾经认为 ssh 就是用来免密登录的,其实不然,它可涉及到非常多的东西,像 sftp / scp / winscp /
- 一般ARP攻击的对治方法现在最常用的基本对治方法是“ARP双向绑定”。由于ARP攻击往往不是病毒造成
- 主要使用request对象的ServerVariables属性,通过它来获得环境变量的值。使用的语法为:Request.ServerVari
- 1.新建用户wwweee000[root@localhost ~]# useradd wwweee000[root@localhost ~]
- keepalived直译就是保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,用来防止单点故障(单点故障是指一旦某一点出现故障就
- (一)最简单的一个网站,可能说是demo更加合适一些,部署上一台计算机上也可以正常运转。通常情况,这种部署方式是效率最高的。但是为什么说需要
- 1、购买云服务器注册帐号,在产品页面选择合适的服务器,进入详细页面选择配置,购买。购买完成后进入管理控制台——云虚拟主机、ECS云服务器查看
- 一、这里太专业,你插不上嘴?答:没你想象的那么专业。我学中文的,洪波也是学中文的,我们都能如鱼得水,你一定也行。陈一舟学物理的,我在武汉问他
- 1、博大精深的美食文化中国美食自古以来在就在全世界有着重要的地位,拥有6万多种传统菜点、2万多种工业食品、五光十色的筵宴和流光溢彩的风味流派
- 1、基于ip地址的虚拟主机Listen 80<VirtualHost 172.20.30.40> &nbs
- Linux免费开源,建站基本上都是选择Linux系统,而且Linux VPS比Windows VPS要便宜不少,成本大大降低,除非程序有特殊
- 使用xshell连接服务器时,出现了“WARNING! The remote SSH server rejected X
- 微软技术支持团队一名经理表示,公司计划在今年10月底之前推出IE8 Beta 2 软件。该经理同时警告网站在预览新浏览器时可能出现被“阻断”
- 架设sendmail服务器 这次我们来学习Linux系统中的Sendmail服务器的配置。(邮件服务器的IP192.168.1.2 主机域名
- 终于装上了时下超热门的Win7操作系统,小编在体验的同时却发现, Win7系统自带的图片处理软件不够简易实用,所以小编在此推荐使用与Win7
- 内容摘要:以汉语全拼为标识的域名,因其特有的易记性而被国内众多网站广泛应用,近两年先后有土豆网、优酷网及蚂蚁网等大型网站高价收购应用全拼域名
- 本文主要介绍的是在CentOS 7.2安装部署邮件服务器(Postfix)的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:Post
- 你可能会因为种种原因而想在 FreeBSD 服务器上彻底禁用 sendmail,那么你可以尝试这样∶1,如果你不想重新启动服务器的
- 本例以整合home为例home的登录系统中加入的防伪码主要是三个define('IN_UCHOME','asdfas
- 当互联网吵吵嚷嚷的进入2.0时代,当互联网的技术不再是那么高不可攀,当复制变成家常便饭,互联网热闹起来了myspace火了,中国冒出更多的m