Nginx 配置多站点vhost

mac2024-05-28  46

一、背景

    在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是否已打开(即删除注释)

最新回复(0)