IBM-IHS/HTTPD-SetEnvIf指令-图片等静态与JSP等动态资源分离

IBM-IHS/HTTPD-SetEnvIf指令:动静分离、设置变量

使用SetEnvIf,可以控制静态资源由IHS(IBM HTTP SERVER)/HTTPD解析,而不需要转发到后端应用服务器,如无需WAS解析,实现静态与动态资源分离。
通过SetEnvIf设置变量,可以与后端逻辑联动,实现版本控制,灰度发布等功能。

SKIPWAS参数

从IHS plugin 8.5.5.1及之后版本,可以使用SetEnvIf设置skipwas变量,强制plugin不处理相关请求,而交由httpd处理。
适用于动态资源与静态资源分离场景,如JSP/servlet交由后端WAS处理,而图片、js、css等资源由httpd处理
如下样例:

Alias /HeaderWrite1/images/ihs/ /opt/IBM/HTTPServer/htdocs/images/ihs/
<Directory /opt/IBM/HTTPServer/htdocs/images>
Order deny,allow
</Directory>
SetEnvIf Request_URI "\.(jpe?g|gif|css|js)$" skipwas=1

首先指定本地图片资源路径,避免本地解析时没有找到文件
其次指定jpg|gif|css|js资源跳过

设置此参数时日志,可以看到交由后端WAS处理:

192.168.56.1 - - [17/Oct/2022:21:39:13 +0800] "GET /HeaderWrite1/images/ihs/support.gif HTTP/1.1" 200 150 39571 "" "**192.168.56.101:9080**" + 

没有设置此参数时日志,可以看到没有交由WAS处理:

192.168.56.1 - - [17/Oct/2022:21:31:51 +0800] "GET /HeaderWrite1/images/ihs/support.gif HTTP/1.1" 200 150 214 "" **"-"** +

来源:https://publib.boulder.ibm.com/httpserv/ihsdiag/plugin_alter_uri.html

静态资源分离

从IHS plugin 8.5.5.1及之后版本,可以使用之前skipwas参数
而之前版本可以使用如下方法

Alias /static /opt/IBM/HTTPServer/htdocs/images
<Directory /opt/IBM/HTTPServer/htdocs/images>
Order deny,allow
</Directory> 

RewriteEngine on
RewriteRule ^/HeaderWrite1/(.*\.(?:css|jpg|gif|js|jpeg)$) /static/$1 [PT]

采用RewriteEngine 及RewriteRule指令重写URL,将URL中css|jpg|gif|js等资源的路径,更改为static子目录下文件

RewriteRule说明
PT标记:The PT flag on the RewriteRule is what allows the WebSphere plug-in to process the rewritten URL.

正则表达式说明:
^:匹配输入字符串的开始位置
.*:匹配除 \n 以外的任何字符。
$:匹配输入行尾。
(?:pattern):非获取匹配,匹配pattern但不获取匹配结果,不进行存储供以后使用。这在使用或字符“(|)”来组合一个模式的各个部分时很有用。例如“industr(?:y|ies)”就是一个比“industry|industries”更简略的表达式。
( ):将( 和 ) 之间的表达式定义为“组”(group),并且将匹配这个表达式的字符保存到一个临时区域(一个正则表达式中最多可以保存9个),它们可以用 \1 到\9 的符号来引用。
$!:之前表达式的组引用

来源:https://publib.boulder.ibm.com/httpserv/ihsdiag/plugin_alter_uri.html

基于访问地址过滤日志或控制访问

可以通过SetEnvIf指令,动态设置变量。用于报文头、内容或日志控制。如下以日志为例

LogFormat "%h %l %u %t \"%r\" %>s %b %D \"%{testFP}e\" \"%{WAS}e\" %X " common

SetEnvIf Host '192\.168\.56\.101' testFP=DEV
SetEnvIf Host '192\.168\.31\.60' testFP=PRD
SetEnvIf testZC ^$ testFP=-1002

查看日志基于IP不同输出信息不同,一个IP的访问中标记为DEV,另一个IP的访问中标记为PRD

