11lxm 发表于 2017-6-29 07:26:56

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

  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_namesomenamealiasanother.alias;                        
                        
#    location / {                        
#      root   html;                        
#      indexindex.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_loglogs/lee.access.log;                  
error_loglogs/lee.error.log;                        
server_name   www.lee.com;                  
      location / {                        
root   C:/wnmp/lee;                        
indexindex.html index.htm index.php;                        
      }                        
location ~ \.php$ {                  
root         C:/wnmp/lee;                        
fastcgi_pass   127.0.0.1:9001;                        
fastcgi_indexindex.php;                        
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;                        
include      fastcgi_params;                        
      }                        
}                        
#modify by lee 20160902 for virtual hostwww.lee.com -e                        
#modify by lee 20160902 for virtual hostwww.lee1.com -s                        
server {                        
listen       80;                        
access_loglogs/lee1.access.log;                  
error_loglogs/lee1.error.log;                        
server_name   www.lee1.com;                  
                        
location / {                        
root   C:/wnmp/lee1;                        
indexindex.html index.htm index.php;                        
      }                        
location ~ \.php$ {                  
root         C:/wnmp/lee1;                        
fastcgi_pass   127.0.0.1:9001;                        
fastcgi_indexindex.php;                        
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;                        
include      fastcgi_params;                        
      }                        
}                        
#modify by lee 20160902 for virtual hostwww.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.iyunv.net/article/27533.htm
  
页: [1]
查看完整版本: Windows下Nginx Virtual Host多站点配置详解