解读nginx之虚拟主机

 基于域名的虚拟主机

http {
  index index.html;  #默认文件顺序
 
  server {
    server_name www.domain1.com;  #服务器名
    access_log logs/domain1.access.log main;  #日志记录
 
    root /var/www/domain1.com/htdocs;  #网站目录
  }
 
  server {
    server_name www.domain2.com;
    access_log  logs/domain2.access.log main;
 
    root /var/www/domain2.com/htdocs;
  }
}

 

--------

基于ip的虚拟主机

http {
 
  server{

listen 192.168.1.201:80;  #监听的IP地址和端口

server_name 192.168.1.201;  #主机名

access_log log/server201.access.log combined;  #访问日志的位置
location /
                      {

index index.html index.htm  #默认文件

root html1  #默认目录位置
                      }
               }

#增加第二个虚拟主机
server{
#监听的IP地址和端口
listen 192.168.1.201:80;
#主机名
server_name 192.168.1.201;
#访问日志的位置
access_log log/server201.access.log combined;
location /
{
#默认文件
index index.html index.htm
#默认目录位置
root html1
}
}
#增加第三个虚拟主机
server{
#监听IP地址和端口
listen 192.168.1.202:80;
#主机名
server_name 192.168.1.201;
#访问日志的位置
access_log log/server202.access.log combined;
location /
{
#默认文件
index index.html index.htm
#默认目录位置
root html2
}
}
}

==============================

另附指定所有的二级域名

server {
  # Replace this port with the right one for your requirements
  listen 80 [default|default_server];  #could also be 1.2.3.4:80  监听端口
 
  # Multiple hostnames separated by spaces.  Replace these as well.
  server_name star.yourdomain.com *.yourdomain.com;

# Alternately: _
 
  root /PATH/TO/WEBROOT/$host;
 
  error_page 404 errors/404.html;   #错误页
  access_log logs/star.yourdomain.com.access.log;
 
  index index.php index.html index.htm;
 
  # serve static files directly
  location ~* /.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }
 
  location ~ /.php$ {
    include fastcgi_params;
    fastcgi_intercept_errors on;
    # By all means use a different server for the fcgi processes if you need to
    fastcgi_pass   127.0.0.1:YOURFCGIPORTHERE;
  }
 
  location ~ //.ht {
    deny  all;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值