egrep -v “#|^$” nginx.conf.default > nginx.conf #过滤#及空行
- 搭建基于域名的多虚拟机主机
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
#nginx vhosts config
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
创建配置文件目录及配置文件
cd /application/nginx/conf/
mkdir extra
touch extra/{www,bbs,blog}.conf
[root@nginx conf]# vi extra/www.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root /var/html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
access_log logs/www_access.log main;
}
创建站点目录
mkdir /var/html/{www,bbs,blog} -p
touch /var/html/{www,bbs,blog}/index.html
for name in www bbs blog; do echo “$name ” >/var/html/$name/index.html;done
重启web服务
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload
echo ” 192.168.80.104 www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org”>>/etc/hosts
如果配置基于域名的多虚拟机主机,使用IP地址访问默认访问第一个server
- 基于端口的多虚拟主机
在基于域名的多虚拟机主机的基础上,修改两个server使用同一个域名,不同的端口,重启,使用不同的端口访问。
- 基于IP的虚拟主机
man ip #查找帮助
********************************************
……
ip addr { add | del } IFADDR dev STRING
……
ip addr add 192.168.80.99/24 dev eth0 #在一个网卡上增加另一个IP地址
仅仅修改一个server中的 listen 192.168.80.99:80;
另一个server中为listen 192.168.80.100:80;即可
- 利用include功能优化Nginx的配置文件
将配置文件中的server模块置于另外一个文件extra/vhost.conf中,然后在nginx.conf文件中加入include extra/vhost.conf;重启即可。也可以添加多个.conf文件,使用include分别包括,也可以直接加入include extra/*.conf;以包含所有.conf文件。
例如: include extra/www.conf;
- 虚拟主机别名配置
在server_name后直接加别名,可以有多个,使用空格隔开,并且在hosts文件中对别名解析。可以利用此功能监控集群节点下的主机
- Nginx状态信息功能实战
- Nginx status介绍
Nginx软件功能模块中有一个ngx_http_stub_status_module模块,这个模块的功能是记录Nginx的基本访问状态信息,让读者了解Nginx的工作状态,例如:网站的负载情况。要想使用状态模块,在编译时必须增加http_stub_status_module模块。可通过如下方式查看安装时编译的参数:./nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
TLS SNI support enabled
configure arguments: –prefix=/application/nginx-1.6.3 –user=nginx –group=nginx –with-http_ssl_module –with-http_stub_status_module
- 配置Nginx状态信息功能
配置文件中添加如下server
server {
listen 80;
server_name status.etiantian.com;
location / {
stub_status on;
access_log off;
}
}
重启服务并在hosts文件中添加主机记录,在浏览器中访问:status.etiantian.com,显示如下:
Active connections: 1 #正在处理的活动链接数
server accepts handled requests
2 2 13
Reading: 0 Writing: 1 Waiting: 0
解析:Active connections //当前 Nginx 正处理的活动连接数。
server accepts handledrequests //总共处理了2个连接 , 成功创建 2 次握手,总共处理了13个请求。
Reading //nginx读取到客户端的 Header 信息数。
Writing //nginx返回给客户端的 Header 信息数。
Waiting //开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接