wnmp中Nginx配置多个子域名

阅读:373 2019-03-19 15:01:32 来源:新网

两种方法可以在nginx环境下配置子域名,形成多站点,第一种方法前面已经介绍过了,就是为每个子域名建立一个.conf配置文件;当子域名很多时,这种方法的就显现弊端了。这里介绍第二种方法。

1、假设已经向服务器添加了一个新域名test.com,下面介绍添加子域名abc.test.com和sub.test.com。在test.com的配置文件中添加如下内容:

server{listen80;server_name*.test.comabc.test.comsub.test.com;#多个子域名用空格隔开rootd:/dev/$host;#定义根目录,这里的$host对应域名access_loglogs/test.access.log;error_loglogs/test.error.log;location/{rootd:/dev/$host/;indexindex.phpindex.htmlindex.htm;}#serverstaticfilesdirectly#直接支持静态文件,图片、样式文件等location~*^.+.(jpg|jpeg|gif|css|png|js|ico|html)${access_logoff;expires30d;}#可根据需要为不同的cgi进程设置不同的服务信息location~.php${fastcgi_pass127.0.0.1:9000;fastcgi_indexindex.php;fastcgi_paramscript_filename$document_root$fastcgi_script_name;fastcgi_paramquery_string$query_string;fastcgi_paramrequest_method$request_method;fastcgi_paramcontent_type$content_type;fastcgi_paramcontent_length$content_length;fastcgi_intercept_errorson;includefastcgi_params;}location~/.ht{denyall;}}

2、修改hosts文件,使子域名abc.test.com和sub.test.com都指向本机。

3、这里的根目录为d:/dev/$host,其中$host是当前域名,那么当访问abc.test.com时,实际访问的文件夹是d:/dev/abc.test.com;访问sub.test.com时,实际访问的是d:/dev/sub.test.com。故一定要存在相应的目录文件,否则回报404错误。

4、重启nginx,分别在对应的文件夹中放入测试文件index.php,访问相应子域名,测试是否成功。

相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服