nginx學習筆記 main模板

2021-09-10 18:13:22 字數 3674 閱讀 3563

nginx在unix系統中能設定程序標題,這是乙個很酷炫的功能,看看是怎麼實現的呢

#ifdef __unix__

/* * memory layout

* ar**[0]\0ar**[1]\0ar**[n]\0env[0]\0env[1]\0env[n]\0

*/void setproctitle(const char* title)

#endif

// unix short style

static char options = "hvc:ts:d";

static char detail_options = "\

-h : print help\n\

-v : print version\n\

-c confile : set configure file, default etc/$.conf\n\

-t : test configure file and exit\n\

-s signal : send signal to process\n\

signal=[start, stop, restart, status]\n\

-d : daemon\n\

";void print_version()

void print_help()

#define invalid_option -1

#define flag_option 1

#define parma_option 2

int get_option(char opt)

int parse_cmdline(int argc, char** ar**)

while (*++p) else if (ar**[i+1] != null) else }}

++i;

next_option:

continue;

}return 0;

}

int create_pidfile() 

char pid[16] = ;

snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);

fwrite(pid, 1, strlen(pid), fp);

fclose(fp); atexit(delete_pidfile);

hlogd("create_pidfile [%s] pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);

return 0;

}void delete_pidfile()

pid_t getpid_from_pidfile()

char pid[64];

int readbytes = fread(pid, 1, sizeof(pid), fp);

fclose(fp);

if (readbytes <= 0)

return atoi(pid);

}

在unix系統中使用signal,在windows系統中使用event

const char* signal = get_arg("s");

if (signal)

} else if (strcmp(signal, "stop") == 0) else

exit(0);

} else if (strcmp(signal, "restart") == 0)

} else if (strcmp(signal, "status") == 0) else

exit(0);

} else

printf("%s start/running\n", g_main_ctx.program_name);

}

#ifdef __unix__

// unix use signal

// we use sigterm to quit process

#define signal_terminate sigterm

#include #define maxnum_worker 1024

static pid_t s_worker_processes[maxnum_worker];

void worker_process_cycle() ;

snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);

setproctitle(proctitle);

while(1)

}int create_worker_process(int worker_processes)

if (pid == 0)

for (int i = 0; i < maxnum_worker; ++i) }}

return 0;

}static int s_signo = 0;

void master_process_signal_handler(int signo)

void master_process_init()

}void master_process_cycle() ;

snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);

setproctitle(proctitle);

signal(sigint, master_process_signal_handler);

signal(sigchld, master_process_signal_handler);

signal(signal_terminate, master_process_signal_handler);

sigset_t sigset;

sigemptyset(&sigset);

sigaddset(&sigset, sigint);

sigaddset(&sigset, sigchld);

sigaddset(&sigset, signal_terminate);

sigprocmask(sig_block, &sigset, null);

create_worker_process(get_ncpu());

sigemptyset(&sigset);

while (1)

msleep(1000);

exit(0);

break;

case sigchld:

}create_worker_process(1);}}

break;

default:

break;}}

}#endif

見下的main.cpp.tmpl檔案

repo中提供了makefile檔案

編譯步驟如下:

make test

bin/test -d

ps aux | grep test

Nginx 學習筆記

nginx配置proxy pass 的 路徑問題 在nginx中配置proxy pass時,如果是按照 匹配路徑時,要注意proxy pass後的url最後的 當加上了 相當於是絕對根路徑,則nginx不會把location中匹配的路徑部分 走 如果沒有 則會把匹配的路徑部分也給 走。locatio...

Nginx學習筆記

常用命令 啟動 start nginx 或者 nginx.exe 停止 nginx.exe s stop 或者 nginx.exe s quit stop是快速停止nginx,quit是完整有序的停止nginx 重啟 nginx.exe s reload 配置資訊修改使用此命令 配置 1 匹配以ro...

nginx學習筆記

mac os x 10.11.5環境 brew install nginx 即可。預設安裝目錄 usr local etc nginx。可以修改目錄下nginx.conf檔案的配置,將8080改為8081,則通過http localhost 8081訪問就可以看到nginx的歡迎介面了。主程序可以處...