Linux中Apache(httpd)配置反向代理二级域名
前言:
项目测试,部署,我们难免会想着将不同的项目放到不同的目录去访问,或则作为程序员的你,仅仅是为了把妹也好~本文带你简单配置httpd实现访问不同的一二级域名访问不同的项目目录结构。
阅读本文前需要你拥有自己的阿里云 ecs 服务器,并且一个域名。这里讲的httpd实现反向代理方案,至于https的代理需要ssl证书,这里不做讲解。当然了,还有nginx的实现,后面我再陆续给大家讲解。
正文:
1.先 yum list 看看有没有你可以的包
yum list httpd
2.如果有的话,比如我用 x86_64 版,就可以安装
yum install httpd.x86_64
3.添加二级域名解析
添加二级域名解析
4.修改代理配置
进入到安装目录:
cd /etc/httpd/conf vi httpd.conf
完整 httpd.conf 配置如下
ServerRoot "/etc/httpd" Listen 80 LoadModule alias_module modules/mod_alias.so LoadModule rewrite_module modules/mod_rewrite.so LoadModule php5_module modules/libphp5.so Include conf.modules.d/*.conf User apache Group apache ServerAdmin root@localhost ServerName www.example.com:80AllowOverride none Require all denied DocumentRoot "/var/www/html"AllowOverride None Require all granted Further relax access to the default document root:Options Indexes FollowSymLinks AllowOverride None Require all granted DirectoryIndex index.html Require all denied ErrorLog "logs/error_log" LogLevel warnLogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio CustomLog "logs/access_log" combinedScriptAlias /cgi-bin/ "/var/www/cgi-bin/" AllowOverride None Options None Require all granted TypesConfig /etc/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType text/html .shtml AddOutputFilter INCLUDES .shtml AddDefaultCharset UTF-8MIMEMagicFile conf/magic EnableSendfile on IncludeOptional conf.d/*.confServerAdmin scqshine@yeah.net DocumentRoot "/var/www/html" ServerName www.beijingdesigner.com ServerAlias www.beijingdesigner.com beijingdesigner.com DirectoryIndex index.php index.html index.htm ServerAdmin scqshine@yeah.net DocumentRoot "/var/www/app" ServerName app.beijingdesigner.com ServerAlias app.beijingdesigner.com DirectoryIndex index.php index.html index.htm ServerAdmin scqshine@yeah.net DocumentRoot "/var/www/contact" ServerName contact.beijingdesigner.com ServerAlias contact.beijingdesigner.com DirectoryIndex index.php index.html index.htm
编辑完后:ew!保存退出即可。
说明:
这里模拟的是三个目录,你可以将你的项目分别部署到这三个目录进行访问。
为什么端口都写成 80,因为 http 访问 ip 后不跟端口号的话,
会自动跳转到 80 端口,所以这里配好 80 后,你部署好项目,直
接重启 httpd 服务,浏览器输入你的一级域名/二级域名不带端口
号,直接就打开了你的上面配置的< DirectoryIndex >的项目了,比
如index.html/index.php/index.htm等。如果你还没准备好一个
完整的 javaweb 项目,或则 php 项目,那么你只需要在你设定好的
目录下放一个 index.html 即可,以便后续测试。
5.停止httpd 服务
service httpd stop
6.开启httpd 服务
service httpd start
7.验证
访问一级域名:
www.beijingdesigner.com
www.beijingdesigner.com
访问二级域名:
app.beijingdesigner.com
app.beijingdesigner.com
访问二级级域名:
contact.beijingdesigner.com
contact.beijingdesigner.com
好了,以上就是httpd的实现方式,后面我们循序渐进,给大家讲nginx实现方式。