有時(shí)為了配置方便會(huì)將不同的 nginx 服務(wù)獨(dú)立出來,在一臺(tái)機(jī)器上啟多個(gè) nginx 實(shí)例。不同的 nginx 實(shí)例支持獨(dú)立的配置文件,可以單獨(dú)啟停。比如默認(rèn)的 nginx 是通過 systemctl 進(jìn)行管理的,它默認(rèn)的配置文件是在 /etc/nginx/ 目錄。通過 nginx -V 可以查看它的編譯選項(xiàng),其中包含了默認(rèn)配置文件位置,以及默認(rèn)日志。nginx -V 2>&1| sed 's#--#\n--#g'
在我們使用 nginx 時(shí),沒有配的參數(shù)它會(huì)使用編譯參數(shù)作為默認(rèn)參數(shù)。如果想在一臺(tái)機(jī)器上運(yùn)行多個(gè)實(shí)例,需要改變它的默認(rèn)參數(shù)。通過調(diào)整兩個(gè)參數(shù),讓 nginx 以不同的身份啟動(dòng)當(dāng)前實(shí)例。調(diào)整 -p 和 -c 參數(shù)可以在一臺(tái)機(jī)器上運(yùn)行多個(gè)實(shí)例。以 API 代理為例,我們想在一臺(tái)機(jī)器上運(yùn)行多個(gè) api 代理,一個(gè)配置文件一個(gè)代理。 cat api.conf
pid /run/nginx_deepseek_api.pid;
events {
worker_connections 768;
}
http {
log_format custom '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"Authorization: $auth_status"';
map $http_authorization $auth_status {
default "401";
"Bearer 23333330-f333-133f-b336-d33333333331" "200";
}
server {
listen 51434;
server_name 192.168.1.1;
access_log /var/log/p51434_access.log custom;
error_log /var/log/p51434_error.log notice;
location / {
deny all;
return 403;
}
location = /api/generate {
proxy_pass http://127.0.0.1:11434/api/generate;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location = /api/chat {
proxy_pass http://127.0.0.1:11434/api/chat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
在配置文件目錄中啟動(dòng) nginx 實(shí)例:nginx -p $PWD -c api.conf
這樣就啟動(dòng)了一個(gè) nginx 實(shí)例,假如有多個(gè)配置文件,則可以用相同命令啟動(dòng)多個(gè)實(shí)例:nginx -p $PWD -c api.conf1
nginx -p $PWD -c api.conf2
nginx -p $PWD -c api.conf3
nginx -p $PWD -c api.conf -s reload
nginx -p $PWD -c api.conf -s stop
每個(gè)配置文件中的 pid 文件不同,選定配置文件即可控制對(duì)應(yīng)的 nginx 實(shí)例。
閱讀原文:原文鏈接
該文章在 2025/9/12 11:19:54 編輯過