您的位置:首页 > 运维架构 > Nginx

用 KVM 搭建web集群实验笔记 - 安装Nginx 和配置Web服务

2016-09-07 10:11 1151 查看
Nginx是一个开源的,支持高性能、高并发的WWW服务和代理服务软件。Nginx的三个主要功能: 1)作为Web软件,2)反向代理或负载均衡服务,3)前端业务数据缓存服务。

Nginx作为Web服务器的主要特点:1)支持高并发(数万并发连接),2)资源占用少(3万连接,200M),3)反向代理(相当于Haproxy),4)缓存功能(类似Squid),5)支持异步网络I/O事件模型epoll。

1.准备工作

克隆一个虚拟机,设置好IP、主机名和yum源

安装依赖包 yum -y install gcc openssl openssl-devel pcre pcre-devel

安装额外的软件 yum -y install lsof tree wget

下载 http://nginx.org/download/nginx-1.6.3.tar.gz
增加用户 useradd nginx -s /sbin/nologin -M

2. 编译安装

解压缩 tar xvf nginx-1.6.3.tar.gz

cd nginx-1.6.3

./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make

make install

用tree命令进行查看nginx的目录结构

3.配置虚拟主机

编辑/usr/local/nginx/conf/nginx.conf 在http{} 配置3个虚拟主机(基于域名)

server {
listen        80;
server_name   www.mydream.com;
location / {
root   html/www;
index  index.html index.htm;
}
}

server {
listen        80;
server_name   bbs.mydream.com;
location / {
root   html/bbs;
index  index.html index.htm;
}
}

server {
listen        80;
server_name   blog.mydream.com;
location / {
root   html/blog;
index  index.html index.htm;
}
}


分别在/usr/local/nginx/html/www、/usr/local/nginx/html/bbs和/usr/local/nginx/html/blog目录下生产index.html文件

echo "www.mydream.com" > /usr/local/nginx/html/www/index.html
echo "bbs.mydream.com" > /usr/local/nginx/html/bbs/index.html
echo "blog.mydream.com" > /usr/local/nginx/html/blog/index.html
在需要访问该网站的机器上,修改/etc/hosts 文件,增加一条

xxx.xxx.xxx.xxx www.mydream.com bbs.mydream.com blog.mydream.com

其中xxx.xxx.xxx.xxx换成实际的ip

验证

curl www.mydream.com
curl bbs.mydream.com
curl blog.mydream.com


3.访问日志文件按日分割

nginx 重新加载的实现方式 nginx -s reload



脚本cut_nginx_log.sh

#!/bin/sh
Dateformat=`date +%Y%m%d`
Basedir="/usr/local/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Nginxlogdir}/${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Logname}_${Dateformat}.log
$Basedir/sbin/nginx -s reload
定义crontab,用crontab -e 增加一行,每天的0点分割访问日志

0 0 * * * /usr/local/nginx/sbin/cut_nginx_log.sh >/dev/null 2>&1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: