全球主机交流论坛

标题: 利用Nginx自动在父文件夹中建立子文件夹以指向子域名 [打印本页]

作者: atbaidu    时间: 2010-12-20 00:40
标题: 利用Nginx自动在父文件夹中建立子文件夹以指向子域名
以下内容转自某同学博客,备份一下:今天在逛nginx wiki时才发现原来nginx 自带了这个功能

这是一个添加子域名(或是当DNS已指向服务器时添加一个新域名)的简单方法。需要注意的是,我已经将FCGI配置进该文件了。如果你只想使服务器为静态文件服务,可以直接将FCGI配置信息注释掉,然后将默认主页文件变成index.html。

这个简单的方法比起为每一个域名建立一个 vhost.conf 配置文件来讲,只需要在现有的配置文件中增加如下内容:

# Replace this port with the right one for your requirements
# 根据你的需求改变此端口
listen       80;  #could also be 1.2.3.4:80 也可以是1.2.3.4:80的形式
# Multiple hostnames seperated by spaces.  Replace these as well.
# 多个主机名可以用空格隔开,当然这个信息也是需要按照你的需求而改变的。
server_name  star.yourdomain.com *.yourdomain.com www.*.yourdomain.com;
#Alternately: _ *
#或者可以使用:_ * (具体内容参见本维基其他页面)
root /PATH/TO/WEBROOT/$host;
error_page  404              http://yourdomain.com/errors/404.html;
access_log  logs/star.yourdomain.com.access.log;
location / {
root   /PATH/TO/WEBROOT/$host/;
index  index.php;
}


# serve static files directly
# 直接支持静态文件 (从配置上看来不是直接支持啊)
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log        off;
expires           30d;
}
location ~ .php$ {
# By all means use a different server for the fcgi processes if you need to
# 如果需要,你可以为不同的FCGI进程设置不同的服务信息
fastcgi_pass   127.0.0.1:YOURFCGIPORTHERE;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /PATH/TO/WEBROOT/$host/$fastcgi_script_name;
fastcgi_param  QUERY_STRING     $query_string;
fastcgi_param  REQUEST_METHOD   $request_method;
fastcgi_param  CONTENT_TYPE     $content_type;
fastcgi_param  CONTENT_LENGTH   $content_length;
fastcgi_intercept_errors on;
}
location ~ /\.ht {
deny  all;
}


用这段配置做了测试,其实这里的$host 会自动变为目录名,比如你使用news.91linux.cn 访问的时候,那么root的目录就是/PATH/TO/WEBROOT/news.91linux.cn

这个功能满好用的,以前要实现这个都是在程序做判断,明显比nginx自带占用资源多得多
作者: needvps    时间: 2010-12-20 01:16
占个位置
作者: Suley    时间: 2010-12-20 01:37
excellent thread
作者: zllovesuki    时间: 2010-12-20 01:44
楼主out了。不过我相信很多人是不知道的
作者: pfdiy    时间: 2010-12-20 09:07
占位,保留!
作者: adochina    时间: 2010-12-20 09:13
这个可以有。
作者: Globalization    时间: 2010-12-20 09:46
学习下
作者: yaoe    时间: 2010-12-20 09:47
楼主out了。不过我相信很多人是不知道的

作者: l4steleven    时间: 2010-12-20 13:08
收藏了
作者: wxforex    时间: 2010-12-20 14:41
真的很多人不知道 收藏!




欢迎光临 全球主机交流论坛 (https://hostloc.onozo.cc/) Powered by Discuz! X3.4