mod_jk(Apache+mod_jk+tomcat)配置全攻略[轉(zhuǎn)]
首先雖然這個(gè)mod_jk已經(jīng)過時(shí),但還是放出來大家一起學(xué)習(xí)一下,文章主要分三部分內(nèi)容:1.第一部分:說明主要配置過程
2.第二部分:貼出我的httpd.conf文件
3.第三部分:對mod_jk代碼講解(來源百度)
第一部分:配置
1. 準(zhǔn)備環(huán)境:
操作系統(tǒng):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的連接器,也可以做負(fù)載均衡器,主要是apache通過jk找到tomcat。
下載地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/
Jk的版本要與apache的版本對應(yīng)如: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文件,使用文本編輯器打開:
偽靜態(tài)修改
將注釋放開
找到AllowOverride None 將其修改為 All 內(nèi)容如下:
- <Directory />
- Options FollowSymLinks
- AllowOverride All
- Order deny,allow
- Deny from all
- </Directory>
Tomcat Connector關(guān)聯(lián)項(xiàng)增加
在LoadModules末尾處增加一下內(nèi)容:
- #加載mod_jk連接
- LoadModule jk_module modules/mod_jk.so
- ### 配置 mod_jk
- JkWorkersFile "conf\workers.properties" #加載集群中的workers
- #此處是指定分配給tomcat的請求 例如*.do *.jsp
- JkMount /*.jsp controller
- JkLogFile logs/mod_jk.log #指定jk的日志輸出文件
- JkLogLevel warn #指定日志級別
- 找到IfModule dir_module 修改默認(rèn)訪問地址,需要根據(jù)具體項(xiàng)目實(shí)際情況填寫,我這里使用的是index.jsp 修改如下:
- <IfModule dir_module>
- DirectoryIndex index.jsp
- </IfModule>
- 找到Virtual hosts 去掉虛擬主機(jī)注釋
- # Virtual hosts
- #Include conf/extra/httpd-vhosts.conf 將 “#”去掉
- Include conf/extra/httpd-vhosts.conf
- 加載代理(暫時(shí)未去掉)
- #LoadModule proxy_module modules/mod_proxy.so
- #LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
- #LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
- #LoadModule proxy_connect_module modules/mod_proxy_connect.so
- #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
2) Tomcat Connector配置
在Apache配置目錄Apache2.2\conf創(chuàng)建workers.properties配置文件,該文件主要用于配置Apache與Tomcat的集成要用到的Tomcat實(shí)例和負(fù)載均衡分發(fā)控制器。
Workers.properties文件放置一下內(nèi)容;
- #下面是分發(fā)控制器 注意不要放tomcat實(shí)例
- worker.list=lbcontroller
- #Tomcat1實(shí)例配置 這里要和Tomcat配置文件Service.xml的jvmRoute保持一致
- worker.test1.host=localhost
- worker.test1.port=8009
- worker.test1.type=ajp13
- #分發(fā)權(quán)重 值越大負(fù)載越大
- worker.tomcat1.lbfactor=1
- #Tomcat2實(shí)例配置
- worker.test2.host=localhost
- worker.test2.port=9009
- worker.test2.type=ajp13
- #分發(fā)權(quán)重 值越大負(fù)載越大
- worker.tomcat2.lbfactor=1
- #負(fù)載均衡分發(fā)控制器
- worker.lbcontroller.type=lb
- worker.lbcontroller.balance_workers=test1,test2
Tomcat配置文件Service.xml主要注意兩個(gè)地方,一個(gè)是Engine節(jié)點(diǎn)需要增加節(jié)點(diǎn)標(biāo)識jvmRoute,一個(gè)是將原本注釋掉的Session復(fù)制節(jié)點(diǎn)改為有效。具體如下:
- <!--jvmRoute在各個(gè)Tomcat配置中不能重復(fù)且要與worker.properties文件中的名稱一致-->
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="test1">
- <!--session復(fù)制內(nèi)容-->
- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
- channelSendOptions="8">
- <Manager className="org.apache.catalina.ha.session.DeltaManager"
- expireSessionsOnShutdown="false"
- notifyListenersOnReplication="true"/>
- <Channel className="org.apache.catalina.tribes.group.GroupChannel">
- <Membership className="org.apache.catalina.tribes.membership.McastService"
- address="228.0.0.4"
- port="45564"
- frequency="500"
- dropTime="3000"/>
- <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
- address="auto"
- port="4000"
- autoBind="100"
- selectorTimeout="5000"
- maxThreads="6"/>
- <!-- timeout="60000"-->
- <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
- <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
- </Sender>
- <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
- <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
- <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
- </Channel>
- <!--過濾的文件-->
- <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
- <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
- <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
- tempDir="/tmp/war-temp/"
- deployDir="/tmp/war-deploy/"
- watchDir="/tmp/war-listen/"
- watchEnabled="false"/>
- <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
- <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
- </Cluster>
- <!--session復(fù)制內(nèi)容-->
<!—Host節(jié)點(diǎn)增加一下內(nèi)容表示站點(diǎn)根路徑-->
- <Context path="/sc" docBase="." privileged="true"/>
我們分別將兩個(gè)Tomcat配置文件中的jvmRoute設(shè)置為tomcat1、tomcat2,Server節(jié)點(diǎn) 端口分別配置為8005和9005, Connector節(jié)點(diǎn)端口分別配置為8080和9090,AJPConnector端口分別配置為8009和9009,Connector端口配置參照單主機(jī)多站點(diǎn)場景。請注意兩個(gè)Tomcat配置文件Host節(jié)點(diǎn)的域名配置必須一樣,Server.xml中的jvmRoute名稱必須和worker.properties中的tomcat實(shí)例名稱一致,不然無法實(shí)現(xiàn)session_stricky。
- *****************************************************************************
- 如果需要實(shí)現(xiàn)session 復(fù)制 需要在Tomcat 下conf/web.xml 中加上<distributable/>
- <?xml version="1.0" encoding="ISO-8859-1"?>
- 省略N多代碼。。。。。。
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <distributable/>
- </web-app>
- *****************************************************************************
第二部分:httpd.conf文件
- #
- # This is the main Apache HTTP server configuration file. It contains the
- # configuration directives that give the server its instructions.
- # See <URL:http://httpd.apache.org/docs/2.2> for detailed information.
- # In particular, see
- # <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>
- # for a discussion of each configuration directive.
- #
- # Do NOT simply read the instructions in here without understanding
- # what they do. They're here only as hints or reminders. If you are unsure
- # consult the online docs. You have been warned.
- #
- # Configuration and logfile names: If the filenames you specify for many
- # of the server's control files begin with "/" (or "drive:/" for Win32), the
- # server will use that explicit path. If the filenames do *not* begin
- # with "/", the value of ServerRoot is prepended -- so "logs/foo.log"
- # with ServerRoot set to "D:/Program Files/Apache Software Foundation/Apache2.2" will be interpreted by the
- # server as "D:/Program Files/Apache Software Foundation/Apache2.2/logs/foo.log".
- #
- # NOTE: Where filenames are specified, you must use forward slashes
- # instead of backslashes (e.g., "c:/apache" instead of "c:\apache").
- # If a drive letter is omitted, the drive on which httpd.exe is located
- # will be used by default. It is recommended that you always supply
- # an explicit drive letter in absolute paths to avoid confusion.
- #
- # ServerRoot: The top of the directory tree under which the server's
- # configuration, error, and log files are kept.
- #
- # Do not add a slash at the end of the directory path. If you point
- # ServerRoot at a non-local disk, be sure to point the LockFile directive
- # at a local disk. If you wish to share the same ServerRoot for multiple
- # httpd daemons, you will need to change at least LockFile and PidFile.
- #
- ServerRoot "D:/Program Files/Apache Software Foundation/Apache2.2"
- #
- # 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 8080
- #
- # Dynamic Shared Object (DSO) Support
- #
- # To be able to use the functionality of a module which was built as a DSO you
- # have to place corresponding `LoadModule' lines at this location so the
- # directives contained in it are actually available _before_ they are used.
- # Statically compiled modules (those listed by `httpd -l') do not need
- # to be loaded here.
- #
- # Example:
- # LoadModule foo_module modules/mod_foo.so
- #
- LoadModule actions_module modules/mod_actions.so
- LoadModule alias_module modules/mod_alias.so
- LoadModule asis_module modules/mod_asis.so
- LoadModule auth_basic_module modules/mod_auth_basic.so
- #LoadModule auth_digest_module modules/mod_auth_digest.so
- #LoadModule authn_alias_module modules/mod_authn_alias.so
- #LoadModule authn_anon_module modules/mod_authn_anon.so
- #LoadModule authn_dbd_module modules/mod_authn_dbd.so
- #LoadModule authn_dbm_module modules/mod_authn_dbm.so
- LoadModule authn_default_module modules/mod_authn_default.so
- LoadModule authn_file_module modules/mod_authn_file.so
- #LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
- #LoadModule authz_dbm_module modules/mod_authz_dbm.so
- LoadModule authz_default_module modules/mod_authz_default.so
- LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
- LoadModule authz_host_module modules/mod_authz_host.so
- #LoadModule authz_owner_module modules/mod_authz_owner.so
- LoadModule authz_user_module modules/mod_authz_user.so
- LoadModule autoindex_module modules/mod_autoindex.so
- #LoadModule cache_module modules/mod_cache.so
- #LoadModule cern_meta_module modules/mod_cern_meta.so
- LoadModule cgi_module modules/mod_cgi.so
- #LoadModule charset_lite_module modules/mod_charset_lite.so
- #LoadModule dav_module modules/mod_dav.so
- #LoadModule dav_fs_module modules/mod_dav_fs.so
- #LoadModule dav_lock_module modules/mod_dav_lock.so
- #LoadModule dbd_module modules/mod_dbd.so
- #LoadModule deflate_module modules/mod_deflate.so
- LoadModule dir_module modules/mod_dir.so
- #LoadModule disk_cache_module modules/mod_disk_cache.so
- #LoadModule dumpio_module modules/mod_dumpio.so
- LoadModule env_module modules/mod_env.so
- #LoadModule expires_module modules/mod_expires.so
- #LoadModule ext_filter_module modules/mod_ext_filter.so
- #LoadModule file_cache_module modules/mod_file_cache.so
- #LoadModule filter_module modules/mod_filter.so
- #LoadModule headers_module modules/mod_headers.so
- #LoadModule ident_module modules/mod_ident.so
- #LoadModule imagemap_module modules/mod_imagemap.so
- LoadModule include_module modules/mod_include.so
- #LoadModule info_module modules/mod_info.so
- LoadModule isapi_module modules/mod_isapi.so
- #LoadModule ldap_module modules/mod_ldap.so
- #LoadModule logio_module modules/mod_logio.so
- LoadModule log_config_module modules/mod_log_config.so
- #LoadModule log_forensic_module modules/mod_log_forensic.so
- #LoadModule mem_cache_module modules/mod_mem_cache.so
- LoadModule mime_module modules/mod_mime.so
- #LoadModule mime_magic_module modules/mod_mime_magic.so
- LoadModule negotiation_module modules/mod_negotiation.so
- #LoadModule proxy_module modules/mod_proxy.so
- #LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
- #LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
- #LoadModule proxy_connect_module modules/mod_proxy_connect.so
- #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
- #LoadModule proxy_http_module modules/mod_proxy_http.so
- #LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
- #LoadModule reqtimeout_module modules/mod_reqtimeout.so
- #url偽靜態(tài)
- LoadModule rewrite_module modules/mod_rewrite.so
- LoadModule setenvif_module modules/mod_setenvif.so
- #LoadModule speling_module modules/mod_speling.so
- #LoadModule ssl_module modules/mod_ssl.so
- #LoadModule status_module modules/mod_status.so
- #LoadModule substitute_module modules/mod_substitute.so
- #LoadModule unique_id_module modules/mod_unique_id.so
- #LoadModule userdir_module modules/mod_userdir.so
- #LoadModule usertrack_module modules/mod_usertrack.so
- #LoadModule version_module modules/mod_version.so
- #LoadModule vhost_alias_module modules/mod_vhost_alias.so
- #加載mod_jk連接
- LoadModule jk_module modules/mod_jk.so
- #加載集群中的workers
- JkWorkersFile "conf/workers.properties"
- #此處是指定分配給tomcat的請求 例如*.do *.jsp
- JkMount /*.jsp lbcontroller
- #指定jk的日志輸出文件
- JkLogFile "logs/mod_jk.log"
- #指定日志級別
- JkLogLevel warn
- <IfModule !mpm_netware_module>
- <IfModule !mpm_winnt_module>
- #
- # If you wish httpd to run as a different user or group, you must run
- # httpd as root initially and it will switch.
- #
- # User/Group: The name (or #number) of the user/group to run httpd as.
- # It is usually good practice to create a dedicated user and group for
- # running httpd, as with most system services.
- #
- User daemon
- Group daemon
- </IfModule>
- </IfModule>
- # 'Main' server configuration
- #
- # The directives in this section set up the values used by the 'main'
- # server, which responds to any requests that aren't handled by a
- # <VirtualHost> definition. These values also provide defaults for
- # any <VirtualHost> containers you may define later in the file.
- #
- # All of these directives may appear inside <VirtualHost> containers,
- # in which case these default settings will be overridden for the
- # virtual host being defined.
- #
- #
- # ServerAdmin: Your address, where problems with the server should be
- # e-mailed. This address appears on some server-generated pages, such
- # as error documents. e.g. admin@your-domain.com
- #
- ServerAdmin localhost
- #
- # ServerName gives the name and port that the server uses to identify itself.
- # This can often be determined automatically, but we recommend you specify
- # it explicitly to prevent problems during startup.
- #
- # If your host doesn't have a registered DNS name, enter its IP address here.
- #
- ServerName localhost:8080
- #
- # DocumentRoot: The directory out of which you will serve your
- # documents. By default, all requests are taken from this directory, but
- # symbolic links and aliases may be used to point to other locations.
- #
- DocumentRoot "D:/work/work_bz/test/WebRoot"
- #
- # Each directory to which Apache has access can be configured with respect
- # to which services and features are allowed and/or disabled in that
- # directory (and its subdirectories).
- #
- # First, we configure the "default" to be a very restrictive set of
- # features.
- # 默認(rèn)為 AllowOverride None
- <Directory />
- Options FollowSymLinks
- AllowOverride All
- #Order deny,allow
- #Deny from all
- Order allow,deny
- Allow from all
- Satisfy all
- </Directory>
- #首次訪問頁面
- <IfModule dir_module>
- DirectoryIndex index.jsp
- </IfModule>
- #
- # The following lines prevent .htaccess and .htpasswd files from being
- # viewed by Web clients.
- #
- <FilesMatch "^\.ht">
- Order allow,deny
- Deny from all
- Satisfy All
- </FilesMatch>
- #
- # ErrorLog: The location of the error log file.
- # If you do not specify an ErrorLog directive within a <VirtualHost>
- # container, error messages relating to that virtual host will be
- # logged here. If you *do* define an error logfile for a <VirtualHost>
- # container, that host's errors will be logged there and not here.
- #
- ErrorLog "logs/error.log"
- #
- # LogLevel: Control the number of messages logged to the error_log.
- # Possible values include: debug, info, notice, warn, error, crit,
- # alert, emerg.
- #
- LogLevel warn
- <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>
- <IfModule alias_module>
- #
- # Redirect: Allows you to tell clients about documents that used to
- # exist in your server's namespace, but do not anymore. The client
- # will make a new request for the document at its new location.
- # Example:
- # Redirect permanent /foo http://192.168.12.242/bar
- #
- # Alias: Maps web paths into filesystem paths and is used to
- # access content that does not live under the DocumentRoot.
- # Example:
- # Alias /webpath /full/filesystem/path
- #
- # If you include a trailing / on /webpath then the server will
- # require it to be present in the URL. You will also likely
- # need to provide a <Directory> section to allow access to
- # the filesystem path.
- #
- # ScriptAlias: This controls which directories contain server scripts.
- # ScriptAliases are essentially the same as Aliases, except that
- # documents in the target directory are treated as applications and
- # run by the server when requested rather than as documents sent to the
- # client. The same rules about trailing "/" apply to ScriptAlias
- # directives as to Alias.
- #
- ScriptAlias /cgi-bin/ "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"
- </IfModule>
- <IfModule cgid_module>
- #
- # ScriptSock: On threaded servers, designate the path to the UNIX
- # socket used to communicate with the CGI daemon of mod_cgid.
- #
- #Scriptsock logs/cgisock
- </IfModule>
- #
- # "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin" should be changed to whatever your ScriptAliased
- # CGI directory exists, if you have that configured.
- #
- <Directory "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin">
- AllowOverride None
- Options None
- Order allow,deny
- Allow from all
- </Directory>
- #
- # DefaultType: the default MIME type the server will use for a document
- # if it cannot otherwise determine one, such as from filename extensions.
- # If your server contains mostly text or HTML documents, "text/plain" is
- # a good value. If most of your content is binary, such as applications
- # or images, you may want to use "application/octet-stream" instead to
- # keep browsers from trying to display binary files as though they are
- # text.
- #
- DefaultType text/plain
- <IfModule mime_module>
- #
- # TypesConfig points to the file containing the list of mappings from
- # filename extension to MIME-type.
- #
- TypesConfig conf/mime.types
- #
- # AddType allows you to add to or override the MIME configuration
- # file specified in TypesConfig for specific file types.
- #
- #AddType application/x-gzip .tgz
- #
- # AddEncoding allows you to have certain browsers uncompress
- # information on the fly. Note: Not all browsers support this.
- #
- #AddEncoding x-compress .Z
- #AddEncoding x-gzip .gz .tgz
- #
- # If the AddEncoding directives above are commented-out, then you
- # probably should define those extensions to indicate media types:
- #
- AddType application/x-compress .Z
- AddType application/x-gzip .gz .tgz
- #
- # AddHandler allows you to map certain file extensions to "handlers":
- # actions unrelated to filetype. These can be either built into the server
- # or added with the Action directive (see below)
- #
- # To use CGI scripts outside of ScriptAliased directories:
- # (You will also need to add "ExecCGI" to the "Options" directive.)
- #
- #AddHandler cgi-script .cgi
- # For type maps (negotiated resources):
- #AddHandler type-map var
- #
- # Filters allow you to process content before it is sent to the client.
- #
- # To parse .shtml files for server-side includes (SSI):
- # (You will also need to add "Includes" to the "Options" directive.)
- #
- #AddType text/html .shtml
- #AddOutputFilter INCLUDES .shtml
- </IfModule>
- #
- # The mod_mime_magic module allows the server to use various hints from the
- # contents of the file itself to determine its type. The MIMEMagicFile
- # directive tells the module where the hint definitions are located.
- #
- #MIMEMagicFile conf/magic
- #
- # Customizable error responses come in three flavors:
- # 1) plain text 2) local redirects 3) external redirects
- #
- # Some examples:
- #ErrorDocument 500 "The server made a boo boo."
- #ErrorDocument 404 /missing.html
- #ErrorDocument 404 "/cgi-bin/missing_handler.pl"
- #ErrorDocument 402 http://192.168.12.242/subscription_info.html
- #
- #
- # MaxRanges: Maximum number of Ranges in a request before
- # returning the entire resource, or 0 for unlimited
- # Default setting is to accept 200 Ranges
- #MaxRanges 0
- #
- # EnableMMAP and EnableSendfile: On systems that support it,
- # memory-mapping or the sendfile syscall is used to deliver
- # files. This usually improves server performance, but must
- # be turned off when serving from networked-mounted
- # filesystems or if support for these functions is otherwise
- # broken on your system.
- #
- #EnableMMAP off
- #EnableSendfile off
- # Supplemental configuration
- #
- # The configuration files in the conf/extra/ directory can be
- # included to add extra features or to modify the default configuration of
- # the server, or you may simply copy their contents here and change as
- # necessary.
- # Server-pool management (MPM specific)修改連接數(shù)
- Include conf/extra/httpd-mpm.conf
- # Multi-language error messages
- #Include conf/extra/httpd-multilang-errordoc.conf
- # Fancy directory listings
- #Include conf/extra/httpd-autoindex.conf
- # Language settings
- #Include conf/extra/httpd-languages.conf
- # User home directories
- #Include conf/extra/httpd-userdir.conf
- # Real-time info on requests and configuration
- #Include conf/extra/httpd-info.conf
- # Virtual hosts 虛擬主機(jī) 沒有域名不需要設(shè)置
- #Include conf/extra/httpd-vhosts.conf
- # Local access to the Apache HTTP Server Manual
- #Include conf/extra/httpd-manual.conf
- # Distributed authoring and versioning (WebDAV)
- #Include conf/extra/httpd-dav.conf
- # Various default settings
- #Include conf/extra/httpd-default.conf
- # Secure (SSL/TLS) connections
- #Include conf/extra/httpd-ssl.conf
- #
- # Note: The following must must be present to support
- # starting without SSL on platforms with no /dev/random equivalent
- # but a statically compiled-in mod_ssl.
- #
- <IfModule ssl_module>
- SSLRandomSeed startup builtin
- SSLRandomSeed connect builtin
- </IfModule>
第三部分:mod_jk代碼講解
mod_jk 分析
1 mod_jk模塊的總體功能
由于tomcat的HTTP處理部分都由Java所寫(5.5.12版本以后出現(xiàn)了native庫,用以
提高其I/O和SSL的性能[1]),在高并發(fā)的情況下負(fù)載較高。而apache對于靜態(tài)文件的處
理能力比tomcat強(qiáng),所以tomcat開發(fā)組開發(fā)了與apache結(jié)合使用的mod_jk模塊。該協(xié)議
由apache作請求代理,將HTTP協(xié)議的請求轉(zhuǎn)化為AJP協(xié)議包,并傳給后端的
tomcat。mod_jk和apache現(xiàn)在普遍使用AJP1.3協(xié)議[2]。它是一個(gè)二進(jìn)制格式的協(xié)議,比
字符格式的HTTP協(xié)議解析速度要快。
除了性能的提升,mod_jk另外的一個(gè)作用可以實(shí)現(xiàn)apache與tomcat一對多的對應(yīng),
使后端tomcat負(fù)載均衡。mod_jk也提供apache與tomcat鏈接情況的監(jiān)控。
mod_jk模塊的典型工作流程是這樣的:一個(gè)HTTP請求過來,mod_jk模塊根據(jù)其URI選
擇合適的worker來進(jìn)行處理。如果是lb_worker(負(fù)載均衡的worker),就再根據(jù)各種條
件選擇后臺合適的ajp_worker(處理AJP協(xié)議的worker)。ajp_worker將HTTP協(xié)議的包,
組裝成AJP協(xié)議格式的包,然后選取一條空閑的鏈接,發(fā)送給后臺的tomcat服務(wù)器。等到
后臺將數(shù)據(jù)發(fā)送過來時(shí),接收并解析AJP協(xié)議,重新組裝成HTTP協(xié)議,然后把結(jié)果發(fā)送給
客戶端。
2 mod_jk模塊的框架
2.1 線程
從宏觀上來講,mod_jk由一個(gè)watchdog線程和一組worker線程(進(jìn)程)組成。
watchdog線程是在apache內(nèi)部新創(chuàng)建的線程,它是一個(gè)維護(hù)線程。每隔
JkWatchdogInterval的時(shí)間(當(dāng)然,所有worker線程也有一個(gè)統(tǒng)一的worker.maintain 時(shí)
間,JkWatchdogInterval應(yīng)該至少大于worker.maintain),它會掃描所有worker線程。
watchdog線程會檢查每個(gè)worker線程的空閑鏈接、負(fù)載情況、與后端的鏈接情況,并使共
享內(nèi)存同步。worker線程是就是一些ajp13,ajp14,jni,lb或status類型的線程,負(fù)責(zé)
所有實(shí)際的工作。
在mod_jk中,線程內(nèi)(外)的同步均是通過線程鎖(pthread_mutex_lock)來實(shí)現(xiàn)的。
而進(jìn)程之間的全局同步,是用文件記錄鎖(flock或fcntl)來實(shí)現(xiàn)。進(jìn)程間的數(shù)據(jù)共享是
用這樣做的:進(jìn)程啟動時(shí),創(chuàng)建一個(gè)JkShmSize 大小的文件,然后mmap到內(nèi)存,由于各進(jìn)
程mmap到內(nèi)存的是相同的鏡像,所以可以實(shí)現(xiàn)數(shù)據(jù)的共享,但是寫入共享內(nèi)存時(shí),要做好
互斥。
由于apache的基本處理方式(prefork和worker模式)就是一個(gè)線程/進(jìn)程負(fù)責(zé)一個(gè)
連接,所以mod_jk各線程中對于網(wǎng)絡(luò)IO處理都是阻塞的。
2.2 worker對象
從具體的worker對象的角度來講,mod_jk由ajp_worker、jni_worker、lb_worker和
status_worker組成。這些對象參考了設(shè)計(jì)模式中factory的模型。每類worker都有生產(chǎn)
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
來分配任務(wù)。這主要取決于URI到底映射到哪個(gè)worker上以及該worker是否在
worker.list配置。
lb_worker,lb_sub_worker和ajp_worker一些配置信息都位于其結(jié)構(gòu)體中,而狀態(tài)信
息或在運(yùn)行中可變的參數(shù)則位于共享內(nèi)存中的對應(yīng)結(jié)構(gòu)體中,當(dāng)然也并不絕對,有些參數(shù)是
冗余的。
從正在運(yùn)行的某個(gè)線程的角度上來講,ajp_worker就是對應(yīng)了一個(gè)線程。
3 從HTTP到AJP的處理流程
由于mod_jk模塊是apache的處理模塊,本節(jié)主要是講述mod_jk模塊從客戶端到后端
服務(wù)器的處理流程。中間會涉及一些apache模塊的一些結(jié)構(gòu)。
3.1 mod_jk模塊在apache中的定義
3.1.1 mod_jk定義
/* 這是jk模塊的主要定義結(jié)構(gòu)體*/
module AP_MODULE_DECLARE_DATA jk_module = {
STANDARD20_MODULE_STUFF,
NULL, /* dir config creater */
NULL, /* dir merger --- default is to override */
create_jk_config, /*創(chuàng)建 jk模塊的配置結(jié)構(gòu)體*/
merge_jk_config, /* 初始化及合并 jk模塊的配置結(jié)構(gòu)體*/
jk_cmds, /* 所有在apahce中的命令及操作函數(shù) */
jk_register_hooks /* 具體的操作函數(shù)處理鉤子 */
};
3.1.2 mod_jk模塊的主要處理函數(shù)
/* mod_jk將各處理函數(shù)掛到相應(yīng)的鉤子上,鉤子實(shí)際上就是一些函數(shù)指針。針對某個(gè)HTTP
請求,這些函數(shù)會自上而下執(zhí)行。*/
static void jk_register_hooks(apr_pool_t * p)
{
/* 該函數(shù)在apache讀入配置后運(yùn)行,用以初始化全局互斥鎖,jk日志,全局變量的初
始化,以及讀入 workers.properties、uriworkermap.properties文件,初始化各worker
的屬性,建立worker名稱到worker結(jié)構(gòu)體的映射,uri到worker的映射*/
ap_hook_post_config(jk_post_config, NULL, NULL, APR_HOOK_MIDDLE);
/*該函數(shù)在apache主進(jìn)程fork工作子進(jìn)程后做的初始化工作,主要包括初始化進(jìn)程內(nèi)
互斥鎖,開啟維護(hù)線程,讀入共享內(nèi)存*/
ap_hook_child_init(jk_child_init, NULL, NULL, APR_HOOK_MIDDLE);
/* 將mod_jk的URI到真實(shí)URI,然后URI到worker的映射翻譯過程,用以找到該URI
相應(yīng)的worker_name */
ap_hook_translate_name(jk_translate, NULL, NULL, APR_HOOK_MIDDLE);
#if (MODULE_MAGIC_NUMBER_MAJOR > 20010808)
/* 綁定那些alias_dir的URI到mod_jk的配置,并找到相應(yīng)worker_name */
ap_hook_map_to_storage(jk_map_to_storage, NULL, NULL, APR_HOOK_MIDDLE);
/* 這是mod_jk的主要處理函數(shù) */
ap_hook_handler(jk_handler, NULL, NULL, APR_HOOK_MIDDLE);
#endif
}
3.2 jk_handler函數(shù)
jk_handler函數(shù)是mod_jk的主要處理函數(shù),它負(fù)責(zé)將HTTP請求從apache發(fā)到tomcat,
再從tomcat接受數(shù)據(jù),并返回給客戶。
它的處理過程如下:
1. 根據(jù)前面得到的worker_name,找到相應(yīng)的jk_worker(其結(jié)構(gòu)體見4.4)。
2. 初始化服務(wù)結(jié)構(gòu)體jk_ws_service,主要是針對HTTP服務(wù)端的一些設(shè)置及函數(shù)(服務(wù)
的結(jié)構(gòu)體見4.5),與AJP協(xié)議關(guān)系不大。
3. 調(diào)用jk_worker的get_endpoint函數(shù),獲取具體的jk_endpoint結(jié)構(gòu)體(函數(shù)見
3.3.1和3.4.1,jk_endpoint的結(jié)構(gòu)體定義見4.4)。
4. 調(diào)用上述jk_endpoint的service函數(shù)。
5. 調(diào)用上述jk_endpoint的done函數(shù),結(jié)束與tomcat的AJP連接。
6. 根據(jù)函數(shù)的結(jié)果處理各種狀態(tài)。
7. 釋放資源。
3.3 lb_worker的處理函數(shù)
3.3.1 lb_worker的get_endpoint
初始化lb_worker的endpoint結(jié)構(gòu)體,掛上其service及done函數(shù)。
3.3.2 lb_worker的service函數(shù)
它的處理過程如下:
1. 獲取共享內(nèi)存中記錄的worker狀態(tài)。
2. 如果需要綁定sessionID,獲取sessionID。
3. 調(diào)用get_most_suitable_worker函數(shù),獲取相應(yīng)的lb_sub_worker(也即
ajp_worker)。
4. 調(diào)用ajp_worker的get_endpoint函數(shù)。
5. 調(diào)用上述endpoint的service函數(shù)。
6. 調(diào)用上述endpoint的done函數(shù)。
7. 根據(jù)service的返回結(jié)果,更新各種記錄狀態(tài)。
3.3.3 get_most_suitable_worker函數(shù)
它的處理過程如下:
1. 如果需要綁定sessionID,就根據(jù)sessionID找到工作正常的最佳worker。
2. 如果找不到,則調(diào)用find_best_worker函數(shù)。其處理過程如下:
i. 又調(diào)用find_best_byvalue函數(shù)。其處理過程如下:按照Round Robin的方式在
本lb_worker中找到第一個(gè)不在錯(cuò)誤狀態(tài)、也沒有停止、沒有disabled 、也不
busy的lb_sub_worker。
ii.如果find_best_byvalue返回錯(cuò)誤,說明本lb_worker的lb_sub_worker都處于
非正常工作狀態(tài),需要調(diào)用find_failover_worker函數(shù),通過
redirect、route、domain指令來進(jìn)行查找[3]。
3.4 ajp_worker的處理函數(shù)
3.4.1 ajp_worker的get_endpoint函數(shù)
它的主要功能是:找到與后端tomcat的一個(gè)空閑連接。
3.4.2 ajp_worker的ajp_service函數(shù)
該函數(shù)主要分為 ajp_send_request和and ajp_get_reply兩個(gè)函數(shù)。
它的處理過程如下:
1. 獲取共享內(nèi)存中的狀態(tài)。
2. 分配AJP請求包、恢復(fù)包、POST包的內(nèi)存。
3. 調(diào)用ajp_marshal_into_msgb函數(shù),將HTTP請求包轉(zhuǎn)化為AJP包。
4. 將當(dāng)前ajp_worker的狀態(tài)更新。
5. 調(diào)用ajp_send_request函數(shù)。
6. 如果發(fā)送成功,調(diào)用ajp_get_reply函數(shù),等待并接受AJP包。
7. 如果成功,更新當(dāng)前狀態(tài)。
3.4.2 ajp_worker的ajp_send_request函數(shù)
它的處理過程如下:
1. 調(diào)用jk_is_socket_connected函數(shù),檢查即將發(fā)送的socket是否有效。它的檢查過
程是運(yùn)用select函數(shù),等1微妙,如果成功返回說明,該socket的文件描述符至少
在內(nèi)核中還有效。
2. 如果上一次發(fā)送超時(shí),則調(diào)用ajp_handle_cping_cpong函數(shù)來判斷后端連接是否有
效。該函數(shù)主要是發(fā)出了一個(gè)AJP13_CPING_REQUEST類型的AJP包,如果tomcat收
到,應(yīng)返回一個(gè)AJP13_CPONG_REPLY的包。
3. 如果上述的測試都成功,調(diào)用ajp_connection_tcp_send_message,發(fā)送AJP包。
3.4.3 ajp_worker的ajp_get_reply函數(shù)
它的處理過程如下:
1. 調(diào)用jk_is_input_event,用select等待接受數(shù)據(jù),直到socket有接受數(shù)據(jù)。
2. 調(diào)用ajp_connection_tcp_get_message,接受AJP包,并檢查協(xié)議頭。
3. 調(diào)用ajp_process_callback,對該AJP包進(jìn)行解析處理。其處理過程如下:
i. 如果是JK_AJP13_SEND_HEADERS包,將其解包成HTTP包頭。如果沒有錯(cuò)誤,就調(diào)
用jk_ws_service->start_response()函數(shù)發(fā)送HTTP回復(fù)的head。返回
JK_AJP13_SEND_HEADERS。
ii.如果是JK_AJP13_SEND_BODY_CHUNK包,調(diào)用jk_ws_service->write發(fā)送HTTP回
復(fù)的body。返回JK_AJP13_NO_RESPONSE。
iii.如果是JK_AJP13_GET_BODY_CHUNK包,說明還有數(shù)據(jù)要發(fā)送到tomcat,調(diào)用
ajp_read_into_msg_buff,繼續(xù)發(fā)送數(shù)據(jù)。返回JK_AJP13_HAS_RESPONSE。
iv.如果是JK_AJP13_END_RESPONSE包,就說明tomcat的回復(fù)已經(jīng)完成。返回
JK_AJP13_END_RESPONSE。
3.5 jk_ws_service的一些處理函數(shù)
3.5.1 jk_ws_service 的 ws_start_response函數(shù)
該函數(shù)主要是構(gòu)建HTTP回復(fù)包的頭部。
3.5.2 jk_ws_service 的 ws_write函數(shù)
調(diào)用 apache的ap_rwrite函數(shù),發(fā)送整個(gè)HTTP包。
3.5.3 jk_ws_service 的 ws_read函數(shù)
調(diào)用apache的ap_get_client_block讀入全部的request body數(shù)據(jù)。
4 mod_jk的主要數(shù)據(jù)結(jié)構(gòu)
4.1 lb_worker
lb_worker是負(fù)載均衡worker。主要負(fù)責(zé)調(diào)配其名下的一些lb_sub_worker 。
- dist/common/jk_lb_worker.h
- struct lb_worker
- {
- /* jk模塊通用worker結(jié)構(gòu)體。包含一些回調(diào)函數(shù),不同的worker有不同的函數(shù)實(shí)現(xiàn)
- */
- jk_worker_t worker;
- /* Shared memory worker data */
- jk_shm_lb_worker_t *s;
- char name[JK_SHM_STR_SIZ+1];
- /* Sequence counter starting at 0 and increasing
- * every time we change the config
- */
- volatile unsigned int sequence;
- jk_pool_t p;
- jk_pool_atom_t buf[TINY_POOL_SIZE];
- JK_CRIT_SEC cs;
- /*其名下的workers*/
- lb_sub_worker_t *lb_workers;
- unsigned int num_of_workers;
- int sticky_session;
- int sticky_session_force;
- int recover_wait_time;
- int max_reply_timeouts;
- int retries;
- int retry_interval;
- int lbmethod;
- int lblock;
- int maintain_time;
- unsigned int max_packet_size;
- unsigned int next_offset;
- /* Session cookie */
- char session_cookie[JK_SHM_STR_SIZ+1];
- /* Session path */
- char session_path[JK_SHM_STR_SIZ+1];
- };
- lb_sub_worker是由lb_worker支配的ajp_worker。當(dāng)然lb_sub_worker有不同的類型。
- 它們的實(shí)際類型存儲在ajp_worker中。
- dist/common/jk_lb_worker.h
- struct lb_sub_worker
- {
- /* 包含ajp_worker的回調(diào)函數(shù)及其ajp_worker結(jié)構(gòu)體*/
- jk_worker_t *worker;
- /* Shared memory worker data */
- jk_shm_lb_sub_worker_t *s;
- char name[JK_SHM_STR_SIZ+1];
- /* Sequence counter starting at 0 and increasing
- * every time we change the config
- */
- volatile unsigned int sequence;
- /* route */
- char route[JK_SHM_STR_SIZ+1];
- /* worker domain */
- char domain[JK_SHM_STR_SIZ+1];
- /* worker redirect route */
- char redirect[JK_SHM_STR_SIZ+1];
- /* worker distance */
- int distance;
- /* current activation state (config) of the worker */
- int activation;
- /* Current lb factor */
- int lb_factor;
- /* Current worker index */
- int i;
- /* Current lb reciprocal factor */
- jk_uint64_t lb_mult;
- };
- typedef struct lb_sub_worker lb_sub_worker_t;
- 4.2 ajp_worker
- ajp_worker是具體工作的worker,對應(yīng)后端一個(gè)tomcat應(yīng)用服務(wù)。
- dist/common/jk_ajp_common.c
- struct ajp_worker
- {
- /* 包含ajp_worker的回調(diào)函數(shù)*/
- jk_worker_t worker;
- /* Shared memory worker data */
- jk_shm_ajp_worker_t *s;
- char name[JK_SHM_STR_SIZ+1];
- /* Sequence counter starting at 0 and increasing
- * every time we change the config
- */
- volatile unsigned int sequence;
- jk_pool_t p;
- jk_pool_atom_t buf[TINY_POOL_SIZE];
- JK_CRIT_SEC cs;
- struct sockaddr_in worker_inet_addr; /* Contains host and port */
- unsigned connect_retry_attempts;
- const char *host;
- int port;
- int maintain_time;
- /*
- * Open connections cache...
- *
- * 1. Critical section object to protect the cache.
- * 2. Cache size.
- * 3. An array of "open" endpoints.
- */
- unsigned int ep_cache_sz;
- unsigned int ep_mincache_sz;
- unsigned int ep_maxcache_sz;
- int cache_acquire_timeout;
- ajp_endpoint_t **ep_cache;
- int proto; /* PROTOCOL USED AJP13/AJP14 */
- jk_login_service_t *login;
- /* Weak secret similar with ajp12, used in ajp13 */
- const char *secret;
- /*
- * Post physical connect handler.
- * AJP14 will set here its login handler
- */
- int (*logon) (ajp_endpoint_t * ae, jk_logger_t *l);
- /*
- * Handle Socket Timeouts
- */
- int socket_timeout;
- int socket_connect_timeout;
- int keepalive;
- int socket_buf;
- /*
- * Handle Cache Timeouts
- */
- int cache_timeout;
- /*
- * Handle Connection/Reply Timeouts
- */
- int connect_timeout; /* connect cping/cpong delay in ms (0 means
- disabled) */
- int reply_timeout; /* reply timeout delay in ms (0 means disabled) */
- int prepost_timeout; /* before sending a request cping/cpong timeout
- delay in ms (0 means disabled) */
- int conn_ping_interval; /* interval for sending keepalive cping packets on
- * unused connection */
- int ping_timeout; /* generic cping/cpong timeout. Used for keepalive
- packets or
- * as default for boolean valued connect and
- prepost timeouts.
- */
- unsigned int ping_mode; /* Ping mode flags (which types of cpings should
- be used) */
- /*
- * Recovery options
- */
- unsigned int recovery_opts;
- /*
- * Public property to enable the number of retry attempts
- * on this worker.
- */
- int retries;
- unsigned int max_packet_size; /* Maximum AJP Packet size */
- int retry_interval; /* Number of milliseconds to sleep before
- doing a retry */
- /*
- * HTTP status that will cause failover (0 means disabled)
- */
- unsigned int http_status_fail_num;
- int http_status_fail[JK_MAX_HTTP_STATUS_FAILS];
- };
- 4.3 status_worker
- status_worker用于產(chǎn)生一些狀態(tài)的統(tǒng)計(jì)信息。
- dist/common/jk_statuc.c
- struct status_worker
- {
- jk_pool_t p;
- jk_pool_atom_t buf[TINY_POOL_SIZE];
- const char *name;
- const char *css;
- const char *ns;
- const char *xmlns;
- const char *doctype;
- const char *prefix;
- int read_only;
- char **user_names;
- unsigned int num_of_users;
- int user_case_insensitive;
- jk_uint32_t good_mask;
- jk_uint32_t bad_mask;
- jk_worker_t worker;
- jk_worker_env_t *we;
- };
- 4.4 jk_worker和 jk_endpoint
- jk_worker是所有worker通用結(jié)構(gòu)體名稱, 主要包含的是一些函數(shù)指針。它是一個(gè)類
- 似java中抽象類的概念,各成員函數(shù)在從factory函數(shù)生產(chǎn)時(shí)初始化。
- dist/common/jk_service.h
- struct jk_worker
- {
- /*
- * A 'this' pointer which is used by the subclasses of this class to
- * point to data/functions which are specific to a given protocol
- * (e.g. ajp12 or ajp13 or ajp14).
- */
- void *worker_private;/* 指向ajp_worker,lb_worker結(jié)構(gòu)體 ...*/
- int type;
- /*
- * For all of the below (except destroy), the first argument is
- * essentially a 'this' pointer.
- */
- /* 先于init函數(shù)調(diào)用,用于分配各類worker結(jié)構(gòu)體的內(nèi)存,作必要的初始化
- * Given a worker which is in the process of being created, and a list
- * of configuration options (or 'properties'), check to see if it the
- * options are. This will always be called before the init() method.
- * The init/validate distinction is a bit hazy to me.
- * See jk_ajp13_worker.c/jk_ajp14_worker.c and jk_worker.c-
- >wc_create_worker()
- */
- int (JK_METHOD * validate) (jk_worker_t *w,
- jk_map_t *props,
- jk_worker_env_t *we, jk_logger_t *l);
- /*
- * Update worker either from jk_status or reloading from workers.properties
- */
- int (JK_METHOD * update) (jk_worker_t *w,
- jk_map_t *props,
- jk_worker_env_t *we, jk_logger_t *l);
- /*
- * Do whatever initialization needs to be done to start this worker up.
- * Configuration options are passed in via the props parameter.
- */
- int (JK_METHOD * init) (jk_worker_t *w,
- jk_map_t *props,
- jk_worker_env_t *we, jk_logger_t *l);
- /*
- * Obtain an endpoint to service a particular request. A pointer to
- * the endpoint is stored in pend.
- */
- int (JK_METHOD * get_endpoint) (jk_worker_t *w,
- jk_endpoint_t **pend, jk_logger_t *l);
- /*
- * Shutdown this worker. The first argument is not a 'this' pointer,
- * but rather a pointer to 'this', so that the object can be free'd (I
- * think -- though that doesn't seem to be happening. Hmmm).
- */
- int (JK_METHOD * destroy) (jk_worker_t **w, jk_logger_t *l);
- /*
- * Maintain this worker.
- */
- int (JK_METHOD * maintain) (jk_worker_t *w, time_t now, jk_logger_t *l);
- };
- /* jk_endpoint可以稱之為通用終端服務(wù)結(jié)構(gòu)體,各類worker可能有自己的endpoint結(jié)構(gòu)
- 體。比如ajp_worker, 該終端是用來做具體工作的,比如發(fā)出請求,接收AJP數(shù)據(jù)等等。如
- 果是lb_worker,該終端是的作用是找出合適ajp_worker, 把任務(wù)交給它。* /
- struct jk_endpoint
- {
- jk_uint64_t rd;
- jk_uint64_t wr;
- /*
- * Flag to pass back recoverability status from
- * a load balancer member to the load balancer itself.
- * Depending on the configuration and request status
- * recovery is not allowed.
- */
- int recoverable;
- /*
- * A 'this' pointer which is used by the subclasses of this class to
- * point to data/functions which are specific to a given protocol
- * (e.g. ajp12 or ajp13 or ajp14).
- */
- void *endpoint_private;
- /* 該函數(shù)是具體的服務(wù)函數(shù)
- * Forward a request to the servlet engine. The request is described
- * by the jk_ws_service_t object.
- * is_error is either 0 meaning recoverable or set to
- * the HTTP error code.
- */
- int (JK_METHOD * service) (jk_endpoint_t *e,
- jk_ws_service_t *s,
- jk_logger_t *l, int *is_error);
- /*
- * Called when this particular endpoint has finished processing a
- * request. For some protocols (e.g. ajp12), this frees the memory
- * associated with the endpoint. For others (e.g. ajp13/ajp14), this can
- * return the endpoint to a cache of already opened endpoints.
- *
- * Note that the first argument is *not* a 'this' pointer, but is
- * rather a pointer to a 'this' pointer. This is necessary, because
- * we may need to free this object.
- */
- int (JK_METHOD * done) (jk_endpoint_t **p, jk_logger_t *l);
- };
- 4.5 jk_ws_service
- 該結(jié)構(gòu)體是與apache相關(guān)的一些參數(shù)或函數(shù),面向HTTP協(xié)議,與AJP協(xié)議不太相關(guān)。
- dist/common/jk_service.h
- struct jk_ws_service
- {
- /*
- * A 'this' pointer which is used by the subclasses of this class to
- * point to data which is specific to a given web server platform
- * (e.g. Apache or IIS).
- */
- void *ws_private;
- /*
- * Provides memory management. All data specific to this request is
- * allocated within this pool, which can then be reclaimed at the end
- * of the request handling cycle.
- *
- * Alive as long as the request is alive.
- */
- jk_pool_t *pool;
- /*
- * CGI Environment needed by servlets
- */
- const char *method;
- const char *protocol;
- char *req_uri;
- const char *remote_addr;
- const char *remote_host;
- const char *remote_user;
- const char *auth_type;
- const char *query_string;
- const char *server_name;
- unsigned server_port;
- char *server_software;
- jk_uint64_t content_length; /* 64 bit integer that represents the content */
- /* length should be 0 if unknown. */
- unsigned is_chunked; /* 1 if content length is unknown (chunked rq) */
- unsigned no_more_chunks; /* 1 if last chunk has been read */
- jk_uint64_t content_read; /* number of bytes read */
- /*
- * SSL information
- *
- * is_ssl - True if request is in ssl connection
- * ssl_cert - If available, base64 ASN.1 encoded client certificates.
- * ssl_cert_len - Length of ssl_cert, 0 if certificates are not available.
- * ssl_cipher - The ssl cipher suite in use.
- * ssl_session - The ssl session string
- *
- * In some servers it is impossible to extract all this information, in this
- * case, we are passing NULL.
- */
- int is_ssl;
- char *ssl_cert;
- unsigned ssl_cert_len;
- char *ssl_cipher;
- char *ssl_session;
- /*
- * SSL extra information for Servlet 2.3 API
- *
- * ssl_key_size - ssl key size in use
- */
- int ssl_key_size;
- /*
- * Headers, names and values.
- */
- char **headers_names; /* Names of the request headers */
- char **headers_values; /* Values of the request headers */
- unsigned num_headers; /* Number of request headers */
- /*
- * Request attributes.
- *
- * These attributes that were extracted from the web server and are
- * sent to Tomcat.
- *
- * The developer should be able to read them from the ServletRequest
- * attributes. Tomcat is required to append org.apache.tomcat. to
- * these attribute names.
- */
- char **attributes_names; /* Names of the request attributes */
- char **attributes_values; /* Values of the request attributes */
- unsigned num_attributes; /* Number of request attributes */
- /*
- * The route is in use when the adapter load balance among
- * several workers. It is the ID of a specific target in the load balance
- * group. We are using this variable to implement target session
- * affinity
- */
- const char *route;
- /* Temp solution for auth. For native1 it'll be sent on each request,
- if an option is present. For native2 it'll be sent with the first
- request. On java side, both cases will work. For tomcat3.2 or
- a version that doesn't support secret - don't set the secret,
- and it'll work.
- */
- const char *secret;
- /*
- * Area to get POST data for fail-over recovery in POST
- */
- jk_msg_buf_t *reco_buf;
- int reco_status;
- /*
- * If set call flush after each write
- */
- int flush_packets;
- /*
- * If set call flush after AJP13_SEND_HEADERS.
- */
- int flush_header;
- /*
- * service extensions
- */
- svc_extension_t extension;
- /*
- * JK_TRUE if response headers have been sent back
- */
- int response_started;
- /*
- * JK_TRUE if response should not be send to the client
- */
- int response_blocked;
- /*
- * HTTP status sent from container.
- */
- int http_response_status;
- /* Uri worker map. Added for virtual host support
- */
- jk_uri_worker_map_t *uw_map;
- /* 下面這些回調(diào)函數(shù)實(shí)現(xiàn)都可以在mod_jk.c找到
- * Callbacks into the web server. For each, the first argument is
- * essentially a 'this' pointer. All return JK_TRUE on success
- * and JK_FALSE on failure.
- */
- /*
- * Send the response headers to the browser.
- */
- int (JK_METHOD * start_response) (jk_ws_service_t *s,
- int status,
- const char *reason,
- const char *const *header_names,
- const char *const *header_values,
- unsigned num_of_headers);
- /*
- * Read a chunk of the request body into a buffer. Attempt to read len
- * bytes into the buffer. Write the number of bytes actually read into
- * actually_read.
- */
- int (JK_METHOD * read) (jk_ws_service_t *s,
- void *buffer,
- unsigned len, unsigned *actually_read);
- /*
- * Write a chunk of response data back to the browser.
- */
- int (JK_METHOD * write) (jk_ws_service_t *s,
- const void *buffer, unsigned len);
- /*
- * Flush a chunk of response data back to the browser.
- */
- void (JK_METHOD * flush) (jk_ws_service_t *s);
- /*
- * Done with sending response back to the browser.
- */
- void (JK_METHOD * done) (jk_ws_service_t *s);
- /*
- * If set do not reuse socket after each full response
- */
- int disable_reuse;
- /*
- * Add more data to log facilities.
- */
- void (JK_METHOD * add_log_items) (jk_ws_service_t *s,
- const char *const *log_names,
- const char *const *log_values,
- unsigned num_of_items);
- /*
- * Iterate through all vhosts
- */
- void *(JK_METHOD * next_vhost) (void *d);
- /*
- * String representation of a vhost
- */
- void (JK_METHOD * vhost_to_text) (void *d, char *buf, size_t len);
- /*
- * Get uw_map associated with a vhost
- */
- jk_uri_worker_map_t *(JK_METHOD * vhost_to_uw_map) (void *d);
- };
- 4.6 共享內(nèi)存中一些數(shù)據(jù)結(jié)構(gòu)
- 共享內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)基本上都是記錄之用。有些參數(shù)在運(yùn)行中會實(shí)時(shí)變化。
- dist/common/jk_shm.h
- /** jk shm generic worker record structure */
- struct jk_shm_worker_header
- {
- int id;
- int type;
- /* worker name */
- char name[JK_SHM_STR_SIZ+1];
- /* Sequence counter starting at 0 and increasing
- * every time we change the config
- */
- volatile unsigned int sequence;
- };
- typedef struct jk_shm_worker_header jk_shm_worker_header_t;
- /** jk shm ajp13/ajp14 worker record structure */
- struct jk_shm_ajp_worker
- {
- jk_shm_worker_header_t h;
- /* Configuration data mirrored from ajp_worker */
- int cache_timeout;
- int connect_timeout;
- int reply_timeout;
- int prepost_timeout;
- unsigned int recovery_opts;
- int retries;
- int retry_interval;
- unsigned int max_packet_size;
- /* current error state (runtime) of the worker */
- volatile int state;
- /* Statistical data */
- /* Number of currently busy channels */
- volatile int busy;
- /* Maximum number of busy channels
- 該參數(shù)并非我們的maxbusy,它是該worker自程序運(yùn)行以來busy的最大值 */
- volatile int max_busy;
- volatile time_t error_time;
- /* Number of bytes read from remote */
- volatile jk_uint64_t readed;
- /* Number of bytes transferred to remote */
- volatile jk_uint64_t transferred;
- /* Number of times the worker was used */
- volatile jk_uint64_t used;
- /* Number of times the worker was used - snapshot during maintenance */
- volatile jk_uint64_t used_snapshot;
- /* Number of non 200 responses */
- volatile jk_uint32_t errors;
- /* Decayed number of reply_timeout errors */
- volatile jk_uint32_t reply_timeouts;
- /* Number of client errors */
- volatile jk_uint32_t client_errors;
- /* Last reset time */
- volatile time_t last_reset;
- volatile time_t last_maintain_time;
- };
- typedef struct jk_shm_ajp_worker jk_shm_ajp_worker_t;
- /** jk shm lb sub worker record structure */
- struct jk_shm_lb_sub_worker
- {
- jk_shm_worker_header_t h;
- /* route */
- char route[JK_SHM_STR_SIZ+1];
- /* worker domain */
- char domain[JK_SHM_STR_SIZ+1];
- /* worker redirect route */
- char redirect[JK_SHM_STR_SIZ+1];
- /* Number of currently busy channels */
- volatile int busy;
- /* worker distance */
- volatile int distance;
- /* current activation state (config) of the worker */
- volatile int activation;
- /* current error state (runtime) of the worker */
- volatile int state;
- /* Current lb factor */
- volatile int lb_factor;
- /* 我們的參數(shù)加在這里*/
- volatile int maxbusy;
- /* Current lb reciprocal factor */
- volatile jk_uint64_t lb_mult;
- /* Current lb value */
- volatile jk_uint64_t lb_value;
- /* Statistical data */
- volatile time_t error_time;
- /* Number of times the worker was elected - snapshot during maintenance */
- volatile jk_uint64_t elected_snapshot;
- /* Number of non 200 responses */
- volatile jk_uint32_t errors;
- };
- typedef struct jk_shm_lb_sub_worker jk_shm_lb_sub_worker_t;
- /** jk shm lb worker record structure */
- struct jk_shm_lb_worker
- {
- jk_shm_worker_header_t h;
- /* Number of currently busy channels,該值是其名下ajp_worker的busy值之和*/
- volatile int busy;
- /* Maximum number of busy channels,該值是其名下ajp_worker的max_busy值之和
- */
- volatile int max_busy;
- int sticky_session;
- int sticky_session_force;
- int recover_wait_time;
- int max_reply_timeouts;
- int retries;
- int retry_interval;
- int lbmethod;
- int lblock;
- unsigned int max_packet_size;
- /* Last reset time */
- volatile time_t last_reset;
- volatile time_t last_maintain_time;
- /* Session cookie */
- char session_cookie[JK_SHM_STR_SIZ+1];
- /* Session path */
- char session_path[JK_SHM_STR_SIZ+1];
- };
- typedef struct jk_shm_lb_worker jk_shm_lb_worker_t;
轉(zhuǎn)自:http://blog.csdn.net/platformer/article/details/7175054
眼鏡蛇
posted on 2013-11-01 18:04 眼鏡蛇 閱讀(2835) 評論(0) 編輯 收藏 所屬分類: Java 、Apache 、mod_jk