Windows下Nginx Virtual Host多站点配置详解 php
此教程适用于Windows系统已经配置好Nginx+Php+Mysql环境的同窗。html
若是您还未搭建WNMP环境,请查看 windows7配置Nginx+php+mysql教程。mysql
先说明一下配置多站点的目的:在生产环境中,若是将系统全部代码文件都放在公开目录中,则很容易被查看到系统源码,这样是很不安全的,因此须要只公开index.php的入口文件目录。而同一个服务器中,可能运行多个系统,这样就必须公开多个入口文件目录,以便用不一样的域名访问不一样的系统。因此这就须要使用virtual host实现多站点。nginx
下面直接进入主题:sql
一.配置virtualhost多站点windows
以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