// 传入的是 main 函数的两个参数 argc 和 argv static ngx_int_t
ngx_get_options(int argc, char *const *argv)
{
u_char *p;
ngx_int_t i;
// 对于每一个 argv(注意是从 1 开始,因为 0 是 "nginx") for (i = 1; i < argc; i++) {
// p 为第 i 个参数的地址
p = (u_char *) argv;
// if (*p++ != '-') {
ngx_log_stderr(0, "invalid option: \"%s\"", argv); return NGX_ERROR;
}
// 之所以 while 循环是因为一个减号可以带过个参数,比如 -hV while (*p) {
// 注意 p 被加 1 switch (*p++) {
// 问号和 h 都是显示帮助信息和版本信息 case '?': case 'h':
ngx_show_version = 1;
ngx_show_help = 1; break;
// 小 v 显示版本信息 case 'v':
ngx_show_version = 1; break;
// 大 v 显示版本信息和配置信息 case 'V':
ngx_show_version = 1;
ngx_show_configure = 1; break;
// t 用于测试配置文件 case 't':
ngx_test_config = 1; break;
// q 表示安静模式 case 'q':
ngx_quiet_mode = 1; break;
// p 为指定 prefix path case 'p': if (*p) {
ngx_prefix = p; goto next;
} if (argv[++i]) {
ngx_prefix = (u_char *) argv; goto next;
}
ngx_log_stderr(0, "option \"-p\" requires directory name"); return NGX_ERROR;
// 使用指定的配置文件 case 'c': if (*p) {
ngx_conf_file = p; goto next;
} if (argv[++i]) {
ngx_conf_file = (u_char *) argv; goto next;
}
ngx_log_stderr(0, "option \"-c\" requires file name"); return NGX_ERROR;
// 在配置文件之外设置全局指令 case 'g': if (*p) {
ngx_conf_params = p; goto next;
} if (argv[++i]) {
ngx_conf_params = (u_char *) argv; goto next;
}
ngx_log_stderr(0, "option \"-g\" requires parameter"); return NGX_ERROR;
// s 为 signal,即给 Nginx 发送信号 case 's': if (*p) { // 下一个参数紧跟在 -s 后,比如 -sstop
ngx_signal = (char *) p;
} elseif (argv[++i]) { // 下一个参数
ngx_signal = argv;
} else { // -s 没有带参数时
ngx_log_stderr(0, "option \"-s\" requires parameter"); return NGX_ERROR;
}
// 四个信号分别对应:停止、退出、重新打开文件(日志文件等)、重新加载配置文件 if (ngx_strcmp(ngx_signal, "stop") == 0
|| ngx_strcmp(ngx_signal, "quit") == 0
|| ngx_strcmp(ngx_signal, "reopen") == 0
|| ngx_strcmp(ngx_signal, "reload") == 0)
{
ngx_process = NGX_PROCESS_SIGNALLER; goto next;
}
ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal); return NGX_ERROR; default:
ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1)); return NGX_ERROR;
}
}
next: continue;
} return NGX_OK;
}
帮助信息如下:
Options:
-?,-h : this help
-v : show version andexit
-V : show version and configure options thenexit
-t : test configurationandexit
-q : suppress non-error messages during configuration testing
-s signal : send signalto a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configurationfile (default: conf/nginx.conf)
-g directives : set global directives outofconfigurationfile
v 版本信息形式如下:
nginx version: nginx/1.3.5
V 版本信息如下:
nginx version: nginx/1.3.5
built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
configure arguments: --with-pcre=/home/michael/packages.d/pcre-8.20 --with-zlib=/home/michael/packages.d/zlib-1.2.7