Nginx 配置多站点vhost

一、背景

    在Linux Nginx中用不同的域名访问不同的目录,这时就要配置多个vhost。

二、配置

假设网站根目录设定在/www/webroot

1、在/www/webroot下新建两个目录。

/www/webroot/test1.com
/www/webroot/test2.com

2、编辑nginx.conf。

# vim /usr/local/nginx/conf/nginx.conf
 
http {
    #...
    #gzip  on;

    include /usr/local/nginx/conf/vhost/*.conf;   #主要是加入此行,如有则忽略
}

3、在/usr/local/nginx/conf/vhost/目录下新建两个conf文件。

/usr/local/nginx/conf/vhost/test1.com.conf
/usr/local/nginx/conf/vhost/test2.com.conf

4、复制如下配置信息到两个文件中,只要修改一下两个关键部分即可。

server{
    listen    80;
    #listen    443 ssl;    # SSL
    server_name  test1.com;    # 关键点1

    #ssl_certificate       /data/ssl/cert_chain.crt;   # SSL
    #ssl_certificate_key   /data/ssl/test1.com.key;  # SSL    

    index index.php;
    root  /www/webroot/test1.com;    # 关键点2
    access_log  /www/weblogs/test1.com/access.log  main;
    error_log   /www/weblogs/test1.com/error.log;

    location / {
        index index.php index.html;
        if ( !-e $request_filename){
            rewrite ^/(.*)$ /index.php?s=$1 last;
            break;
        }
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires      30d;
    }

    location ~ .*\.(js|css)?$ {
        expires      30d;
    }

    location ~ .*\.(php|php5)?$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}

三、测试+重启

[root@izuf68cj***3v9lvz vhost]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@izuf68cj***3v9lvz vhost]# nginx -s reload

执行 nginx -t 时,若提示:nginx: [emerg] unknown log format "main" in /usr/local/nginx/conf*****,则查看/usr/local/nginx/conf/nginx.conf文件中的log_format  main是否已打开(即删除注释)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值