apache2.2的基本配置

apache配置文件:apache目录的conf/httpd.conf。以下主要是给apache配置端口号、静态站点目录和访问权限、多域名与虚拟主机的映射关系、反向代理。

PS:该配置文件中,开头是#号的为注释,具体配置看参考这些注释的内容

1. 设置端口号为80:

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80

2. 设置静态站点目录和访问权限

默认是的htdoc目录为:$APACHE_HOME/htdocs        ($APACHE_HOME为apache的安装目录),假如你想把这个静态文件的目录设置成D:/xxx/myHtdocs,(注意斜杠的方向,与windows的正好相反)则需要修改如下的两个设置:

DocumentRoot “D:/xxx/myHtdocs”

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory “D:/xxx/myHtdocs”>
#
# Possible values for the Options directive are “None”, “All”,
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that “MultiViews” must be named *explicitly* — “Options All”
# doesn’t give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
AllowOverride None

#
# Controls who can get stuff from this server.
#
    Order allow,deny
    Allow from all

</Directory>

3. 设置多个域名对应的虚拟主机

先把“2. 设置默认的静态站点目录和访问权限”中的”Allow from all”设置成“Deny from all”。

需求:一台主机、两个域名分别映射两个静态站点,两个静态站点对应两个不同的文件目录。

NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot “D:/xxx/site1″
<Directory “D:/xxx/site1″>
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ServerName www.site1.com
# Other directives here
</VirtualHost>

<VirtualHost *:80>
DocumentRoot “D:/xxx/site2″
<Directory “D:/xxx/site2″>
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ServerName www.site2.com
# Other directives here
</VirtualHost>

 4. 设置反向代理

加载代理模块:

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_http_module modules/mod_proxy_http.so

需求1:把域名为www.mysite.com的请求全部转给localhost:8080来处理。

设置域名的代理

<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ServerName www.mysite.com
</VirtualHost>

需求2:把域名为www.mysite.com/foo/的请求转给http://localhost:8080/aaa/bbb/

<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass /foo/ http://localhost:8080/aaa/bbb/
ProxyPassReverse /foo/ http://localhost:8080/aaa/bbb/
ServerName www.mysite.com
</VirtualHost>

5. Reference

http://httpd.apache.org/docs/2.2/  apache http server2.2版文档

原创文章:apache2.2的基本配置,转载请注明:转载自戎码一生

Post Footer automatically generated by wp-posturl plugin for wordpress.