当前位置: 首页 > 工具软件 > Boa Webserver > 使用案例 >

Boa Webserver使用和部分源码学习(凭兴趣更新)

郎献
2023-12-01

目录

第一章 为何使用Boa

第二章 Boa结构

第三章 Boa源码目录介绍

第四章 Boa部分重要源码介绍

 

一、为何使用Boa

Boa是嵌入式系统常用的轻量级webserver,是一个小巧高效的web服务器,其可执行代码只有大约60KB左右。是一个运行于unix或linux下的,支持CGI的、适合于嵌入式系统的单任务的http服务器。更重要的是源代码开放、性能高。由于它是一个单任务的Web服务器,只能一次完成用户的请求,而不会fork出新的进程来处理并发的链接请求。但是Boa支持Cgi,能够为Cgi程序fork出一个进程来执行相应的客户请求。

参考:http://www.boa.org/documentation/boa-1.html

二、Boa结构

 

参考:https://blog.csdn.net/u014530704/article/details/72822775

三、源码目录介绍

|-- apmib            <----- apmib library   apmib库
|--cgi                   <------用来验证cgi是否连接的测试代码
| --contrib        <----- boa自带吹水文件,里面记述的是吹水人员的威水事
|-- defconfig        <----- default apmib configuration 默认apmib配置
|--docs               <------boa自带
|-- example          <----boa自带,内有boa.conf,该文件是Boa的唯一配置文件。该文件中的指令在DIRECTIVES部分中定义。
|--extras              <----boa自带额外小程序,例如字符串过滤等。
|-- html             <----- HTML pages and graphics files html页面和图形文件
|-- src              <----- source code of Boa web server  Boa web服务器源代码,自带
|-- system           <----- source code for system and network init  系统和网络初始化的源代码
|-- tools            <----- tools on x86  x86上的工具
|-- utils            <----- flash utility  flash实用程序

以上根据版本有所区别,一般情况,boa.conf在根目录下,其他命名包都是厂商增加而为之。

四、Boa部分重要源码介绍

1.boa.conf

代码如下(示例):

# Boa v0.94 configuration file
# File format has not changed from 0.93
# File format has changed little from 0.92
# version changes are noted in the comments
#
# The Boa configuration file is parsed with a custom parser.  If it
# reports an error, the line number will be provided; it should be easy
# to spot.  The syntax of each of these rules is very simple, and they
# can occur in any order.  Where possible these directives mimic those
# of NCSA httpd 1.3; I saw no reason to introduce gratuitous
# differences.

# $Id: boa.conf,v 1.3.2.6 2003/02/02 05:02:22 jnelson Exp $

# The "ServerRoot" is not in this configuration file.  It can be
# compiled into the server (see defines.h) or specified on the command
# line with the -c option, for example:
#
# boa -c /usr/local/boa


# Port: The port Boa runs on.  The default port for http servers is 80.
# If it is less than 1024, the server must be started as root.

Port 80

# Listen: the Internet address to bind(2) to.  If you leave it out,
# it takes the behavior before 0.93.17.2, which is to bind to all
# addresses (INADDR_ANY).  You only get one "Listen" directive,
# if you want service on multiple IP addresses, you have three choices:
#    1. Run boa without a "Listen" directive
#       a. All addresses are treated the same; makes sense if the addresses
#          are localhost, ppp, and eth0.
#       b. Use the VirtualHost directive below to point requests to different
#          files.  Should be good for a very large number of addresses (web
#          hosting clients).
#    2. Run one copy of boa per IP address, each has its own configuration
#       with a "Listen" directive.  No big deal up to a few tens of addresses.
#       Nice separation between clients.
# The name you provide gets run through inet_aton(3), so you have to use dotted
# quad notation.  This configuration is too important to trust some DNS.

#Listen 192.68.0.5

#  User: The name or UID the server should run as.
# Group: The group name or GID the server should run as.

#User nobody
#Group nogroup
User root
Group root

# ServerAdmin: The email address where server problems should be sent.
# Note: this is not currently used, except as an environment variable
# for CGIs.

#ServerAdmin root@localhost

