如何在一台服务器上运行多个 nginx 实例
|
admin
2025年9月11日 20:26
本文热度 102
|
有时为了配置方便会将不同的 nginx 服务独立出来,在一台机器上启多个 nginx 实例。不同的 nginx 实例支持独立的配置文件,可以单独启停。比如默认的 nginx 是通过 systemctl 进行管理的,它默认的配置文件是在 /etc/nginx/ 目录。通过 nginx -V 可以查看它的编译选项,其中包含了默认配置文件位置,以及默认日志。nginx -V 2>&1| sed 's#--#\n--#g'
在我们使用 nginx 时,没有配的参数它会使用编译参数作为默认参数。如果想在一台机器上运行多个实例,需要改变它的默认参数。通过调整两个参数,让 nginx 以不同的身份启动当前实例。调整 -p 和 -c 参数可以在一台机器上运行多个实例。以 API 代理为例,我们想在一台机器上运行多个 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 文件不同,选定配置文件即可控制对应的 nginx 实例。
阅读原文:原文链接
该文章在 2025/9/12 11:19:54 编辑过