nginx多域名设置
时间:2010-11-21 来源:hanh422
一个服务器上需要跑多个网站,如果仅仅把域名解析到server的IP是不行的,访问不同域名打开的都是nginx默认的网站。要想分别对应,需要在nginx里设置vhost。
我是用lnmp一键安装包(http://www.lnmp.org/ )安装的nginx+mysql+php环境,对于其他自己编译的nginx估计配置文件和安装目录会有所不同,自己酌情修改哦,呵呵
编辑/usr/local/nginx/conf/nginx.conf,去掉server的参数。
server
{
listen 80;
server_name www.wifizoo.net;
index index.html index.htm index.php;
root /tmp/wwwroot;
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /home/wwwroot/logs/access.log access;
}
然后加入vhost定义:
include /usr/local/nginx/vhost/*.conf;
}
再在/usr/local/nginx/建立vhost文件夹,里面创建各域名的对应配置文件。
这个简单,就把之前的server配置内容复制到创建的对应conf文件里就OK了。
server
{
listen 80;
server_name www.meituge.com;
server_name meituge.com;
index index.html index.htm index.php;
root /tmp/wwwroot/meituge;
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
#log_format access '$remote_addr - $remote_user [$time_local] "$request" '
#'$status $body_bytes_sent "$http_referer" '
#'"$http_user_agent" $http_x_forwarded_for';
#access_log /home/wwwroot/logs/access.log access;
}
这里要注意,如果你用的是一级域名,那么需要在server配置里指定不加www前缀的域名,否则访问meituge.com会被定义到默认站点而非www.meituge.com
server_name www.meituge.com;
server_name meituge.com;