# PidFile: where to put the pid of the process.
# Comment out to write no pid file.
# Note: Because Boa drops privileges at startup, and the
# pid file is written by the UID/GID before doing so, Boa
# does not attempt removal of the pid file.
# PidFile /var/run/boa.pid
PidFile /var/run/webs.pid

# ErrorLog: The location of the error log file. If this does not start
# with /, it is considered relative to the server root.
# Set to /dev/null if you don't want errors logged.
# If unset, defaults to /dev/stderr
# Please NOTE: Sending the logs to a pipe ('|'), as shown below,
#  is somewhat experimental and might fail under heavy load.
# "Usual libc implementations of printf will stall the whole
#  process if the receiving end of a pipe stops reading."
#ErrorLog "|/usr/sbin/cronolog --symlink=/var/log/boa/error_log /var/log/boa/error-%Y%m%d.log"

#ErrorLog /var/log/boa/error_log

# AccessLog: The location of the access log file. If this does not
# start with /, it is considered relative to the server root.
# Comment out or set to /dev/null (less effective) to disable.
# Useful to set to /dev/stdout for use with daemontools.
# Access logging.  
# Please NOTE: Sending the logs to a pipe ('|'), as shown below,
#  is somewhat experimental and might fail under heavy load.
# "Usual libc implementations of printf will stall the whole
#  process if the receiving end of a pipe stops reading."
#AccessLog  "|/usr/sbin/cronolog --symlink=/var/log/boa/access_log /var/log/boa/access-%Y%m%d.log"

#AccessLog /var/log/boa/access_log

# CGILog /var/log/boa/cgi_log
# CGILog: The location of the CGI stderr log file. If this does not
# start with /, it is considered relative to the server root.
# The log file would contain any contents send to /dev/stderr
# by the CGI. If this is commented out, it defaults to whatever 
# ErrorLog points.  Set to /dev/null to disable CGI stderr logging.
# Please NOTE: Sending the logs to a pipe ('|'), as shown below,
#  is somewhat experimental and might fail under heavy load.
# "Usual libc implementations of printf will stall the whole
#  process if the receiving end of a pipe stops reading."
#CGILog  "|/usr/sbin/cronolog --symlink=/var/log/boa/cgi_log /var/log/boa/cgi-%Y%m%d.log"

# CGIumask 027 (no mask for user, read-only for group, and nothing for user)
# CGIumask 027
# The CGIumask is set immediately before execution of the CGI.

# UseLocaltime: Logical switch.  Uncomment to use localtime 
# instead of UTC time
#UseLocaltime

# VerboseCGILogs: this is just a logical switch.
#  It simply notes the start and stop times of cgis in the error log
# Comment out to disable.

#VerboseCGILogs

# ServerName: the name of this server that should be sent back to 
# clients if different than that returned by gethostname + gethostbyname 

#ServerName www.your.org.here
#ServerName ""

# VirtualHost: a logical switch.
# Comment out to disable.
# Given DocumentRoot /var/www, requests on interface 'A' or IP 'IP-A'
# become /var/www/IP-A.
# Example: http://localhost/ becomes /var/www/127.0.0.1
#
# Not used until version 0.93.17.2.  This "feature" also breaks commonlog
# output rules, it prepends the interface number to each access_log line.
# You are expected to fix that problem with a postprocessing script.

#VirtualHost 


# VHostRoot: the root location for all virtually hosted data
# Comment out to disable.
# Incompatible with 'Virtualhost' and 'DocumentRoot'!!
# Given VHostRoot /var/www, requests to host foo.bar.com,
# where foo.bar.com is ip a.b.c.d,
# become /var/www/a.b.c.d/foo.bar.com 
# Hostnames are "cleaned", and must conform to the rules
# specified in rfc1034, which are be summarized here:
# 
# Hostnames must start with a letter, end with a letter or digit, 
# and have as interior characters only letters, digits, and hyphen.
# Hostnames must not exceed 63 characters in length.

#VHostRoot /var/www

# DefaultVHost
# Define this in order to have a default hostname when the client does not
# specify one, if using VirtualHostName. If not specified, the word
# "default" will be used for compatibility with older clients.

