mod_jk(Apache+mod_jk+tomcat)配置全攻略[轉]

          首先雖然這個mod_jk已經過時,但還是放出來大家一起學習一下,文章主要分三部分內容:

          1.第一部分:說明主要配置過程

          2.第二部分:貼出我的httpd.conf文件

          3.第三部分:對mod_jk代碼講解(來源百度)

           

          第一部分:配置

          1. 準備環境:

          操作系統:windows7

          httpd-2.2.21-win32-x86-no_ssl.msi

          apache-tomcat-6.0.7

          apache-tomcat-5.0.7

          tomcat-connectors-1.2.32-windows-i386-httpd-2.2.x

          jdk1.5

          2. 下載APACHE

          這里下載的是APACHE2.2.21版本

          3. 下載JK(Tomcat Connector)

          Jk是apache和tomcat的連接器,也可以做負載均衡器,主要是apache通過jk找到tomcat。

          下載地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/

          Jk的版本要與apache的版本對應如:mod_jk-1.2.32-httpd-2.2.x.zip其匹配的Apache為2.2.版本以上可用

          4. 下載TOMCAT

          apache-tomcat-6.0.7.rar

          5. 配置修改過程

          1) Apache配置

          將Tomcat Connector文件mod_jk.so拷貝到Apache安裝目錄的modules目錄下。

          在Apache安裝目錄下找到conf/httpd.conf文件,使用文本編輯器打開:

          偽靜態修改

          將注釋放開

          找到AllowOverride None 將其修改為 All 內容如下:

          1. <Directory />
          2. Options FollowSymLinks
          3. AllowOverride All
          4. Order deny,allow
          5. Deny from all
          6. </Directory>

          Tomcat Connector關聯項增加

          在LoadModules末尾處增加一下內容:

          1. #加載mod_jk連接
          2. LoadModule jk_module modules/mod_jk.so
          3. ### 配置 mod_jk
          4. JkWorkersFile "conf\workers.properties" #加載集群中的workers
          5. #此處是指定分配給tomcat的請求 例如*.do *.jsp
          6. JkMount /*.jsp controller
          7. JkLogFile logs/mod_jk.log #指定jk的日志輸出文件
          8. JkLogLevel warn #指定日志級別
          9. 找到IfModule dir_module 修改默認訪問地址,需要根據具體項目實際情況填寫,我這里使用的是index.jsp 修改如下:
          10. <IfModule dir_module>
          11. DirectoryIndex index.jsp
          12. </IfModule>
          13. 找到Virtual hosts 去掉虛擬主機注釋
          14. # Virtual hosts
          15. #Include conf/extra/httpd-vhosts.conf 將 “#”去掉
          16. Include conf/extra/httpd-vhosts.conf
          17. 加載代理(暫時未去掉)
          18. #LoadModule proxy_module modules/mod_proxy.so
          19. #LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
          20. #LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
          21. #LoadModule proxy_connect_module modules/mod_proxy_connect.so
          22. #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so

          2) Tomcat Connector配置

          在Apache配置目錄Apache2.2\conf創建workers.properties配置文件,該文件主要用于配置Apache與Tomcat的集成要用到的Tomcat實例和負載均衡分發控制器。

          Workers.properties文件放置一下內容;

          1. #下面是分發控制器 注意不要放tomcat實例
          2. worker.list=lbcontroller
          3. #Tomcat1實例配置 這里要和Tomcat配置文件Service.xml的jvmRoute保持一致
          4. worker.test1.host=localhost
          5. worker.test1.port=8009
          6. worker.test1.type=ajp13
          7. #分發權重 值越大負載越大
          8. worker.tomcat1.lbfactor=1
          9. #Tomcat2實例配置
          10. worker.test2.host=localhost
          11. worker.test2.port=9009
          12. worker.test2.type=ajp13
          13. #分發權重 值越大負載越大
          14. worker.tomcat2.lbfactor=1
          15. #負載均衡分發控制器
          16. worker.lbcontroller.type=lb
          17. worker.lbcontroller.balance_workers=test1,test2

          Tomcat配置文件Service.xml主要注意兩個地方,一個是Engine節點需要增加節點標識jvmRoute,一個是將原本注釋掉的Session復制節點改為有效。具體如下:

          1. <!--jvmRoute在各個Tomcat配置中不能重復且要與worker.properties文件中的名稱一致-->
          2. <Engine name="Catalina" defaultHost="localhost" jvmRoute="test1">
          3. <!--session復制內容-->
          4. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
          5. channelSendOptions="8">
          6. <Manager className="org.apache.catalina.ha.session.DeltaManager"
          7. expireSessionsOnShutdown="false"
          8. notifyListenersOnReplication="true"/>
          9. <Channel className="org.apache.catalina.tribes.group.GroupChannel">
          10. <Membership className="org.apache.catalina.tribes.membership.McastService"
          11. address="228.0.0.4"
          12. port="45564"
          13. frequency="500"
          14. dropTime="3000"/>
          15. <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
          16. address="auto"
          17. port="4000"
          18. autoBind="100"
          19. selectorTimeout="5000"
          20. maxThreads="6"/>
          21. <!-- timeout="60000"-->
          22. <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
          23. <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
          24. </Sender>
          25. <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
          26. <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
          27. <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
          28. </Channel>
          29. <!--過濾的文件-->
          30. <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
          31. <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
          32. <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
          33. tempDir="/tmp/war-temp/"
          34. deployDir="/tmp/war-deploy/"
          35. watchDir="/tmp/war-listen/"
          36. watchEnabled="false"/>
          37. <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
          38. <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
          39. </Cluster>
          40. <!--session復制內容-->

          <!—Host節點增加一下內容表示站點根路徑-->

          1. <Context path="/sc" docBase="." privileged="true"/>

          我們分別將兩個Tomcat配置文件中的jvmRoute設置為tomcat1、tomcat2,Server節點 端口分別配置為8005和9005, Connector節點端口分別配置為8080和9090,AJPConnector端口分別配置為8009和9009,Connector端口配置參照單主機多站點場景。請注意兩個Tomcat配置文件Host節點的域名配置必須一樣,Server.xml中的jvmRoute名稱必須和worker.properties中的tomcat實例名稱一致,不然無法實現session_stricky。

           

          1. *****************************************************************************
          2. 如果需要實現session 復制 需要在Tomcat 下conf/web.xml 中加上<distributable/>
          3. <?xml version="1.0" encoding="ISO-8859-1"?>
          4. 省略N多代碼。。。。。。
          5. <welcome-file-list>
          6. <welcome-file>index.html</welcome-file>
          7. <welcome-file>index.htm</welcome-file>
          8. <welcome-file>index.jsp</welcome-file>
          9. </welcome-file-list>
          10. <distributable/>
          11. </web-app>
          12. *****************************************************************************


          第二部分:httpd.conf文件

          1. #
          2. # This is the main Apache HTTP server configuration file. It contains the
          3. # configuration directives that give the server its instructions.
          4. # See <URL:http://httpd.apache.org/docs/2.2> for detailed information.
          5. # In particular, see
          6. # <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>
          7. # for a discussion of each configuration directive.
          8. #
          9. # Do NOT simply read the instructions in here without understanding
          10. # what they do. They're here only as hints or reminders. If you are unsure
          11. # consult the online docs. You have been warned.
          12. #
          13. # Configuration and logfile names: If the filenames you specify for many
          14. # of the server's control files begin with "/" (or "drive:/" for Win32), the
          15. # server will use that explicit path. If the filenames do *not* begin
          16. # with "/", the value of ServerRoot is prepended -- so "logs/foo.log"
          17. # with ServerRoot set to "D:/Program Files/Apache Software Foundation/Apache2.2" will be interpreted by the
          18. # server as "D:/Program Files/Apache Software Foundation/Apache2.2/logs/foo.log".
          19. #
          20. # NOTE: Where filenames are specified, you must use forward slashes
          21. # instead of backslashes (e.g., "c:/apache" instead of "c:\apache").
          22. # If a drive letter is omitted, the drive on which httpd.exe is located
          23. # will be used by default. It is recommended that you always supply
          24. # an explicit drive letter in absolute paths to avoid confusion.
          25. #
          26. # ServerRoot: The top of the directory tree under which the server's
          27. # configuration, error, and log files are kept.
          28. #
          29. # Do not add a slash at the end of the directory path. If you point
          30. # ServerRoot at a non-local disk, be sure to point the LockFile directive
          31. # at a local disk. If you wish to share the same ServerRoot for multiple
          32. # httpd daemons, you will need to change at least LockFile and PidFile.
          33. #
          34. ServerRoot "D:/Program Files/Apache Software Foundation/Apache2.2"
          35. #
          36. # Listen: Allows you to bind Apache to specific IP addresses and/or
          37. # ports, instead of the default. See also the <VirtualHost>
          38. # directive.
          39. #
          40. # Change this to Listen on specific IP addresses as shown below to
          41. # prevent Apache from glomming onto all bound IP addresses.
          42. #
          43. #Listen 12.34.56.78:80
          44. Listen 8080
          45. #
          46. # Dynamic Shared Object (DSO) Support
          47. #
          48. # To be able to use the functionality of a module which was built as a DSO you
          49. # have to place corresponding `LoadModule' lines at this location so the
          50. # directives contained in it are actually available _before_ they are used.
          51. # Statically compiled modules (those listed by `httpd -l') do not need
          52. # to be loaded here.
          53. #
          54. # Example:
          55. # LoadModule foo_module modules/mod_foo.so
          56. #
          57. LoadModule actions_module modules/mod_actions.so
          58. LoadModule alias_module modules/mod_alias.so
          59. LoadModule asis_module modules/mod_asis.so
          60. LoadModule auth_basic_module modules/mod_auth_basic.so
          61. #LoadModule auth_digest_module modules/mod_auth_digest.so
          62. #LoadModule authn_alias_module modules/mod_authn_alias.so
          63. #LoadModule authn_anon_module modules/mod_authn_anon.so
          64. #LoadModule authn_dbd_module modules/mod_authn_dbd.so
          65. #LoadModule authn_dbm_module modules/mod_authn_dbm.so
          66. LoadModule authn_default_module modules/mod_authn_default.so
          67. LoadModule authn_file_module modules/mod_authn_file.so
          68. #LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
          69. #LoadModule authz_dbm_module modules/mod_authz_dbm.so
          70. LoadModule authz_default_module modules/mod_authz_default.so
          71. LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
          72. LoadModule authz_host_module modules/mod_authz_host.so
          73. #LoadModule authz_owner_module modules/mod_authz_owner.so
          74. LoadModule authz_user_module modules/mod_authz_user.so
          75. LoadModule autoindex_module modules/mod_autoindex.so
          76. #LoadModule cache_module modules/mod_cache.so
          77. #LoadModule cern_meta_module modules/mod_cern_meta.so
          78. LoadModule cgi_module modules/mod_cgi.so
          79. #LoadModule charset_lite_module modules/mod_charset_lite.so
          80. #LoadModule dav_module modules/mod_dav.so
          81. #LoadModule dav_fs_module modules/mod_dav_fs.so
          82. #LoadModule dav_lock_module modules/mod_dav_lock.so
          83. #LoadModule dbd_module modules/mod_dbd.so
          84. #LoadModule deflate_module modules/mod_deflate.so
          85. LoadModule dir_module modules/mod_dir.so
          86. #LoadModule disk_cache_module modules/mod_disk_cache.so
          87. #LoadModule dumpio_module modules/mod_dumpio.so
          88. LoadModule env_module modules/mod_env.so
          89. #LoadModule expires_module modules/mod_expires.so
          90. #LoadModule ext_filter_module modules/mod_ext_filter.so
          91. #LoadModule file_cache_module modules/mod_file_cache.so
          92. #LoadModule filter_module modules/mod_filter.so
          93. #LoadModule headers_module modules/mod_headers.so
          94. #LoadModule ident_module modules/mod_ident.so
          95. #LoadModule imagemap_module modules/mod_imagemap.so
          96. LoadModule include_module modules/mod_include.so
          97. #LoadModule info_module modules/mod_info.so
          98. LoadModule isapi_module modules/mod_isapi.so
          99. #LoadModule ldap_module modules/mod_ldap.so
          100. #LoadModule logio_module modules/mod_logio.so
          101. LoadModule log_config_module modules/mod_log_config.so
          102. #LoadModule log_forensic_module modules/mod_log_forensic.so
          103. #LoadModule mem_cache_module modules/mod_mem_cache.so
          104. LoadModule mime_module modules/mod_mime.so
          105. #LoadModule mime_magic_module modules/mod_mime_magic.so
          106. LoadModule negotiation_module modules/mod_negotiation.so
          107. #LoadModule proxy_module modules/mod_proxy.so
          108. #LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
          109. #LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
          110. #LoadModule proxy_connect_module modules/mod_proxy_connect.so
          111. #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
          112. #LoadModule proxy_http_module modules/mod_proxy_http.so
          113. #LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
          114. #LoadModule reqtimeout_module modules/mod_reqtimeout.so
          115. #url偽靜態
          116. LoadModule rewrite_module modules/mod_rewrite.so
          117. LoadModule setenvif_module modules/mod_setenvif.so
          118. #LoadModule speling_module modules/mod_speling.so
          119. #LoadModule ssl_module modules/mod_ssl.so
          120. #LoadModule status_module modules/mod_status.so
          121. #LoadModule substitute_module modules/mod_substitute.so
          122. #LoadModule unique_id_module modules/mod_unique_id.so
          123. #LoadModule userdir_module modules/mod_userdir.so
          124. #LoadModule usertrack_module modules/mod_usertrack.so
          125. #LoadModule version_module modules/mod_version.so
          126. #LoadModule vhost_alias_module modules/mod_vhost_alias.so
          127. #加載mod_jk連接
          128. LoadModule jk_module modules/mod_jk.so
          129. #加載集群中的workers
          130. JkWorkersFile "conf/workers.properties"
          131. #此處是指定分配給tomcat的請求 例如*.do *.jsp
          132. JkMount /*.jsp lbcontroller
          133. #指定jk的日志輸出文件
          134. JkLogFile "logs/mod_jk.log"
          135. #指定日志級別
          136. JkLogLevel warn
          137. <IfModule !mpm_netware_module>
          138. <IfModule !mpm_winnt_module>
          139. #
          140. # If you wish httpd to run as a different user or group, you must run
          141. # httpd as root initially and it will switch.
          142. #
          143. # User/Group: The name (or #number) of the user/group to run httpd as.
          144. # It is usually good practice to create a dedicated user and group for
          145. # running httpd, as with most system services.
          146. #
          147. User daemon
          148. Group daemon
          149. </IfModule>
          150. </IfModule>
          151. # 'Main' server configuration
          152. #
          153. # The directives in this section set up the values used by the 'main'
          154. # server, which responds to any requests that aren't handled by a
          155. # <VirtualHost> definition. These values also provide defaults for
          156. # any <VirtualHost> containers you may define later in the file.
          157. #
          158. # All of these directives may appear inside <VirtualHost> containers,
          159. # in which case these default settings will be overridden for the
          160. # virtual host being defined.
          161. #
          162. #
          163. # ServerAdmin: Your address, where problems with the server should be
          164. # e-mailed. This address appears on some server-generated pages, such
          165. # as error documents. e.g. admin@your-domain.com
          166. #
          167. ServerAdmin localhost
          168. #
          169. # ServerName gives the name and port that the server uses to identify itself.
          170. # This can often be determined automatically, but we recommend you specify
          171. # it explicitly to prevent problems during startup.
          172. #
          173. # If your host doesn't have a registered DNS name, enter its IP address here.
          174. #
          175. ServerName localhost:8080
          176. #
          177. # DocumentRoot: The directory out of which you will serve your
          178. # documents. By default, all requests are taken from this directory, but
          179. # symbolic links and aliases may be used to point to other locations.
          180. #
          181. DocumentRoot "D:/work/work_bz/test/WebRoot"
          182. #
          183. # Each directory to which Apache has access can be configured with respect
          184. # to which services and features are allowed and/or disabled in that
          185. # directory (and its subdirectories).
          186. #
          187. # First, we configure the "default" to be a very restrictive set of
          188. # features.
          189. # 默認為 AllowOverride None
          190. <Directory />
          191. Options FollowSymLinks
          192. AllowOverride All
          193. #Order deny,allow
          194. #Deny from all
          195. Order allow,deny
          196. Allow from all
          197. Satisfy all
          198. </Directory>
          199. #首次訪問頁面
          200. <IfModule dir_module>
          201. DirectoryIndex index.jsp
          202. </IfModule>
          203. #
          204. # The following lines prevent .htaccess and .htpasswd files from being
          205. # viewed by Web clients.
          206. #
          207. <FilesMatch "^\.ht">
          208. Order allow,deny
          209. Deny from all
          210. Satisfy All
          211. </FilesMatch>
          212. #
          213. # ErrorLog: The location of the error log file.
          214. # If you do not specify an ErrorLog directive within a <VirtualHost>
          215. # container, error messages relating to that virtual host will be
          216. # logged here. If you *do* define an error logfile for a <VirtualHost>
          217. # container, that host's errors will be logged there and not here.
          218. #
          219. ErrorLog "logs/error.log"
          220. #
          221. # LogLevel: Control the number of messages logged to the error_log.
          222. # Possible values include: debug, info, notice, warn, error, crit,
          223. # alert, emerg.
          224. #
          225. LogLevel warn
          226. <IfModule log_config_module>
          227. #
          228. # The following directives define some format nicknames for use with
          229. # a CustomLog directive (see below).
          230. #
          231. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
          232. LogFormat "%h %l %u %t \"%r\" %>s %b" common
          233. <IfModule logio_module>
          234. # You need to enable mod_logio.c to use %I and %O
          235. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
          236. </IfModule>
          237. #
          238. # The location and format of the access logfile (Common Logfile Format).
          239. # If you do not define any access logfiles within a <VirtualHost>
          240. # container, they will be logged here. Contrariwise, if you *do*
          241. # define per-<VirtualHost> access logfiles, transactions will be
          242. # logged therein and *not* in this file.
          243. #
          244. CustomLog "logs/access.log" common
          245. #
          246. # If you prefer a logfile with access, agent, and referer information
          247. # (Combined Logfile Format) you can use the following directive.
          248. #
          249. #CustomLog "logs/access.log" combined
          250. </IfModule>
          251. <IfModule alias_module>
          252. #
          253. # Redirect: Allows you to tell clients about documents that used to
          254. # exist in your server's namespace, but do not anymore. The client
          255. # will make a new request for the document at its new location.
          256. # Example:
          257. # Redirect permanent /foo http://192.168.12.242/bar
          258. #
          259. # Alias: Maps web paths into filesystem paths and is used to
          260. # access content that does not live under the DocumentRoot.
          261. # Example:
          262. # Alias /webpath /full/filesystem/path
          263. #
          264. # If you include a trailing / on /webpath then the server will
          265. # require it to be present in the URL. You will also likely
          266. # need to provide a <Directory> section to allow access to
          267. # the filesystem path.
          268. #
          269. # ScriptAlias: This controls which directories contain server scripts.
          270. # ScriptAliases are essentially the same as Aliases, except that
          271. # documents in the target directory are treated as applications and
          272. # run by the server when requested rather than as documents sent to the
          273. # client. The same rules about trailing "/" apply to ScriptAlias
          274. # directives as to Alias.
          275. #
          276. ScriptAlias /cgi-bin/ "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"
          277. </IfModule>
          278. <IfModule cgid_module>
          279. #
          280. # ScriptSock: On threaded servers, designate the path to the UNIX
          281. # socket used to communicate with the CGI daemon of mod_cgid.
          282. #
          283. #Scriptsock logs/cgisock
          284. </IfModule>
          285. #
          286. # "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin" should be changed to whatever your ScriptAliased
          287. # CGI directory exists, if you have that configured.
          288. #
          289. <Directory "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin">
          290. AllowOverride None
          291. Options None
          292. Order allow,deny
          293. Allow from all
          294. </Directory>
          295. #
          296. # DefaultType: the default MIME type the server will use for a document
          297. # if it cannot otherwise determine one, such as from filename extensions.
          298. # If your server contains mostly text or HTML documents, "text/plain" is
          299. # a good value. If most of your content is binary, such as applications
          300. # or images, you may want to use "application/octet-stream" instead to
          301. # keep browsers from trying to display binary files as though they are
          302. # text.
          303. #
          304. DefaultType text/plain
          305. <IfModule mime_module>
          306. #
          307. # TypesConfig points to the file containing the list of mappings from
          308. # filename extension to MIME-type.
          309. #
          310. TypesConfig conf/mime.types
          311. #
          312. # AddType allows you to add to or override the MIME configuration
          313. # file specified in TypesConfig for specific file types.
          314. #
          315. #AddType application/x-gzip .tgz
          316. #
          317. # AddEncoding allows you to have certain browsers uncompress
          318. # information on the fly. Note: Not all browsers support this.
          319. #
          320. #AddEncoding x-compress .Z
          321. #AddEncoding x-gzip .gz .tgz
          322. #
          323. # If the AddEncoding directives above are commented-out, then you
          324. # probably should define those extensions to indicate media types:
          325. #
          326. AddType application/x-compress .Z
          327. AddType application/x-gzip .gz .tgz
          328. #
          329. # AddHandler allows you to map certain file extensions to "handlers":
          330. # actions unrelated to filetype. These can be either built into the server
          331. # or added with the Action directive (see below)
          332. #
          333. # To use CGI scripts outside of ScriptAliased directories:
          334. # (You will also need to add "ExecCGI" to the "Options" directive.)
          335. #
          336. #AddHandler cgi-script .cgi
          337. # For type maps (negotiated resources):
          338. #AddHandler type-map var
          339. #
          340. # Filters allow you to process content before it is sent to the client.
          341. #
          342. # To parse .shtml files for server-side includes (SSI):
          343. # (You will also need to add "Includes" to the "Options" directive.)
          344. #
          345. #AddType text/html .shtml
          346. #AddOutputFilter INCLUDES .shtml
          347. </IfModule>
          348. #
          349. # The mod_mime_magic module allows the server to use various hints from the
          350. # contents of the file itself to determine its type. The MIMEMagicFile
          351. # directive tells the module where the hint definitions are located.
          352. #
          353. #MIMEMagicFile conf/magic
          354. #
          355. # Customizable error responses come in three flavors:
          356. # 1) plain text 2) local redirects 3) external redirects
          357. #
          358. # Some examples:
          359. #ErrorDocument 500 "The server made a boo boo."
          360. #ErrorDocument 404 /missing.html
          361. #ErrorDocument 404 "/cgi-bin/missing_handler.pl"
          362. #ErrorDocument 402 http://192.168.12.242/subscription_info.html
          363. #
          364. #
          365. # MaxRanges: Maximum number of Ranges in a request before
          366. # returning the entire resource, or 0 for unlimited
          367. # Default setting is to accept 200 Ranges
          368. #MaxRanges 0
          369. #
          370. # EnableMMAP and EnableSendfile: On systems that support it,
          371. # memory-mapping or the sendfile syscall is used to deliver
          372. # files. This usually improves server performance, but must
          373. # be turned off when serving from networked-mounted
          374. # filesystems or if support for these functions is otherwise
          375. # broken on your system.
          376. #
          377. #EnableMMAP off
          378. #EnableSendfile off
          379. # Supplemental configuration
          380. #
          381. # The configuration files in the conf/extra/ directory can be
          382. # included to add extra features or to modify the default configuration of
          383. # the server, or you may simply copy their contents here and change as
          384. # necessary.
          385. # Server-pool management (MPM specific)修改連接數
          386. Include conf/extra/httpd-mpm.conf
          387. # Multi-language error messages
          388. #Include conf/extra/httpd-multilang-errordoc.conf
          389. # Fancy directory listings
          390. #Include conf/extra/httpd-autoindex.conf
          391. # Language settings
          392. #Include conf/extra/httpd-languages.conf
          393. # User home directories
          394. #Include conf/extra/httpd-userdir.conf
          395. # Real-time info on requests and configuration
          396. #Include conf/extra/httpd-info.conf
          397. # Virtual hosts 虛擬主機 沒有域名不需要設置
          398. #Include conf/extra/httpd-vhosts.conf
          399. # Local access to the Apache HTTP Server Manual
          400. #Include conf/extra/httpd-manual.conf
          401. # Distributed authoring and versioning (WebDAV)
          402. #Include conf/extra/httpd-dav.conf
          403. # Various default settings
          404. #Include conf/extra/httpd-default.conf
          405. # Secure (SSL/TLS) connections
          406. #Include conf/extra/httpd-ssl.conf
          407. #
          408. # Note: The following must must be present to support
          409. # starting without SSL on platforms with no /dev/random equivalent
          410. # but a statically compiled-in mod_ssl.
          411. #
          412. <IfModule ssl_module>
          413. SSLRandomSeed startup builtin
          414. SSLRandomSeed connect builtin
          415. </IfModule>


          第三部分:mod_jk代碼講解

          mod_jk 分析
          1 mod_jk模塊的總體功能
          由于tomcat的HTTP處理部分都由Java所寫(5.5.12版本以后出現了native庫,用以
          提高其I/O和SSL的性能[1]),在高并發的情況下負載較高。而apache對于靜態文件的處
          理能力比tomcat強,所以tomcat開發組開發了與apache結合使用的mod_jk模塊。該協議
          由apache作請求代理,將HTTP協議的請求轉化為AJP協議包,并傳給后端的
          tomcat。mod_jk和apache現在普遍使用AJP1.3協議[2]。它是一個二進制格式的協議,比
          字符格式的HTTP協議解析速度要快。
          除了性能的提升,mod_jk另外的一個作用可以實現apache與tomcat一對多的對應,
          使后端tomcat負載均衡。mod_jk也提供apache與tomcat鏈接情況的監控。
          mod_jk模塊的典型工作流程是這樣的:一個HTTP請求過來,mod_jk模塊根據其URI選
          擇合適的worker來進行處理。如果是lb_worker(負載均衡的worker),就再根據各種條
          件選擇后臺合適的ajp_worker(處理AJP協議的worker)。ajp_worker將HTTP協議的包,
          組裝成AJP協議格式的包,然后選取一條空閑的鏈接,發送給后臺的tomcat服務器。等到
          后臺將數據發送過來時,接收并解析AJP協議,重新組裝成HTTP協議,然后把結果發送給
          客戶端。
          2 mod_jk模塊的框架
          2.1 線程
          從宏觀上來講,mod_jk由一個watchdog線程和一組worker線程(進程)組成。
          watchdog線程是在apache內部新創建的線程,它是一個維護線程。每隔
          JkWatchdogInterval的時間(當然,所有worker線程也有一個統一的worker.maintain 時
          間,JkWatchdogInterval應該至少大于worker.maintain),它會掃描所有worker線程。
          watchdog線程會檢查每個worker線程的空閑鏈接、負載情況、與后端的鏈接情況,并使共
          享內存同步。worker線程是就是一些ajp13,ajp14,jni,lb或status類型的線程,負責
          所有實際的工作。
          在mod_jk中,線程內(外)的同步均是通過線程鎖(pthread_mutex_lock)來實現的。
          而進程之間的全局同步,是用文件記錄鎖(flock或fcntl)來實現。進程間的數據共享是
          用這樣做的:進程啟動時,創建一個JkShmSize 大小的文件,然后mmap到內存,由于各進
          程mmap到內存的是相同的鏡像,所以可以實現數據的共享,但是寫入共享內存時,要做好
          互斥。
          由于apache的基本處理方式(prefork和worker模式)就是一個線程/進程負責一個
          連接,所以mod_jk各線程中對于網絡IO處理都是阻塞的。
          2.2 worker對象
          從具體的worker對象的角度來講,mod_jk由ajp_worker、jni_worker、lb_worker和
          status_worker組成。這些對象參考了設計模式中factory的模型。每類worker都有生產
          workers的factory。
          在mod_jk中,其中的worker主要在worker.list中列出。其中,lb_worker可以含有
          balance_workers,以lb_sub_worker的形式存儲于lb_worker中。lb_sub_worker可以是各
          種類型的ajp_worker。所以真正工作的ajp_worker既可以“單干”,也可以由lb_worker
          來分配任務。這主要取決于URI到底映射到哪個worker上以及該worker是否在
          worker.list配置。
          lb_worker,lb_sub_worker和ajp_worker一些配置信息都位于其結構體中,而狀態信
          息或在運行中可變的參數則位于共享內存中的對應結構體中,當然也并不絕對,有些參數是
          冗余的。
          從正在運行的某個線程的角度上來講,ajp_worker就是對應了一個線程。
          3 從HTTP到AJP的處理流程
          由于mod_jk模塊是apache的處理模塊,本節主要是講述mod_jk模塊從客戶端到后端
          服務器的處理流程。中間會涉及一些apache模塊的一些結構。
          3.1 mod_jk模塊在apache中的定義
          3.1.1 mod_jk定義
          /* 這是jk模塊的主要定義結構體*/
          module AP_MODULE_DECLARE_DATA jk_module = {
          STANDARD20_MODULE_STUFF,
          NULL, /* dir config creater */
          NULL, /* dir merger --- default is to override */
          create_jk_config, /*創建 jk模塊的配置結構體*/
          merge_jk_config, /* 初始化及合并 jk模塊的配置結構體*/
          jk_cmds, /* 所有在apahce中的命令及操作函數 */
          jk_register_hooks /* 具體的操作函數處理鉤子 */
          };
          3.1.2 mod_jk模塊的主要處理函數
          /* mod_jk將各處理函數掛到相應的鉤子上,鉤子實際上就是一些函數指針。針對某個HTTP
          請求,這些函數會自上而下執行。*/
          static void jk_register_hooks(apr_pool_t * p)
          {
          /* 該函數在apache讀入配置后運行,用以初始化全局互斥鎖,jk日志,全局變量的初
          始化,以及讀入 workers.properties、uriworkermap.properties文件,初始化各worker
          的屬性,建立worker名稱到worker結構體的映射,uri到worker的映射*/
          ap_hook_post_config(jk_post_config, NULL, NULL, APR_HOOK_MIDDLE);
          /*該函數在apache主進程fork工作子進程后做的初始化工作,主要包括初始化進程內
          互斥鎖,開啟維護線程,讀入共享內存*/
          ap_hook_child_init(jk_child_init, NULL, NULL, APR_HOOK_MIDDLE);
          /* 將mod_jk的URI到真實URI,然后URI到worker的映射翻譯過程,用以找到該URI
          相應的worker_name */
          ap_hook_translate_name(jk_translate, NULL, NULL, APR_HOOK_MIDDLE);
          #if (MODULE_MAGIC_NUMBER_MAJOR > 20010808)
          /* 綁定那些alias_dir的URI到mod_jk的配置,并找到相應worker_name */
          ap_hook_map_to_storage(jk_map_to_storage, NULL, NULL, APR_HOOK_MIDDLE);
          /* 這是mod_jk的主要處理函數 */
          ap_hook_handler(jk_handler, NULL, NULL, APR_HOOK_MIDDLE);
          #endif
          }
          3.2 jk_handler函數
          jk_handler函數是mod_jk的主要處理函數,它負責將HTTP請求從apache發到tomcat,
          再從tomcat接受數據,并返回給客戶。
          它的處理過程如下:
          1. 根據前面得到的worker_name,找到相應的jk_worker(其結構體見4.4)。
          2. 初始化服務結構體jk_ws_service,主要是針對HTTP服務端的一些設置及函數(服務
          的結構體見4.5),與AJP協議關系不大。
          3. 調用jk_worker的get_endpoint函數,獲取具體的jk_endpoint結構體(函數見
          3.3.1和3.4.1,jk_endpoint的結構體定義見4.4)。
          4. 調用上述jk_endpoint的service函數。
          5. 調用上述jk_endpoint的done函數,結束與tomcat的AJP連接。
          6. 根據函數的結果處理各種狀態。
          7. 釋放資源。
          3.3 lb_worker的處理函數
          3.3.1 lb_worker的get_endpoint
          初始化lb_worker的endpoint結構體,掛上其service及done函數。
          3.3.2 lb_worker的service函數
          它的處理過程如下:
          1. 獲取共享內存中記錄的worker狀態。
          2. 如果需要綁定sessionID,獲取sessionID。
          3. 調用get_most_suitable_worker函數,獲取相應的lb_sub_worker(也即
          ajp_worker)。
          4. 調用ajp_worker的get_endpoint函數。
          5. 調用上述endpoint的service函數。
          6. 調用上述endpoint的done函數。
          7. 根據service的返回結果,更新各種記錄狀態。
          3.3.3 get_most_suitable_worker函數
          它的處理過程如下:
          1. 如果需要綁定sessionID,就根據sessionID找到工作正常的最佳worker。
          2. 如果找不到,則調用find_best_worker函數。其處理過程如下:
          i. 又調用find_best_byvalue函數。其處理過程如下:按照Round Robin的方式在
          本lb_worker中找到第一個不在錯誤狀態、也沒有停止、沒有disabled 、也不
          busy的lb_sub_worker。
          ii.如果find_best_byvalue返回錯誤,說明本lb_worker的lb_sub_worker都處于
          非正常工作狀態,需要調用find_failover_worker函數,通過
          redirect、route、domain指令來進行查找[3]。
          3.4 ajp_worker的處理函數
          3.4.1 ajp_worker的get_endpoint函數
          它的主要功能是:找到與后端tomcat的一個空閑連接。
          3.4.2 ajp_worker的ajp_service函數
          該函數主要分為 ajp_send_request和and ajp_get_reply兩個函數。
          它的處理過程如下:
          1. 獲取共享內存中的狀態。
          2. 分配AJP請求包、恢復包、POST包的內存。
          3. 調用ajp_marshal_into_msgb函數,將HTTP請求包轉化為AJP包。
          4. 將當前ajp_worker的狀態更新。
          5. 調用ajp_send_request函數。
          6. 如果發送成功,調用ajp_get_reply函數,等待并接受AJP包。
          7. 如果成功,更新當前狀態。
          3.4.2 ajp_worker的ajp_send_request函數
          它的處理過程如下:
          1. 調用jk_is_socket_connected函數,檢查即將發送的socket是否有效。它的檢查過
          程是運用select函數,等1微妙,如果成功返回說明,該socket的文件描述符至少
          在內核中還有效。
          2. 如果上一次發送超時,則調用ajp_handle_cping_cpong函數來判斷后端連接是否有
          效。該函數主要是發出了一個AJP13_CPING_REQUEST類型的AJP包,如果tomcat收
          到,應返回一個AJP13_CPONG_REPLY的包。
          3. 如果上述的測試都成功,調用ajp_connection_tcp_send_message,發送AJP包。
          3.4.3 ajp_worker的ajp_get_reply函數
          它的處理過程如下:
          1. 調用jk_is_input_event,用select等待接受數據,直到socket有接受數據。
          2. 調用ajp_connection_tcp_get_message,接受AJP包,并檢查協議頭。
          3. 調用ajp_process_callback,對該AJP包進行解析處理。其處理過程如下:
          i. 如果是JK_AJP13_SEND_HEADERS包,將其解包成HTTP包頭。如果沒有錯誤,就調
          用jk_ws_service->start_response()函數發送HTTP回復的head。返回
          JK_AJP13_SEND_HEADERS。
          ii.如果是JK_AJP13_SEND_BODY_CHUNK包,調用jk_ws_service->write發送HTTP回
          復的body。返回JK_AJP13_NO_RESPONSE。
          iii.如果是JK_AJP13_GET_BODY_CHUNK包,說明還有數據要發送到tomcat,調用
          ajp_read_into_msg_buff,繼續發送數據。返回JK_AJP13_HAS_RESPONSE。
          iv.如果是JK_AJP13_END_RESPONSE包,就說明tomcat的回復已經完成。返回
          JK_AJP13_END_RESPONSE。
          3.5 jk_ws_service的一些處理函數
          3.5.1 jk_ws_service 的 ws_start_response函數
          該函數主要是構建HTTP回復包的頭部。
          3.5.2 jk_ws_service 的 ws_write函數
          調用 apache的ap_rwrite函數,發送整個HTTP包。
          3.5.3 jk_ws_service 的 ws_read函數
          調用apache的ap_get_client_block讀入全部的request body數據。
          4 mod_jk的主要數據結構
          4.1 lb_worker
          lb_worker是負載均衡worker。主要負責調配其名下的一些lb_sub_worker 。

          1. dist/common/jk_lb_worker.h
          2. struct lb_worker
          3. {
          4. /* jk模塊通用worker結構體。包含一些回調函數,不同的worker有不同的函數實現
          5. */
          6. jk_worker_t worker;
          7. /* Shared memory worker data */
          8. jk_shm_lb_worker_t *s;
          9. char name[JK_SHM_STR_SIZ+1];
          10. /* Sequence counter starting at 0 and increasing
          11. * every time we change the config
          12. */
          13. volatile unsigned int sequence;
          14. jk_pool_t p;
          15. jk_pool_atom_t buf[TINY_POOL_SIZE];
          16. JK_CRIT_SEC cs;
          17. /*其名下的workers*/
          18. lb_sub_worker_t *lb_workers;
          19. unsigned int num_of_workers;
          20. int sticky_session;
          21. int sticky_session_force;
          22. int recover_wait_time;
          23. int max_reply_timeouts;
          24. int retries;
          25. int retry_interval;
          26. int lbmethod;
          27. int lblock;
          28. int maintain_time;
          29. unsigned int max_packet_size;
          30. unsigned int next_offset;
          31. /* Session cookie */
          32. char session_cookie[JK_SHM_STR_SIZ+1];
          33. /* Session path */
          34. char session_path[JK_SHM_STR_SIZ+1];
          35. };
          36. lb_sub_worker是由lb_worker支配的ajp_worker。當然lb_sub_worker有不同的類型。
          37. 它們的實際類型存儲在ajp_worker中。
          38. dist/common/jk_lb_worker.h
          39. struct lb_sub_worker
          40. {
          41. /* 包含ajp_worker的回調函數及其ajp_worker結構體*/
          42. jk_worker_t *worker;
          43. /* Shared memory worker data */
          44. jk_shm_lb_sub_worker_t *s;
          45. char name[JK_SHM_STR_SIZ+1];
          46. /* Sequence counter starting at 0 and increasing
          47. * every time we change the config
          48. */
          49. volatile unsigned int sequence;
          50. /* route */
          51. char route[JK_SHM_STR_SIZ+1];
          52. /* worker domain */
          53. char domain[JK_SHM_STR_SIZ+1];
          54. /* worker redirect route */
          55. char redirect[JK_SHM_STR_SIZ+1];
          56. /* worker distance */
          57. int distance;
          58. /* current activation state (config) of the worker */
          59. int activation;
          60. /* Current lb factor */
          61. int lb_factor;
          62. /* Current worker index */
          63. int i;
          64. /* Current lb reciprocal factor */
          65. jk_uint64_t lb_mult;
          66. };
          67. typedef struct lb_sub_worker lb_sub_worker_t;
          68. 4.2 ajp_worker
          69. ajp_worker是具體工作的worker,對應后端一個tomcat應用服務。
          70. dist/common/jk_ajp_common.c
          71. struct ajp_worker
          72. {
          73. /* 包含ajp_worker的回調函數*/
          74. jk_worker_t worker;
          75. /* Shared memory worker data */
          76. jk_shm_ajp_worker_t *s;
          77. char name[JK_SHM_STR_SIZ+1];
          78. /* Sequence counter starting at 0 and increasing
          79. * every time we change the config
          80. */
          81. volatile unsigned int sequence;
          82. jk_pool_t p;
          83. jk_pool_atom_t buf[TINY_POOL_SIZE];
          84. JK_CRIT_SEC cs;
          85. struct sockaddr_in worker_inet_addr; /* Contains host and port */
          86. unsigned connect_retry_attempts;
          87. const char *host;
          88. int port;
          89. int maintain_time;
          90. /*
          91. * Open connections cache...
          92. *
          93. * 1. Critical section object to protect the cache.
          94. * 2. Cache size.
          95. * 3. An array of "open" endpoints.
          96. */
          97. unsigned int ep_cache_sz;
          98. unsigned int ep_mincache_sz;
          99. unsigned int ep_maxcache_sz;
          100. int cache_acquire_timeout;
          101. ajp_endpoint_t **ep_cache;
          102. int proto; /* PROTOCOL USED AJP13/AJP14 */
          103. jk_login_service_t *login;
          104. /* Weak secret similar with ajp12, used in ajp13 */
          105. const char *secret;
          106. /*
          107. * Post physical connect handler.
          108. * AJP14 will set here its login handler
          109. */
          110. int (*logon) (ajp_endpoint_t * ae, jk_logger_t *l);
          111. /*
          112. * Handle Socket Timeouts
          113. */
          114. int socket_timeout;
          115. int socket_connect_timeout;
          116. int keepalive;
          117. int socket_buf;
          118. /*
          119. * Handle Cache Timeouts
          120. */
          121. int cache_timeout;
          122. /*
          123. * Handle Connection/Reply Timeouts
          124. */
          125. int connect_timeout; /* connect cping/cpong delay in ms (0 means
          126. disabled) */
          127. int reply_timeout; /* reply timeout delay in ms (0 means disabled) */
          128. int prepost_timeout; /* before sending a request cping/cpong timeout
          129. delay in ms (0 means disabled) */
          130. int conn_ping_interval; /* interval for sending keepalive cping packets on
          131. * unused connection */
          132. int ping_timeout; /* generic cping/cpong timeout. Used for keepalive
          133. packets or
          134. * as default for boolean valued connect and
          135. prepost timeouts.
          136. */
          137. unsigned int ping_mode; /* Ping mode flags (which types of cpings should
          138. be used) */
          139. /*
          140. * Recovery options
          141. */
          142. unsigned int recovery_opts;
          143. /*
          144. * Public property to enable the number of retry attempts
          145. * on this worker.
          146. */
          147. int retries;
          148. unsigned int max_packet_size; /* Maximum AJP Packet size */
          149. int retry_interval; /* Number of milliseconds to sleep before
          150. doing a retry */
          151. /*
          152. * HTTP status that will cause failover (0 means disabled)
          153. */
          154. unsigned int http_status_fail_num;
          155. int http_status_fail[JK_MAX_HTTP_STATUS_FAILS];
          156. };
          157. 4.3 status_worker
          158. status_worker用于產生一些狀態的統計信息。
          159. dist/common/jk_statuc.c
          160. struct status_worker
          161. {
          162. jk_pool_t p;
          163. jk_pool_atom_t buf[TINY_POOL_SIZE];
          164. const char *name;
          165. const char *css;
          166. const char *ns;
          167. const char *xmlns;
          168. const char *doctype;
          169. const char *prefix;
          170. int read_only;
          171. char **user_names;
          172. unsigned int num_of_users;
          173. int user_case_insensitive;
          174. jk_uint32_t good_mask;
          175. jk_uint32_t bad_mask;
          176. jk_worker_t worker;
          177. jk_worker_env_t *we;
          178. };
          179. 4.4 jk_worker和 jk_endpoint
          180. jk_worker是所有worker通用結構體名稱, 主要包含的是一些函數指針。它是一個類
          181. 似java中抽象類的概念,各成員函數在從factory函數生產時初始化。
          182. dist/common/jk_service.h
          183. struct jk_worker
          184. {
          185. /*
          186. * A 'this' pointer which is used by the subclasses of this class to
          187. * point to data/functions which are specific to a given protocol
          188. * (e.g. ajp12 or ajp13 or ajp14).
          189. */
          190. void *worker_private;/* 指向ajp_worker,lb_worker結構體 ...*/
          191. int type;
          192. /*
          193. * For all of the below (except destroy), the first argument is
          194. * essentially a 'this' pointer.
          195. */
          196. /* 先于init函數調用,用于分配各類worker結構體的內存,作必要的初始化
          197. * Given a worker which is in the process of being created, and a list
          198. * of configuration options (or 'properties'), check to see if it the
          199. * options are. This will always be called before the init() method.
          200. * The init/validate distinction is a bit hazy to me.
          201. * See jk_ajp13_worker.c/jk_ajp14_worker.c and jk_worker.c-
          202. >wc_create_worker()
          203. */
          204. int (JK_METHOD * validate) (jk_worker_t *w,
          205. jk_map_t *props,
          206. jk_worker_env_t *we, jk_logger_t *l);
          207. /*
          208. * Update worker either from jk_status or reloading from workers.properties
          209. */
          210. int (JK_METHOD * update) (jk_worker_t *w,
          211. jk_map_t *props,
          212. jk_worker_env_t *we, jk_logger_t *l);
          213. /*
          214. * Do whatever initialization needs to be done to start this worker up.
          215. * Configuration options are passed in via the props parameter.
          216. */
          217. int (JK_METHOD * init) (jk_worker_t *w,
          218. jk_map_t *props,
          219. jk_worker_env_t *we, jk_logger_t *l);
          220. /*
          221. * Obtain an endpoint to service a particular request. A pointer to
          222. * the endpoint is stored in pend.
          223. */
          224. int (JK_METHOD * get_endpoint) (jk_worker_t *w,
          225. jk_endpoint_t **pend, jk_logger_t *l);
          226. /*
          227. * Shutdown this worker. The first argument is not a 'this' pointer,
          228. * but rather a pointer to 'this', so that the object can be free'd (I
          229. * think -- though that doesn't seem to be happening. Hmmm).
          230. */
          231. int (JK_METHOD * destroy) (jk_worker_t **w, jk_logger_t *l);
          232. /*
          233. * Maintain this worker.
          234. */
          235. int (JK_METHOD * maintain) (jk_worker_t *w, time_t now, jk_logger_t *l);
          236. };
          237. /* jk_endpoint可以稱之為通用終端服務結構體,各類worker可能有自己的endpoint結構
          238. 體。比如ajp_worker, 該終端是用來做具體工作的,比如發出請求,接收AJP數據等等。如
          239. 果是lb_worker,該終端是的作用是找出合適ajp_worker, 把任務交給它。* /
          240. struct jk_endpoint
          241. {
          242. jk_uint64_t rd;
          243. jk_uint64_t wr;
          244. /*
          245. * Flag to pass back recoverability status from
          246. * a load balancer member to the load balancer itself.
          247. * Depending on the configuration and request status
          248. * recovery is not allowed.
          249. */
          250. int recoverable;
          251. /*
          252. * A 'this' pointer which is used by the subclasses of this class to
          253. * point to data/functions which are specific to a given protocol
          254. * (e.g. ajp12 or ajp13 or ajp14).
          255. */
          256. void *endpoint_private;
          257. /* 該函數是具體的服務函數
          258. * Forward a request to the servlet engine. The request is described
          259. * by the jk_ws_service_t object.
          260. * is_error is either 0 meaning recoverable or set to
          261. * the HTTP error code.
          262. */
          263. int (JK_METHOD * service) (jk_endpoint_t *e,
          264. jk_ws_service_t *s,
          265. jk_logger_t *l, int *is_error);
          266. /*
          267. * Called when this particular endpoint has finished processing a
          268. * request. For some protocols (e.g. ajp12), this frees the memory
          269. * associated with the endpoint. For others (e.g. ajp13/ajp14), this can
          270. * return the endpoint to a cache of already opened endpoints.
          271. *
          272. * Note that the first argument is *not* a 'this' pointer, but is
          273. * rather a pointer to a 'this' pointer. This is necessary, because
          274. * we may need to free this object.
          275. */
          276. int (JK_METHOD * done) (jk_endpoint_t **p, jk_logger_t *l);
          277. };
          278. 4.5 jk_ws_service
          279. 該結構體是與apache相關的一些參數或函數,面向HTTP協議,與AJP協議不太相關。
          280. dist/common/jk_service.h
          281. struct jk_ws_service
          282. {
          283. /*
          284. * A 'this' pointer which is used by the subclasses of this class to
          285. * point to data which is specific to a given web server platform
          286. * (e.g. Apache or IIS).
          287. */
          288. void *ws_private;
          289. /*
          290. * Provides memory management. All data specific to this request is
          291. * allocated within this pool, which can then be reclaimed at the end
          292. * of the request handling cycle.
          293. *
          294. * Alive as long as the request is alive.
          295. */
          296. jk_pool_t *pool;
          297. /*
          298. * CGI Environment needed by servlets
          299. */
          300. const char *method;
          301. const char *protocol;
          302. char *req_uri;
          303. const char *remote_addr;
          304. const char *remote_host;
          305. const char *remote_user;
          306. const char *auth_type;
          307. const char *query_string;
          308. const char *server_name;
          309. unsigned server_port;
          310. char *server_software;
          311. jk_uint64_t content_length; /* 64 bit integer that represents the content */
          312. /* length should be 0 if unknown. */
          313. unsigned is_chunked; /* 1 if content length is unknown (chunked rq) */
          314. unsigned no_more_chunks; /* 1 if last chunk has been read */
          315. jk_uint64_t content_read; /* number of bytes read */
          316. /*
          317. * SSL information
          318. *
          319. * is_ssl - True if request is in ssl connection
          320. * ssl_cert - If available, base64 ASN.1 encoded client certificates.
          321. * ssl_cert_len - Length of ssl_cert, 0 if certificates are not available.
          322. * ssl_cipher - The ssl cipher suite in use.
          323. * ssl_session - The ssl session string
          324. *
          325. * In some servers it is impossible to extract all this information, in this
          326. * case, we are passing NULL.
          327. */
          328. int is_ssl;
          329. char *ssl_cert;
          330. unsigned ssl_cert_len;
          331. char *ssl_cipher;
          332. char *ssl_session;
          333. /*
          334. * SSL extra information for Servlet 2.3 API
          335. *
          336. * ssl_key_size - ssl key size in use
          337. */
          338. int ssl_key_size;
          339. /*
          340. * Headers, names and values.
          341. */
          342. char **headers_names; /* Names of the request headers */
          343. char **headers_values; /* Values of the request headers */
          344. unsigned num_headers; /* Number of request headers */
          345. /*
          346. * Request attributes.
          347. *
          348. * These attributes that were extracted from the web server and are
          349. * sent to Tomcat.
          350. *
          351. * The developer should be able to read them from the ServletRequest
          352. * attributes. Tomcat is required to append org.apache.tomcat. to
          353. * these attribute names.
          354. */
          355. char **attributes_names; /* Names of the request attributes */
          356. char **attributes_values; /* Values of the request attributes */
          357. unsigned num_attributes; /* Number of request attributes */
          358. /*
          359. * The route is in use when the adapter load balance among
          360. * several workers. It is the ID of a specific target in the load balance
          361. * group. We are using this variable to implement target session
          362. * affinity
          363. */
          364. const char *route;
          365. /* Temp solution for auth. For native1 it'll be sent on each request,
          366. if an option is present. For native2 it'll be sent with the first
          367. request. On java side, both cases will work. For tomcat3.2 or
          368. a version that doesn't support secret - don't set the secret,
          369. and it'll work.
          370. */
          371. const char *secret;
          372. /*
          373. * Area to get POST data for fail-over recovery in POST
          374. */
          375. jk_msg_buf_t *reco_buf;
          376. int reco_status;
          377. /*
          378. * If set call flush after each write
          379. */
          380. int flush_packets;
          381. /*
          382. * If set call flush after AJP13_SEND_HEADERS.
          383. */
          384. int flush_header;
          385. /*
          386. * service extensions
          387. */
          388. svc_extension_t extension;
          389. /*
          390. * JK_TRUE if response headers have been sent back
          391. */
          392. int response_started;
          393. /*
          394. * JK_TRUE if response should not be send to the client
          395. */
          396. int response_blocked;
          397. /*
          398. * HTTP status sent from container.
          399. */
          400. int http_response_status;
          401. /* Uri worker map. Added for virtual host support
          402. */
          403. jk_uri_worker_map_t *uw_map;
          404. /* 下面這些回調函數實現都可以在mod_jk.c找到
          405. * Callbacks into the web server. For each, the first argument is
          406. * essentially a 'this' pointer. All return JK_TRUE on success
          407. * and JK_FALSE on failure.
          408. */
          409. /*
          410. * Send the response headers to the browser.
          411. */
          412. int (JK_METHOD * start_response) (jk_ws_service_t *s,
          413. int status,
          414. const char *reason,
          415. const char *const *header_names,
          416. const char *const *header_values,
          417. unsigned num_of_headers);
          418. /*
          419. * Read a chunk of the request body into a buffer. Attempt to read len
          420. * bytes into the buffer. Write the number of bytes actually read into
          421. * actually_read.
          422. */
          423. int (JK_METHOD * read) (jk_ws_service_t *s,
          424. void *buffer,
          425. unsigned len, unsigned *actually_read);
          426. /*
          427. * Write a chunk of response data back to the browser.
          428. */
          429. int (JK_METHOD * write) (jk_ws_service_t *s,
          430. const void *buffer, unsigned len);
          431. /*
          432. * Flush a chunk of response data back to the browser.
          433. */
          434. void (JK_METHOD * flush) (jk_ws_service_t *s);
          435. /*
          436. * Done with sending response back to the browser.
          437. */
          438. void (JK_METHOD * done) (jk_ws_service_t *s);
          439. /*
          440. * If set do not reuse socket after each full response
          441. */
          442. int disable_reuse;
          443. /*
          444. * Add more data to log facilities.
          445. */
          446. void (JK_METHOD * add_log_items) (jk_ws_service_t *s,
          447. const char *const *log_names,
          448. const char *const *log_values,
          449. unsigned num_of_items);
          450. /*
          451. * Iterate through all vhosts
          452. */
          453. void *(JK_METHOD * next_vhost) (void *d);
          454. /*
          455. * String representation of a vhost
          456. */
          457. void (JK_METHOD * vhost_to_text) (void *d, char *buf, size_t len);
          458. /*
          459. * Get uw_map associated with a vhost
          460. */
          461. jk_uri_worker_map_t *(JK_METHOD * vhost_to_uw_map) (void *d);
          462. };
          1. 4.6 共享內存中一些數據結構
          2. 共享內存中的數據結構基本上都是記錄之用。有些參數在運行中會實時變化。
          1. dist/common/jk_shm.h
          2. /** jk shm generic worker record structure */
          3. struct jk_shm_worker_header
          4. {
          5. int id;
          6. int type;
          7. /* worker name */
          8. char name[JK_SHM_STR_SIZ+1];
          9. /* Sequence counter starting at 0 and increasing
          10. * every time we change the config
          11. */
          12. volatile unsigned int sequence;
          13. };
          14. typedef struct jk_shm_worker_header jk_shm_worker_header_t;
          15. /** jk shm ajp13/ajp14 worker record structure */
          16. struct jk_shm_ajp_worker
          17. {
          18. jk_shm_worker_header_t h;
          19. /* Configuration data mirrored from ajp_worker */
          20. int cache_timeout;
          21. int connect_timeout;
          22. int reply_timeout;
          23. int prepost_timeout;
          24. unsigned int recovery_opts;
          25. int retries;
          26. int retry_interval;
          27. unsigned int max_packet_size;
          28. /* current error state (runtime) of the worker */
          29. volatile int state;
          30. /* Statistical data */
          31. /* Number of currently busy channels */
          32. volatile int busy;
          33. /* Maximum number of busy channels
          34. 該參數并非我們的maxbusy,它是該worker自程序運行以來busy的最大值 */
          35. volatile int max_busy;
          36. volatile time_t error_time;
          37. /* Number of bytes read from remote */
          38. volatile jk_uint64_t readed;
          39. /* Number of bytes transferred to remote */
          40. volatile jk_uint64_t transferred;
          41. /* Number of times the worker was used */
          42. volatile jk_uint64_t used;
          43. /* Number of times the worker was used - snapshot during maintenance */
          44. volatile jk_uint64_t used_snapshot;
          45. /* Number of non 200 responses */
          46. volatile jk_uint32_t errors;
          47. /* Decayed number of reply_timeout errors */
          48. volatile jk_uint32_t reply_timeouts;
          49. /* Number of client errors */
          50. volatile jk_uint32_t client_errors;
          51. /* Last reset time */
          52. volatile time_t last_reset;
          53. volatile time_t last_maintain_time;
          54. };
          55. typedef struct jk_shm_ajp_worker jk_shm_ajp_worker_t;
          56. /** jk shm lb sub worker record structure */
          57. struct jk_shm_lb_sub_worker
          58. {
          59. jk_shm_worker_header_t h;
          60. /* route */
          61. char route[JK_SHM_STR_SIZ+1];
          62. /* worker domain */
          63. char domain[JK_SHM_STR_SIZ+1];
          64. /* worker redirect route */
          65. char redirect[JK_SHM_STR_SIZ+1];
          66. /* Number of currently busy channels */
          67. volatile int busy;
          68. /* worker distance */
          69. volatile int distance;
          70. /* current activation state (config) of the worker */
          71. volatile int activation;
          72. /* current error state (runtime) of the worker */
          73. volatile int state;
          74. /* Current lb factor */
          75. volatile int lb_factor;
          76. /* 我們的參數加在這里*/
          77. volatile int maxbusy;
          78. /* Current lb reciprocal factor */
          79. volatile jk_uint64_t lb_mult;
          80. /* Current lb value */
          81. volatile jk_uint64_t lb_value;
          82. /* Statistical data */
          83. volatile time_t error_time;
          84. /* Number of times the worker was elected - snapshot during maintenance */
          85. volatile jk_uint64_t elected_snapshot;
          86. /* Number of non 200 responses */
          87. volatile jk_uint32_t errors;
          88. };
          89. typedef struct jk_shm_lb_sub_worker jk_shm_lb_sub_worker_t;
          90. /** jk shm lb worker record structure */
          91. struct jk_shm_lb_worker
          92. {
          93. jk_shm_worker_header_t h;
          94. /* Number of currently busy channels,該值是其名下ajp_worker的busy值之和*/
          95. volatile int busy;
          96. /* Maximum number of busy channels,該值是其名下ajp_worker的max_busy值之和
          97. */
          98. volatile int max_busy;
          99. int sticky_session;
          100. int sticky_session_force;
          101. int recover_wait_time;
          102. int max_reply_timeouts;
          103. int retries;
          104. int retry_interval;
          105. int lbmethod;
          106. int lblock;
          107. unsigned int max_packet_size;
          108. /* Last reset time */
          109. volatile time_t last_reset;
          110. volatile time_t last_maintain_time;
          111. /* Session cookie */
          112. char session_cookie[JK_SHM_STR_SIZ+1];
          113. /* Session path */
          114. char session_path[JK_SHM_STR_SIZ+1];
          115. };
          116. typedef struct jk_shm_lb_worker jk_shm_lb_worker_t;

           

          轉自:http://blog.csdn.net/platformer/article/details/7175054



          眼鏡蛇

          posted on 2013-11-01 18:04 眼鏡蛇 閱讀(2834) 評論(0)  編輯  收藏 所屬分類: JavaApachemod_jk

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 佛冈县| 湾仔区| 比如县| 伊川县| 万州区| 漠河县| 攀枝花市| 历史| 恭城| 呼伦贝尔市| 延川县| 徐汇区| 日喀则市| 清涧县| 丁青县| 德格县| 晴隆县| 濮阳市| 桐城市| 康保县| 丹巴县| 岗巴县| 孟津县| 容城县| 吐鲁番市| 深圳市| 西青区| 湘潭市| 乐亭县| 株洲县| 柯坪县| 天等县| 本溪市| 吉首市| 延长县| 玉田县| 盘山县| 朝阳县| 淳安县| 海门市| 三明市|