Nginx配置虚拟主机

参考资料

[1]. 跟老男孩学Linux运维:Web集群实战,老男孩html

安装过程

配置基于域名的nginx.conf内容

[root@www ~]# mkdir /application/nginx/conf/extra
[root@www ~]# cd /application/nginx/conf/extra

编辑配置文件

编辑 www.etiantian.org etiantian.org; 网站

[root@www extra]# vim /application/nginx/conf/extra/www.conf
server{
        listen  80;
        server_name     www.etiantian.org etiantian.org;  #配置别名
        location / {
                root    html/www;
                index   index.html index.htm;
        }
        ## gzip buffer=32k flush=5s; 是高并发场景下
        # access_log logs/access_www.log main;
        access_log logs/access_www.log main gzip buffer=32k flush=5s;
}

编辑 bbs 网站

[root@www extra]# vim /application/nginx/conf/extra/bbs.conf

server{
        listen  80;
        server_name     bbs.etiantian.org;
        location / {
                root    html/bbs;
                index   index.html index.htm;
        }
        # access_log logs/access_bbs.log main;
        access_log logs/access_bbs.log main gzip buffer=32k flush=5s;
}

编辑 blog 网站

[root@www extra]# vim /application/nginx/conf/extra/blog.conf
server{
        listen  80;
        server_name     blog.etiantian.org;
        location / {
                root    html/blog;
                index   index.html index.htm;
        }
        # access_log logs/access_blog.log main;
        access_log logs/access_blog.log main gzip buffer=32k flush=5s;
}

编辑 Nginx 状态信息功能

[root@www ~]# vim /application/nginx/conf/extra/status.conf
server{
        listen  80;
        server_name     status.etiantian.org;
        location / {
                stub_status on;
                access_log off;
                allow 192.0.0.0/24; # 容许ip段 只能选一个
                deny all;           # 禁止ip段 只能选一个
        }
}
[root@www extra]# cd ..

编辑主配置文件

[root@www conf]# vim /application/nginx/conf/nginx.conf

worker_processes  1;
error_log       logs/error.log;                                 
events {
    worker_connections  1024;
}
http {
    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"';
    sendfile        on;
    keepalive_timeout  65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/status.conf;
}

建立域名对应的站点目录及文件

[root@www conf]# mkdir /application/nginx/html/www /application/nginx/html/bbs /application/nginx/html/blog -p 
[root@www conf]# echo "http://www.etiantian.org" >/application/nginx/html/www/index.html
[root@www conf]# echo "http://bbs.etiantian.org" >/application/nginx/html/bbs/index.html
[root@www conf]# echo "http://blog.etiantian.org" >/application/nginx/html/blog/index.html

检查文件和目录

[root@www conf]# LANG=en
[root@www conf]# tree /application/nginx/html/

检查并从新加载

[root@www conf]# /application/nginx/sbin/nginx -t
[root@www conf]# /application/nginx/sbin/nginx -s reload

在Linux客户端测试

[root@www conf]# echo "192.168.1.70 www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org status.etiantian.org" >> /etc/hosts
[root@www conf]# curl www.etiantian.org
[root@www conf]# curl bbs.etiantian.org
[root@www conf]# curl blog.etiantian.org

在windows 7系统下测试

编辑 C:\Windows\System32\drivers\etc\hosts
追加内容:192.168.1.70 www.etiantian.org bbs.etiantian.org blog.etiantian.org
在dos提示符下检测
ping www.etiantian.org
ping bbs.etiantian.org
ping blog.etiantian.org
在浏览器下直接输入
www.etiantian.org
bbs.etiantian.org
blog.etiantian.orglinux

追加内容:192.168.1.70 www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org status.etiantian.org
此时经过浏览器访问,便可查看状态参数。nginx

nginx 状态信息参数

8.1 server 表示Nginx从启动到如今共处理了xx个链接
8.2 accelits 表示Nginx从启动到如今共建立了xx次握手
8.3 handled requests 表示Nginx从启动到如今共处理了xx次请求
8.4 Reading 为Nginx读取到客户端的Headr信息数
8.5 Writing 为Nginx返回到客户端的Headr信息数
8.6 Waiting 为Nginx已经处理完正在等候下一次请求指令的驻留链接,在开启keep-alive的状况下,这个值等于active-(reading+writing)web

Nginx 访问日志轮询切割

[root@www ~]# vim /server/scripts/cut_nginx_log.sh
#!bin/sh
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ]&& cd $Nginxlogdir||exit 1
[ -f ${Logname}.log ]||exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload

[root@www ~]# crontab -e

## 添加下面的内容
#cut nginx access log by oldboy
00 00 * * * /bin/sh /server/script/cut_nginx_log.sh >/dev/null 2>&1