#DefaultVHost foo.bar.com

# DocumentRoot: The root directory of the HTML documents.
# Comment out to disable server non user files.

#DocumentRoot /var/www
DocumentRoot /web

# UserDir: The name of the directory which is appended onto a user's home
# directory if a ~user request is received.

UserDir public_html

# DirectoryIndex: Name of the file to use as a pre-written HTML
# directory index.  Please MAKE AND USE THESE FILES.  On the
# fly creation of directory indexes can be _slow_.
# Comment out to always use DirectoryMaker

DirectoryIndex index.html

# DirectoryMaker: Name of program used to create a directory listing.
# Comment out to disable directory listings.  If both this and
# DirectoryIndex are commented out, accessing a directory will give
# an error (though accessing files in the directory are still ok).

#DirectoryMaker /usr/lib/boa/boa_indexer

# DirectoryCache: If DirectoryIndex doesn't exist, and DirectoryMaker
# has been commented out, the the on-the-fly indexing of Boa can be used
# to generate indexes of directories. Be warned that the output is 
# extremely minimal and can cause delays when slow disks are used.
# Note: The DirectoryCache must be writable by the same user/group that 
# Boa runs as.

#DirectoryCache /var/spool/boa/dircache
DirectoryCache /tmp

# KeepAliveMax: Number of KeepAlive requests to allow per connection
# Comment out, or set to 0 to disable keepalive processing

#KeepAliveMax 1000
KeepAliveMax 0

# KeepAliveTimeout: seconds to wait before keepalive connection times out

KeepAliveTimeout 10

# MimeTypes: This is the file that is used to generate mime type pairs
# and Content-Type fields for boa.
# Set to /dev/null if you do not want to load a mime types file.
# Do *not* comment out (better use AddType!)

#MimeTypes /etc/mime.types
MimeTypes /etc/boa/mime.types

# DefaultType: MIME type used if the file extension is unknown, or there
# is no file extension.

#DefaultType text/plain
DefaultType text/html

# CGIPath: The value of the $PATH environment variable given to CGI progs.

CGIPath /bin:/usr/bin:/web/cgi-bin/

# SinglePostLimit: The maximum allowable number of bytes in 
SinglePostLimit 0x800000 
# a single POST.  Default is normally 1MB.

# AddType: adds types without editing mime.types
# Example: AddType type extension [extension ...]

# Uncomment the next line if you want .cgi files to execute from anywhere
AddType application/x-httpd-cgi cgi
AddType application/x-httpd-cgi php

# Redirect, Alias, and ScriptAlias all have the same semantics -- they
# match the beginning of a request and take appropriate action.  Use
# Redirect for other servers, Alias for the same server, and ScriptAlias
# to enable directories for script execution.

# Redirect allows you to tell clients about documents which used to exist in
# your server's namespace, but do not anymore. This allows you to tell the
# clients where to look for the relocated document.
# Example: Redirect /bar http://elsewhere/feh/bar

# Aliases: Aliases one path to another.
# Example: Alias /path1/bar /path2/foo

# Alias /doc /usr/doc

# ScriptAlias: Maps a virtual path to a directory for serving scripts
# Example: ScriptAlias /htbin/ /www/htbin/

ScriptAlias /cgi-bin/ /web/cgi-bin/
Port <integer>
这是Boa运行的端口。http服务器的默认端口为80。如果小于1024,则必须以root用户身份启动服务器。

Listen <IP>
Listen: 绑定(2)到的Internet地址。如果不进行设置,它将采取0.93.17.2之前的行为,即绑定到所有地址(INADDR_ANY)。您仅获得一个“ Listen”指令,如果要在多个IP地址上进行服务,则有以下三种选择:
  1. 在没有“ Listen”指令的情况下运行蟒蛇
    1. 所有地址均相同;如果地址是localhost,ppp和eth0,则很有意义。
    2. 使用下面的VirtualHost指令将请求指向不同的文件。对于大量地址(网络托管客户端)来说应该是不错的选择。
  2. 每个IP地址运行一个boa副本,每个副本都有自己的配置,并带有“ Listen”指令。最多几十个地址没什么大不了的。客户之间的良好分离。您提供的名称通过inet_aton(3)运行,因此您必须使用点分四元符号。此配置太重要了,无法信任某些DNS。
