Nginx 虚拟主机配置及日志详解

虚拟主机管理

1.什么是nginx 虚拟主机

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,
每个虚拟主机可以独立对外提供 www 服务,这样就可以实现一台主机对外提供多个 web 服务,
每个虚拟主机之间是独立的,互不影响。

2.Nginx 支持三种类型的虚拟主机配置

1.基于 IP 的虚拟主机  
2.基于域名的虚拟主机
3.基于端口的虚拟主机

配置文件server 标签写法

# server标签(虚拟主机)
server {
# 监听80端口
listen 80;
#域名
server_name localhost;
# 字符集
#charset koi8-r;
#日志
#access_log /var/log/nginx/host.access.log main;
#访问的是/
location / {
# 在/usr/share/nginx/html
root /usr/share/nginx/html;
#站点目录 = /usr/share/nginx/html;
# 默认找index.html页面或者index.htm 从左往右查找
index index.html index.htm; # 默认页面
= index.html || index.htm } # 错误页面 404 找站点目录下的404.html error_page 404 /404.html; # redirect server error pages to the static page /50x.html # # 错误页面 5xx的,访问站点目录下的50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }
server {
#监听的端口
listen 80;
#域名,IP
server_name www.test.com;
# 所有的请求以/开始 匹配location
location / {
#站点目录 网页存放处
root /opt/test;
#欢迎页面 html页面 从左向右查询
index index.html index.htm;
}
}

基于多IP配置方式

1.单网卡多ip,和多网卡多ip   ##添加VIP

 绑定虚拟IP给eth0
[root@web01 conf.d]# ifconfig eth0:0 10.0.0.100/24
[root@web01 conf.d]# ifconfig eth0:1 10.0.0.101/24

2.配置nginx

[root@web01 conf.d]# cat /etc/nginx/conf.d/www.zls.com_test.conf
server {
listen 80;
server_name 10.0.0.100;
location / {
root /yw1/html;
index index.html;
}
}
server {
listen 80;
server_name 10.0.0.101;
location / {
root /yw2/html;
index index.html;
}
}

基于多端口配置方式

[root@web01 conf.d]# cat /etc/nginx/conf.d/www.zls.com_test.conf
server {
listen 80;
server_name 10.0.0.100;
location / {
root /yw1/html;
index index.html;
}
}
server {
listen 81;
server_name 10.0.0.100;
location / {
root /yw2/html;
index index.html;
}
}

基于多域名配置方式

[root@web01 conf.d]# cat /etc/nginx/conf.d/www.zls.com_test.conf
server {
listen 80;
server_name www.yewu1.com;
location / {
root /yw1/html;
index index.html;
}
}
server {
listen 80;
server_name www.yewu2.com;
location / {
root /yw2/html;
index index.html;
}
}

Nginx 日志管理

Nginx日志描述

通过访问日志,你可以得到用户地域来源、跳转来源、使用终端、某个URL访问量等相关信息;
通过错误日志,你可以得到系统某个服务或server的性能瓶颈等。因此,将日志好好利用,你可以得到很多有价值的信息。

Nginx日志格式

1.打开nginx.conf配置文件

vim /etc/nginx/nginx.conf 

2.参数明细

   
   
   
   
   
   
   
   
   
   

猜你喜欢

转载自www.cnblogs.com/goodsuperman/p/12901224.html
今日推荐