192.168.56.1 - - [17/Oct/2022:20:38:23 +0800] "GET /HeaderWrite1/HeaderWrite1.jsp HTTP/1.1" 200 8221 1368880 "DEV" "192.168.56.101:9080" + 
192.168.31.86 - - [17/Oct/2022:20:38:59 +0800] "GET /HeaderWrite1/HeaderWrite1.jsp HTTP/1.1" 200 8218 79473 "PRD" "192.168.56.101:9080" + 

比如 当满足某个条件时才可以访问,如当hname1有值时,可以访问。:

SetEnvIf Host '192\.168\.56\.101'  hname1=0
SetEnvIf Host '192\.168\.31\.60'  hname1=1
Allow from env=hname1

基于变量过滤输出日志

在LogFormat及CustomLog中设置参数,指定变量用作日志记录的条件。
如下,如果请求没有定义变量env=WAS(意味着它没有被WebSphere Application Server处理),则不会记录日志。

LogFormat "%h %l %u %t \"%r\" %>s %b" custom
CustomLog logs/access.log common env=WAS

注意:对于转发到WebSphere Application Server 的请求,插件创建一个名为“WAS”的环境变量,其中包含处理请求的Hostname及Port。开启PLUGIN的DEBUG日志级别,能看到环境变量的操作

 DEBUG: mod_was_ap24_http: as_handler: set env WAS "192.168.56.101:9080"

如下,如果请求匹配shop关键字(意味处理过shop上下文),会记录日志。

SetEnvIf Request_URI "^/shop/" storereq
CustomLog logs/access.log common env=storereq

如下,指定相关请求不记录日志。

SetEnvIf Request_URI \.gif nolog
SetEnvIf Request_URI \.jpg nolog
CustomLog logs/access.log combined env=!nolog

开启server-info以及server-status

server-status可以看到服务器运行状态,包括线程、访问资源、系统压力等,开启参数

LoadModule status_module modules/mod_status.so
<IfModule mod_status.c>
ExtendedStatus On
</IfModule>

<IfModule mod_status.c>
<Location /server-status>
    SetHandler server-status
    Require all denied
#   To allow access from a specific IP:
    Require ip 192.168
</Location>
</IfModule>

server-status服务器状态
server-info可以看到服务器配置状态,包括模块、参数列表等,开启方法

LoadModule info_module modules/mod_info.so
<Location /server-info>
   SetHandler server-info
   Require all denied
   Require ip 192.168
</Location>


server-info配置信息

注意:
为安全,建议开启IP控制,以及设置SSL通讯连接

LogFormat日志

监管以及监控,以及排查问题需要,需要挑战LogFormat日志

LogFormat "%h %l %u %t \"%r\" %>s %b %D" common
    Print the time taken to serve the request, in microseconds: %D
    Print the time taken to serve the request, in seconds: %T
    Print the contents of the cookie JSESSIONID in the request sent to the server (also includes the clone ID that the cookie wants the request to go back to): %{JSESSIONID}C
    Print the contents of the cookie JSESSIONID in the response sent to the client: %{JSESSIONID}o
    View and log the SSL cipher negotiated for each connection: \"SSL=%{HTTPS}e\" \"%{HTTPS_CIPHER}e\" \"%{HTTPS_KEYSIZE}e\" \"%{HTTPS_SECRETKEYSIZE}e\"
    Print the host name the request was for (useful when the site serves multiple hosts using virtual hosts): %{Host}i

Access Log Response Times (%D)

It is recommended to use %D in the LogFormat to track response times (in microseconds). The response time includes application time, queue time, and network time from/to the end-user and to/from the application.

注意%t在不同版本HTTPD中有区别
Note that the time (%t) represents the time the request arrived for HTTPD >= 2.0 and the time the response was sent back for HTTPD < 2.0.

以下是建议的缺省设置

LoadModule logio_module modules/mod_logio.so
LogIOTrackTTFB ON
LogFormat "%h %l %u %t \"%r\" %>s %b %D \"%{WAS}e\" %X %I %O %^FB %{remote}p %p" common

来源:https://publib.boulder.ibm.com/httpserv/cookbook/Web_Servers.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值