User <user name or UID>
服务器应以其运行的名称或UID。为了让Boa尝试这样做,必须以root用户身份启动服务器。//不要改

Group <group name or GID>
服务器应以其运行的组名或GID。为了让Boa尝试这样做,必须以root用户身份启动服务器。//不要改

ServerAdmin <email address>
应该将服务器问题发送到的电子邮件地址。//默认不开启

ErrorLog <filename>
错误日志文件的位置。如果不是以/开头,则将其视为相对于服务器根目录。如果您不想记录错误,请设置为/ dev / null。

AccessLog <filename>
访问日志文件的位置。如果不是以/开头,则将其视为相对于服务器根目录。注释掉或设置为/ dev / null(效果较差)以禁用访问日志记录。

VerboseCGILogs
这是一个逻辑开关,不带任何参数。注释掉以禁用。它所做的只是打开或关闭CGI启动时间以及子级返回时间的日志记录。//默认不开启

CgiLog <filename>
CGI错误日志文件的位置。如果指定,这是CGI的stderr所绑定的文件。否则,写入stderr会遇到位桶。

ServerName <server_name>
如果该服务器的名称与gethostname返回的名称不同,则应将其发送回客户端。

VirtualHost
这是一个逻辑开关,不带任何参数。注释掉以禁用。给定DocumentRoot / var / www,接口“ A”或IP“ IP-A”上的请求将变为/ var / www / IP-A。示例:http:// localhost /变为/var/www/127.0.0.1   //一般也注释掉

DocumentRoot <directory>
HTML文档的根目录。如果不是以/开头,则将其视为相对于服务器根目录。//就是说把我们的html丢到这个目录上

UserDir <directory>
如果收到用户请求,则将目录的名称附加到用户的主目录中。

DirectoryIndex <filename>
用作预写HTML目录索引的文件名。请制作并使用这些文件。动态创建目录索引可能很慢。

DirectoryMaker <full pathname to program>
用于生成即时目录列表的程序的名称。该程序必须带有一个或两个命令行参数,第一个是要索引的目录(绝对),第二个是可选的,应为文档的“标题”。如果您不想要即时目录列表,请注释掉。如果不是以/开头,则将其视为相对于服务器根目录。//不知道啥玩意,不敢动

DirectoryCache <directory>
DirectoryCache:如果DirectoryIndex不存在,并且DirectoryMaker已被注释掉,则Boa的动态索引可用于生成目录索引。警告:使用极慢的磁盘时,输出极小,可能会导致延迟。注意:DirectoryCache必须可由Boa运行的同一用户/组写入。

KeepAliveMax <integer>
每个连接允许的KeepAlive请求数。注释掉,或设置为0以禁用keepalive处理。//默认1000,够大了吧

KeepAliveTimeout <integer>
Keepalive连接超时之前要等待的秒数。

MimeTypes <file>
mime.types文件的位置。如果不是以/开头,则将其视为相对于服务器根目录。注释掉以避免加载mime.types(最好使用AddType!)

DefaultType <mime type>
如果文件扩展名未知或没有文件扩展名,则使用MIME类型。

AddType <mime type> <extension> extension...
将MIME类型与一个或多个扩展名相关联。

Redirect, Alias, and ScriptAlias <path1> <path2>
重定向,别名和ScriptAlias都具有相同的语义-它们匹配请求的开头并采取适当的措施。将重定向用于其他服务器,将别名用于同一服务器,并将ScriptAlias用于启用目录以执行脚本。

Redirect
允许您告诉客户端有关曾经存在于服务器命名空间中的文档,但现在不再存在。这使您可以告诉客户端在何处查找已重定位的文档。

Alias
别名一条路径到另一条。当然,文件系统中的符号链接也可以正常工作。
ScriptAlias
将虚拟路径映射到用于提供脚本的目录
参考:boa.org/documentation/boa-2.html

 

 类似资料: