有時為了配置方便會將不同的 nginx 服務(wù)獨立出來,在一臺機(jī)器上啟多個 nginx 實例。不同的 nginx 實例支持獨立的配置文件,可以單獨啟停。比如默認(rèn)的 nginx 是通過 systemctl 進(jìn)行管理的,它默認(rèn)的配置文件是在 /etc/nginx/ 目錄。通過 nginx -V 可以查看它的編譯選項,其中包含了默認(rèn)配置文件位置,以及默認(rèn)日志。nginx -V 2>&1| sed 's#--#\n--#g'
在我們使用 nginx 時,沒有配的參數(shù)它會使用編譯參數(shù)作為默認(rèn)參數(shù)。如果想在一臺機(jī)器上運(yùn)行多個實例,需要改變它的默認(rèn)參數(shù)。通過調(diào)整兩個參數(shù),讓 nginx 以不同的身份啟動當(dāng)前實例。調(diào)整 -p 和 -c 參數(shù)可以在一臺機(jī)器上運(yùn)行多個實例。以 API 代理為例,我們想在一臺機(jī)器上運(yùn)行多個 api 代理,一個配置文件一個代理。 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;
}
}
}
nginx -p $PWD -c api.conf
這樣就啟動了一個 nginx 實例,假如有多個配置文件,則可以用相同命令啟動多個實例: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
每個配置文件中的 pid 文件不同,選定配置文件即可控制對應(yīng)的 nginx 實例。
閱讀原文:原文鏈接
該文章在 2025/9/12 11:19:54 編輯過