在使用Nginx做为">Web服务器使用的过程中,对于虚拟主机的设置是必不可少的一项关键技术,下面关于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; } }
一个默认的虚拟主机
http { index index.html; server { listen 80 default; server_name _; access_log logs/default.access.log main; server_name_in_redirect off; root /var/www/default/htdocs; } }
在父文件夹中通配符子域名
这仅仅是一个非常简单的方式不断添加新的子域名,或添加新的域,DNS记录会自动在服务器中指明。请注意,我这里也包括FCGI。如果您只想使用静态文件,请脱离FCGI配置和更改默认的文档index.html。而不是为每个域名创建一个新vhost.conf文件,只需创建下列操作之一:
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; } }