Apache/Ngnix虚拟主机的配置

一.配置http-vhosts.conf

首先在apache配置文件中httpd.conf将下面代码前的#去掉php

#Include conf/extra/httpd-vhosts.conf

而后打开conf/extra/httpd-vhosts.conf加上下面的配置信息html

#默认访问网络路径映射的物理路径
<VirtualHost _default_:80>
	DocumentRoot "D:\WWW"  //设置默认网站根目录
</VirtualHost>
#文档可访问性设置,这里对全部的访问公开,并支持cgi
#Options -Indexes +FollowSymLinks +ExecCGI 禁止自动索引目录中的文件,注意,+和-以前必须有空格
<Directory "D:\WWW">
    Options -Indexes +FollowSymLinks +ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

#配置本身的虚拟站点,如如下将物理路径映射出响应的网络路径,注意文件夹名能够和域名不一样
<VirtualHost *:80>
    DocumentRoot "D:\WWW\anciyoujiao"
    ServerName www.anciyoujiao.com
    ServerAlias anciyoujiao.com
</VirtualHost>
#这里也是设置权限,只是目录位置不一样而已,其实这一步已无必要,可是为了规范仍是加上吧
<Directory "D:\WWW\anciyoujiao">
    Options FollowSymLinks ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

固然,以上方式是基于主机名称,若是想基于端口,请使用以下方式

Listen 9090
<VirtualHost *:9090>
  DocumentRoot "D:\PHPStudy\WWW"
  <Directory "D:\PHPStudy\WWW">
    Options -Indexes +FollowSymLinks +ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>
Listen 9091
<VirtualHost *:9091>
  DocumentRoot "D:\PHPStudy\myWeb"
  ServerName www.myhost.com
  <Directory "D:\PHPStudy\myWeb">
    Options -Indexes +FollowSymLinks +ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>

二.关于Apache指令

关于指令的具体用法,请参考《Upgrading to 2.4 from 2.2nginx

Order表示检查规则的顺序:allow和deny那个规则先起做用apache

①顺序决定过滤结果  allow,deny表示先过滤allow的,最后deny规则来过滤allow过滤的后的,反之同样。windows

② deny或者规则不存在,那么被认为起相反的做用,好比,deny规则不存在时,不回去过滤,等同于allow安全

同一类【Allow类Deny类】中规则指令具备优先级,规则越靠后,优先级越高服务器

 Order [(allow,deny)或(deny,allow)]  #顺序指令
 [Allow 或 Deny规则1]   #规则指令
 [Allow 或 Deny规则2]   #规则指令
 [Allow 或 Deny规则3]   #规则指令

 

下面的例子是拒绝全部网络

apache 2.2 app

Order deny,allow
Deny from all

Deny先检查,Allow规则后检查ide

Deny规则拒绝全部ip,但allow 规则不存在,不进行过滤,等于认同Deny规则

apache2.4

Require all denied

下面的例子是统一全部访问

apache 2.2

Order allow,deny
Allow from all

apache2.4

Require all granted

AllowOverride指令

AllowOverride参数就是指明Apache服务器是否去找.htacess文件做为配置文件,若是设置为none,那么服务器将忽略.htacess文件,若是设置为All,那么全部在.htaccess文件里有的指令都将被重写。

Options指令

1  All         准许如下除MultiViews之外全部功能

2  MultiViews  容许多重内容被浏览,若是你的目录下有一个叫作foo.txt的文件,那么你能够经过/foo来访问到它,这对于一个多语言内容的站点比较有用

3  Indexes     若该目录下无index文件,则准许显示该目录下的文件以供选择

4  IncludesNOEXEC  准许SSI,但不可以使用#exec和#include功能

5  Includes    准许SSI

6  FollowSymLinks  在该目录中,服务器将跟踪符号连接。注意,即便服务器跟踪符号连接,它也不会改变用来匹配不一样区域的路径名,若是在<Local>;标记内设置,该选项会被忽略

7  SymLinksIfOwnerMatch  在该目录中仅仅跟踪本站点内的连接

8  ExecCGI     在该目录下准许使用CGI

三.修改Hosts地址映射

还剩最后一步,那就是该本身本地的域名解析地址(dns)

一般位置在"C:/windows/System32/drivers/etc/hosts"

添加映射本身的地址,仿照

127.0.0.1   localhost
192.168.28.3  www.anciyoujiao.com

好了,重启服务器,而后在url栏输入你配置虚拟主句地址便可

 

四.Nginx 虚拟主机站点配置

vhosts.conf

#静态域名配置
server {
	listen       80;
	server_name www.appTest.com;
	index index.html index.htm index.php;
	root  "D:/PHPStudy/WWW/appTest";
	
	location ~ .*\.(php|php5)?$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO  $fastcgi_path_info;
    fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
    include        fastcgi_params;
	}
 
  # 这里为图片添加为期 1小时的过时时间, 而且只容许Google, 百度和本站引用图片,其余站点禁止访问
	location ~ .*\.(ico|jpg|jpeg|png|gif)$ {
		expires 1h;
		valid_referers none blocked *.appTest.com *.google.com *.baidu.com;
		if ($invalid_referer) {
			  rewrite ^/ /site_safe_img.php;
			 # return 404;
		}
	}
      location ^~ /logs/{ #禁止访问日志目录
            deny all;
        }
          #目录映射(root 映射)
         location ^~ /BBS {
		root 'D:/phpStudy/WWW/';#访问的地址是D:/phpStudy/WWW/BBS/
		index index.html index.htm;
	 }
	  #目录映射(alias 映射)
         location ^~ /BBS2 {
		alias 'D:/phpStudy/WWW/TestBBS/';#访问的地址是D:/phpStudy/WWW/TestBBS/
		index index.html index.htm;
	 }
	log_format www.appTest.com '$remote_addr - $remote_user [$time_local] $request'
	'$status $body_bytes_sent $http_referer '
	'$http_user_agent $http_x_forwarded_for';
	 access_log  "D:/PHPStudy/WWW/appTest/logs/www.appTest.com.log.txt" www.appTest.com;
}
#动态二级域名配置
server
   {
     listen       80;
     server_name  ~^(.+)?\.phpTest\.com$;
     index index.html index.htm;
     if ($host = phpTest.com){
         rewrite ^ http://www.phpTest.com permanent;
     }
     root  D:/PHPStudy/WWW/phpTest/$1/;
   }

为了配合防盗链,咱们有必要设置一个防盗链图片广告,来利用盗链信息

site_safe_img.php

<?php

	$filePath = __dir__.DIRECTORY_SEPARATOR.'IMG_02.jpg';

	$file = fopen($filePath,"r"); // 打开文件
	// 输入文件标签
	Header("Content-type: Image/jpeg");
	Header("Accept-Ranges: bytes");
	Header("Accept-Length: ".filesize($filePath));
	Header("Content-Disposition: inline; filename="."site_safe_img.jpg");
	// 输出文件内容
	echo fread($file,filesize($filePath));
	fclose($file);
	exit();

?>

ngnix

include vhosts.conf;

Apache 的 httpd.conf 详解

http://blog.csdn.net/scliu0718/article/details/7198889

Nginx 配置之完整篇

Nginx 配置之性能篇

Nginx 配置之安全篇

相关文章
相关标签/搜索