基于域名的虚拟主机
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; } }