Windows下Nginx Virtual Host多站点配置详解

2023-05-25,,

Windows下Nginx Virtual Host多站点配置详解  

  此教程适用于Windows系统已经配置好Nginx+Php+Mysql环境的同学。

如果您还未搭建WNMP环境,请查看 windows7配置Nginx+php+mysql教程。

  先说明一下配置多站点的目的:在生产环境中,如果将系统所有代码文件都放在公开目录中,则很容易被查看到系统源码,这样是很不安全的,所以需要只公开index.php的入口文件目录。而同一个服务器中,可能运行多个系统,这样就必须公开多个入口文件目录,以便用不同的域名访问不同的系统。所以这就需要使用virtual host实现多站点。

  下面直接进入主题:

一.配置virtualhost多站点

 以www.lee.com和www.lee1.com为两个栗子。

 1. 定义站点域名。

   首先修改系统hosts文件(hosts文件位于C:\Windows\System32\drivers\etc文件夹内)。在修改hosts文件之前要先确定有修改此文件的权限,鼠标右键hosts文件,点击属性,如下图所示点击编辑修改用户的权限为可以写入。

        

   然后在hosts文件底部,仿照如下添加:(根据需求可随意添加)

      127.0.0.1           www.lee.com

      127.0.0.1           www.lee1.com

 2. 创建站点公开文件目录,并创建测试文件

   我设置的文件目录如图所示:

      

    nginx文件夹为nginx相关内容,php为php相关内容。

    其中lee和lee1位公开的两个文件目录,文件目录path和文件夹名可以根据站点域名做任意更改。

    在lee和lee1文件夹中添加两个php文件用于测试。

    在lee文件夹中添加index.php,并编辑内容为:

<?php
echo "www.lee.com<br/>";
echo phpinfo();
?>

    在lee1文件夹中添加index.php,并编辑内容为:

<?php
echo "www.lee1.com<br/>";
echo phpinfo();
?>

 3. 修改nginx.conf配置文件

   在该配置文件中如下代码位置进行修改:(nginx.conf配置位于nginx/conf/文件夹内)

 # 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.htm;
# }
#}

  将上述配置代码修改为:

    # another virtual host using mix of IP-, name-, and port-based configuration
#
#modify by lee 20160902 for virtual host www.lee.com -s
server {
listen 80;
access_log logs/lee.access.log;
error_log logs/lee.error.log;
server_name www.lee.com;
location / {
root C:/wnmp/lee;
index index.html index.htm index.php;
}
location ~ \.php$ {
root C:/wnmp/lee;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#modify by lee 20160902 for virtual host www.lee.com -e
#modify by lee 20160902 for virtual host www.lee1.com -s
server {
listen 80;
access_log logs/lee1.access.log;
error_log logs/lee1.error.log;
server_name www.lee1.com; location / {
root C:/wnmp/lee1;
index index.html index.htm index.php;
}
location ~ \.php$ {
root C:/wnmp/lee1;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#modify by lee 20160902 for virtual host www.lee1.com -e

    其中server_name为hosts文件中设置的站点域名,access_log和error_log为日志文件,文件名做响应更改。

      root为 步骤2设置的站点公开文件目录。

 4. 测试

    重启Nginx和php-cgi服务,启动方法详见我的上一篇文章------windows7配置Nginx+php+mysql教程  (步骤4(5))

    打开浏览器,访问  www.lee.com

     

    访问 www.lee1.com

      

     VirtualHost多站点配置成功!

   下一篇文章会是: Windows下Nginx配置Openssl实现Https访问(包含证书生成)

   参考:http://www.jb51.net/article/27533.htm

Windows下Nginx Virtual Host多站点配置详解的相关教程结束。

《Windows下Nginx Virtual Host多站点配置详解.doc》

下载本文的Word格式文档,以方便收藏与打印。