linux安装Apache代理及配置httpd


有些linux版本中已经预置了有Apache,并且已经是服务了,所有首先检查系统中是否已经存在httpd的服务
ps -ef|grep httpd

查看linux系统服务中有没有httpd
chkconfig --list


如果有服务就
关闭httpd服务自启动
chkconfig httpd off

停止httpd服务
service httpd stop

卸载httpd软件

#先检查安装包的名字
rpm -qa|grep httpd

删除软件包
rpm -e httpd

删除httpd.conf文件


tar xvzf httpd-2.4.18.tar.gz -C /usr/local/src
cd /usr/local/src/httpd-2.4.18

ps -ef | grep httpd

cd /usr/local/src/httpd-2.4.18

 ./configure --prefix=/usr/local/apache2.4 --enable-so --enable-ssl --enable-vhost-alias --enable-rewrite --enable-deflate --enable-mods-shared=all --with-mpm=worker --enable-mod_proxy --enable-proxy_connect --enable-proxy_http --enable-status --with-z=/usr/local/zlib --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util

make

make install

/usr/local/apache2.4/bin/apachectl -t

/usr/local/apache2.4/bin/apachectl start

ps -ef | grep httpd

/usr/local/apache2.4/bin/apachectl stop

cp /usr/local/apache2.4/bin/apachectl /etc/rc.d/init.d/httpd

vi /etc/rc.d/init.d/httpd

ll /etc/rc.d/init.d/httpd

chkconfig --add httpd
chkconfig --list
chkconfig httpd on

service httpd start
service httpd stopt
service httpd stop


安装完了设置配置文件
找到apache的安装目录下的conf/httpd.conf文件

配置监听端口

Listen 80
这一行是用来配置监听端口的,默认配置是80,你可以随意修改为你喜欢的端口号。
该配置项可以同时配置多个,实现同时监听多个端口

ServerRoot "/usr/local/apache2.4" #你的apache软件安装的位置。其它指定的目录如果没有指定绝对路径,则目录是相对于该目录。

ServerName 127.0.0.1:80 #主站点名称(或网站的域名)。

ServerAdmin xxx@xxx.com #管理员的邮件地址。

DocumentRoot "/usr/local/apache2.4/web" #主站点的网页存储位置。

<Directory "/usr/local/apache2.4/web">
    #
    # 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.4/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:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

这段<Directory>属性配置

Options:配置在特定目录使用哪些特性,常用的值和基本含义如下:
Indexes: 当用户访问该目录时,如果用户找不到DirectoryIndex指定的主页文件(例如index.html),则返回该目录下的文件列表给用户。 
FollowSymLinks: 在该目录下允许文件系统使用符号连接。 
ExecCGI: 在该目录下允许执行CGI脚本。
SymLinksIfOwnerMatch: 当使用符号连接时,只有当符号连接的文件拥有者与实际文件的拥有者相同时才可以访问。

AllowOverride:允许存在于.htaccess文件中的指令类型(.htaccess文件名是可以改变的,其文件名由AccessFileName指令决定): ​
None: 当AllowOverride被设置为None时。不搜索该目录下的.htaccess文件(可以减小服务器开销)。
All: 在.htaccess文件中可以使用所有的指令。 


Order:控制在访问时Allow和Deny两个访问规则哪个优先: 
Allow:允许访问的主机列表(可用域名或子网,例如:Allow from 192.168.0.0/16)。 
Deny:拒绝访问的主机列表。 

Require all granted 对目录给予授权,这是6.0手动编译安装的apache时,定义被访问目录的权限


访问日志设置 
<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    #
    # The location and format of the access logfile (Common Logfile Format).
    # If you do not define any access logfiles within a <VirtualHost>
    # container, they will be logged here.  Contrariwise, if you *do*
    # define per-<VirtualHost> access logfiles, transactions will be
    # logged therein and *not* in this file.
    #
    CustomLog "logs/access_log" common

    #
    # If you prefer a logfile with access, agent, and referer information
    # (Combined Logfile Format) you can use the following directive.
    #
    #CustomLog "logs/access_log" combined
</IfModule>

格式中的各个参数如下: 

%h --客户端的ip地址或主机名 

%l --The 这是由客户端 identd 判断的RFC 1413身份,输出中的符号 "-" 表示此处信息无效。 

%u --由HTTP认证系统得到的访问该网页的客户名。有认证时才有效,输出中的符号 "-" 表示此处信息无效。 

%t --服务器完成对请求的处理时的时间。 

"%r" --引号中是客户发出的包含了许多有用信息的请求内容。

%>s --这个是服务器返回给客户端的状态码。 

%b --最后这项是返回给客户端的不包括响应头的字节数。 

"%{Referer}i" --此项指明了该请求是从被哪个网页提交过来的。

"%{User-Agent}i" --此项是客户浏览器提供的浏览器识别信息。



# Virtual hosts
Include conf.d/*.conf  #在主配置文件末尾增加。


注释:httpd.conf是apache服务器的主配置文件,由于内容过多,不易查看,因此初见一个子主配置文件,当主配置文件读完之后就开始读子主配置文件,所以直接在子配置文件中修改配置文件。




反向代理

#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
在文件中找到上面这两行,把前面的 # 号注释去掉。

添加代理转发规则
ProxyPass / http://localhost:8888/
ProxyPassReverse / http://localhost:8888/


添加上面这两行即可实现拦截全部请求反向代理至http://localhost:8888/。

ProxyPass详解
ProxyPass path url 是用来配置转发规则,path不支持正则。以path打头的请求都会被拦截,然后转发至url这个路径去。
有一点要注意,就是path只是用来匹配拦截规则而已,在转发之后是不会一并带过去的。

例如:
ProxyPass /example http://localhost:8888/

这样配置的话,当拦截到 /example/abc 这样的请求时,转发至目标url时是 http://localhost:8888/abc ,而不是 http://localhost:8888/example/abc 。

ProxyPassReverse详解
ProxyPassReverse path url 指令一般与ProxyPass path url配合使用,且其path和url都与ProxyPass是一样的。
其作用是当相应请求是重定向请求时,替换其目录路径为代理服务器路径。

假设代理服务器的地址是abc.com,如果用户请求的是abc.com/example/abc ,请求到达apache后,被转发至http://localhost:8888/abc 。

假设该请求结果是需要重定向到login.html,如果没有配置ProxpassReverse的话,客户端将会重定向至http://localhost:8888/login.html 这个路径,这时候肯定会请求失败;

如果配置了ProxpassReverse的话,则apache会在返回之前将这个重定向请求修改为abc.com/example/login.html ,那么客户端发起的重定向请求也会经过apache转发至http://localhost:8888/login.html。




参考:
https://blog.csdn.net/chengxuyuanyonghu/article/details/61924018
https://www.jianshu.com/p/d2ccddea1c77

 

转载于:https://my.oschina.net/saulc/blog/1835561

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值