这一篇文章中,我们来学习如何在一个Nginx服务器中配置多个站点。

假要你所在的公司由于预算问题,现在只能提供一台服务器,但是有以下几个网站:www.yourmall.com, m.yourmall.com,www.yourcrm.com需要部署,并且你已经根据我们上一篇文章中安装配置了,实现以下需求。

现在为以上网站在服务器上规划目录,创建一个目录:/var/sites,为上面三个站点分别创建一个目录:

如下图所示 -

[root@localhost sites]# mkdir /var/sites/yourmall
[root@localhost sites]# mkdir /var/sites/m_yourmall
[root@localhost sites]# mkdir /var/sites/yourcrm
[root@localhost sites]# pwd
/var/sites
[root@localhost sites]# ll
total 0
drwxr-xr-x. 2 root root 6 Apr 28 04:23 m_yourmall
drwxr-xr-x. 2 root root 6 Apr 28 04:23 yourcrm
drwxr-xr-x. 2 root root 6 Apr 28 04:23 yourmall
[root@localhost sites]#

1. 第一个域名/网站配置

打开Nginx的配置文件:/usr/local/nginx/conf/nginx.conf ,并在 http 块下加入以下子块server,如下配置内容所示 -

 # vhost for yourmall.com configure.
    server {
        listen       80;
        server_name  www.yourmall.com yourmall.com;

        #charset koi8-r;

        #access_log logs/yourmall.access.log main;

        location / {
            root   /var/sites/yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/sites/yourmall;
        }

        location ~ \.php$ {
            root           /var/sites/yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

现在,完整的Nginx配置文件:/usr/local/nginx/conf/nginx.conf的内容如下所示 -

#user nobody;
worker_processes  1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    # '$status $body_bytes_sent "$http_referer" '
    # '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log logs/access.log main;

    sendfile        on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout  65;

    #gzip on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log logs/host.access.log main;

        location / {
            root   html;
            index  index.html index.html;
        }

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        # proxy_pass http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        # deny all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    # listen 8000;
    # listen somename:8080;
    # server_name somename alias another.alias;

    # location / {
    # root html;
    # index index.html index.html;
    # }
    #}


    # HTTPS server
    #
    #server {
    # listen 443 ssl;
    # server_name localhost;

    # ssl_certificate cert.pem;
    # ssl_certificate_key cert.key;

    # ssl_session_cache shared:SSL:1m;
    # ssl_session_timeout 5m;

    # ssl_ciphers HIGH:!aNULL:!MD5;
    # ssl_prefer_server_ciphers on;

    # location / {
    # root html;
    # index index.html index.html;
    # }
    #}

    # vhost for yourmall.com configure.
    server {
        listen       80;
        server_name  www.yourmall.com yourmall.com;

        #charset koi8-r;

        #access_log logs/yourmall.access.log main;

        location / {
            root   /var/sites/yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/sites/yourmall;
        }

        location ~ \.php$ {
            root           /var/sites/yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}

在目录 /var/sites/yourmall 下创建一个文件:index.html用于测试网站的配置情况,其代码如下 -

<!DOCTYPE html>
<html>
<head>
<title>Welcome To Yourmall.com</title>
<style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style>
</head>
<body>
<h1>欢迎您访问:Yourmall.com </h1>
<p>这是 Yourmall.com 的首页,仅用于Nginx的虚拟机配置测试演示。</p>

<p>
<a href="http://www.yiibai.com/">yourmall.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

现在,重新加载Nginx配置文件,在加载配置文件之前,最好先测试一下配置文件语法是否正确,使用以下命令来测试 -

[root@localhost sites]# /usr/local/nginx/sbin/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@localhost sites]#

然后执行重新加载Nginx配置文件 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost sites]#

到了这一步,我们来看看网站 www.yourmall.com 是否配置成功,接下来我们要访问这个域名,看能不能看到我们在上面写入的 index.html 文件中的内容。在测试访问 www.yourmall.com 域名之前,我们还要对这个域名进行映射(外网称为解析)。在局域网内的一台Windows计算机上打开文件 <mark>C:\Windows\System32\drivers\hosts</mark>,在这个文件的最后一行加入以下内容 -

192.168.0.134    www.yourmall.com
192.168.0.134    yourmall.com

注意:在这里,192.168.0.134 是配置Nginx服务器的IP,如果你的服务器不是这个IP,请写上对应的服务器IP。

现在,打开浏览器,访问以下网站域名: www.yourmall.comyourmall.com ,没有错误,应该会看到以下界面 -

就这样,第一个网站的配置完成了!

2. 第二个域名/网站配置

现在,我们来配置第二个网站:m.yourmall.com, 假设 m.yourmall.com 这个网站它是 www.yourmall.com 的二级域名网站,主要用于服务主站 www.yourmall.com 的移动端访问用户。

打开Nginx的配置文件:/usr/local/nginx/conf/nginx.conf ,并在 http 块下加入以下子块server,如下配置内容所示 -

# 手机/移动端网站 vhost for m.yourmall.com configure.
    server {
        listen       80;
        server_name  m.yourmall.com;

        #charset koi8-r;

        #access_log logs/m_yourmall.access.log main;

        location / {
            root   /var/sites/m_yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/sites/m_yourmall;
        }

        location ~ \.php$ {
            root           /var/sites/m_yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

现在,完整的Nginx配置文件:/usr/local/nginx/conf/nginx.conf的内容如下所示 -

#user nobody;
worker_processes  1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    # '$status $body_bytes_sent "$http_referer" '
    # '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log logs/access.log main;

    sendfile        on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout  65;

    #gzip on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log logs/host.access.log main;

        location / {
            root   html;
            index  index.html index.html;
        }

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }



    # vhost for yourmall.com configure.
    server {
        listen       80;
        server_name  www.yourmall.com yourmall.com;

        #charset koi8-r;

        #access_log logs/yourmall.access.log main;

        location / {
            root   /var/sites/yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/sites/yourmall;
        }

        location ~ \.php$ {
            root           /var/sites/yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }


    # 手机/移动端网站 vhost for m.yourmall.com configure.
    server {
        listen       80;
        server_name  m.yourmall.com;

        #charset koi8-r;

        #access_log logs/m_yourmall.access.log main;

        location / {
            root   /var/sites/m_yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/sites/m_yourmall;
        }

        location ~ \.php$ {
            root           /var/sites/m_yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}

在目录 /var/sites/m_yourmall 下创建一个文件:index.html用于测试网站的配置情况,其代码如下 -

<!DOCTYPE html>
<html>
<head>
<title>Welcome To Yourmall.com</title>
<style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style>
</head>
<body>
<h1>欢迎您访问:m.yourmall.com </h1>
<p>这是 Yourmall.com 主站的子域名:<a href="http://www.yiibai.com/">m.yourmall.com</a> ,仅用于Nginx的虚拟机配置测试演示。</p>

<p>
<a href="http://www.yiibai.com/">m.yourmall.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

现在,重新加载Nginx配置文件,在加载配置文件之前,最好先测试一下配置文件语法是否正确,使用以下命令来测试 -

[root@localhost sites]# /usr/local/nginx/sbin/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@localhost sites]#

然后执行重新加载Nginx配置文件 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost sites]#

到了这一步,我们来看看网站 m.yourmall.com 是否配置成功,接下来我们要访问这个域名,看能不能看到我们在上面写入的 index.html 文件中的内容。在测试访问m.yourmall.com 域名之前,我们还要对这个域名进行映射(外网称为解析)。在局域网内的一台Windows计算机上打开文件 C:\Windows\System32\drivers\hosts,在这个文件的最后一行加入以下内容 -

192.168.0.134    m.yourmall.com

注意:在这里,192.168.0.134 是配置Nginx服务器的IP,如果你的服务器不是这个IP,请写上对应的服务器IP。

现在,打开浏览器,访问以下网站域名: m.yourmall.com 没有错误,应该会看到以下界面 -

就这样,第二个网站(子域名)的配置完成了!

3. 第三个域名/网站配置

接下来,我们来配置第三个网站:www.yourcrm.com, 假设 www.yourcrm.com 这个网站它是一个客户管理系统,主要记录客户信息的管理网站。

注意:配置这个网站与第一个网站类似,只是指定/使用文件目录不太一样。

打开Nginx的配置文件:/usr/local/nginx/conf/nginx.conf ,并在 http 块下加入以下子块server,如下配置内容所示 -

 # 客户管理系统 vhost for yourcrm.com configure.
    server {
        listen       80;
        server_name  www.yourcrm.com yourcrm.com;

        #charset koi8-r;

        #access_log logs/yourcrm.access.log main;

        location / {
            root   /var/sites/yourcrm;
            index  index.html index.html;
        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/sites/yourcrm;
        }

        location ~ \.php$ {
            root           /var/sites/yourcrm;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

现在,完整的Nginx配置文件:/usr/local/nginx/conf/nginx.conf的内容如下所示 -

#user nobody;
worker_processes  1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    # '$status $body_bytes_sent "$http_referer" '
    # '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log logs/access.log main;

    sendfile        on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout  65;

    #gzip on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log logs/host.access.log main;

        location / {
            root   html;
            index  index.html index.html;
        }

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }



    # vhost for yourmall.com configure.
    server {
        listen       80;
        server_name  www.yourmall.com yourmall.com;

        #charset koi8-r;

        #access_log logs/yourmall.access.log main;

        location / {
            root   /var/sites/yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/sites/yourmall;
        }

        location ~ \.php$ {
            root           /var/sites/yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }


    # 手机/移动端网站 vhost for m.yourmall.com configure.
    server {
        listen       80;
        server_name  m.yourmall.com;

        #charset koi8-r;

        #access_log logs/m_yourmall.access.log main;

        location / {
            root   /var/sites/m_yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/sites/m_yourmall;
        }

        location ~ \.php$ {
            root           /var/sites/m_yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }


    # 客户管理系统 vhost for yourcrm.com configure.
    server {
        listen       80;
        server_name  www.yourcrm.com yourcrm.com;

        #charset koi8-r;

        #access_log logs/yourcrm.access.log main;

        location / {
            root   /var/sites/yourcrm;
            index  index.html index.html;
        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/sites/yourcrm;
        }

        location ~ \.php$ {
            root           /var/sites/yourcrm;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}

在目录 /var/sites/yourcrm 下创建一个文件:index.html用于测试网站的配置情况,其代码如下 -

<!DOCTYPE html>
<html>
<head>
<title>Welcome To YourCrm.com</title>
<style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style>
</head>
<body>
<h1>欢迎您访问:yourcrm.com </h1>
<p>这是 yourcrm.com 网站的首页 ,仅用于Nginx的虚拟机配置测试演示。</p>

<p>
<a href="http://www.yiibai.com/">yourcrm.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

现在,重新加载Nginx配置文件,在加载配置文件之前,最好先测试一下配置文件语法是否正确,使用以下命令来测试 -

[root@localhost sites]# /usr/local/nginx/sbin/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@localhost sites]#

然后执行重新加载Nginx配置文件 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost sites]#

到了这一步,我们来看看网站 www.yourcrm.com 是否配置成功,接下来我们要访问这个域名,看能不能看到我们在上面写入的 index.html 文件中的内容。在测试访问 www.yourcrm.com 域名之前,我们还要对这个域名进行映射(外网称为解析)。在局域网内的一台Windows计算机上打开文件 C:\Windows\System32\drivers\hosts,在这个文件的最后一行加入以下内容 -

192.168.0.134    www.yourcrm.com
192.168.0.134    yourcrm.com

注意:在这里,192.168.0.134 是配置Nginx服务器的IP,如果你的服务器不是这个IP,请写上对应的服务器IP。

现在,打开浏览器,访问以下网站域名: www.yourcrm.comyourcrm.com ,没有错误,应该会看到以下界面 -


就这样,第三个网站的配置完成了!

4. 优化配置

经过了上面三个网站的配置,相信你可能觉得 Nginx 的配置文件:/usr/local/nginx/conf/nginx.conf 内容有点多了,这还只是最简单的配置(还未包函重写,日志记录等规则),从头看到尾多少有点不太方便,能不能有更好的办法解决这个问题? 当然可以。我们可以把每个网站的配置独立成一个配置文件,然后在主配置文件中使用 include 指令包函进来。现在来看看怎么修改配置,首先分别创建三个网站的配置文件:

再将上面配置中对应每个网站的文件包括放入主配置文件。例如,对于文件 /usr/local/nginx/conf/vhosts/yourmall.conf 放置的文件内容如下:

# vhost for yourmall.com configure.
server {
    listen       80;
    server_name  www.yourmall.com yourmall.com;

    #charset koi8-r;

    #access_log logs/yourmall.access.log main;

    location / {
        root   /var/sites/yourmall;
        index  index.html index.html;
    }

    error_page  404              /404.html;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/sites/yourmall;
    }

    location ~ \.php$ {
        root           /var/sites/yourmall;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
        include        fastcgi_params;
    }

}

其它两个网站也同样放入对应配置。在主配置文件:/usr/local/nginx/conf/nginx.conf 中,包函上述三个文件即可。现在文件:/usr/local/nginx/conf/nginx.conf的内容如下所示 -

#user nobody;
worker_processes  1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    # '$status $body_bytes_sent "$http_referer" '
    # '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log logs/access.log main;

    sendfile        on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout  65;

    #gzip on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log logs/host.access.log main;

        location / {
            root   html;
            index  index.html index.html;
        }

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

    # 网站配置
    include /usr/local/nginx/conf/vhosts/yourmall.conf;

    include /usr/local/nginx/conf/vhosts/m_yourmall.conf;

    include /usr/local/nginx/conf/vhosts/yourcrm.conf;

}

重新加载Nginx配置文件,在加载配置文件之前,最好先测试一下配置文件语法是否正确,使用以下命令来测试 -

[root@localhost sites]# /usr/local/nginx/sbin/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@localhost sites]#

Shell然后执行重新加载Nginx配置文件 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost sites]#

到此,在Nginx上配置虚拟机演示实例就完了。