简单的 nginx 多站点配置

时间:2023-03-09 17:01:22
简单的 nginx 多站点配置

测试环境:基于CentOS6.8 编译安装LNMP(http://www.cnblogs.com/afee666/p/6836161.html)

一 需求

在一个 VPS 主机上配置 web 服务器,实现通过一个 IP 访问多个站点或域名。

假设:

IP地址: 66.88.33.11

域名1 test1.example.com 放在 /web/www/test1

域名2 test2.example.com 放在 /web/www/test2

二 思路

2.1 把2个站点 test1.example.com, test2.example.com 放到 nginx 可以访问的目录 /web/www/

2.2 给每个站点分别创建一个 nginx 配置文件 test1.conf,test2.conf,并把配置文件放到 /etc/nginx/vhosts/ 下

2.3 在 /etc/niginx/nginx.conf 里面加一句 include 把步骤2创建的配置文件全部包含进来(用 * 号),重启 nginx

三 实现

3.1 在 /etc/nginx 下创建 vhosts 目录

mkdir /etc/nginx/vhosts

3.2 在 /etc/nginx/vhosts/ 里创建一个名字为 test1.conf 的文件,把以下内容拷进去

server {
listen ;
server_name test1.example.com;
index index.html index.htm index.php;
root /web/www/test1;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
include /web/server/nginx/conf/rewrite/test1.conf;
access_log /web/log/nginx/access/conf.log;
}

3.3 在 /etc/nginx/vhosts/ 里创建一个名字为 test2.conf 的文件,把以下内容拷进去

server {
listen ;
server_name test2.example.com;
index index.html index.htm index.php;
root /web/www/test2;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
include /web/server/nginx/conf/rewrite/test2.conf;
access_log /web/log/nginx/access/test2.log;
}

3.4 打开 /etc/nginx/nginix.conf 文件,在 http 模块中加入 include 把以上2个文件包含进来

 include /etc/nginx/vhosts/*;