NGINX 集成§
Unit 本身就是一个强大且多功能的服务器。但是,如果您习惯了 NGINX 丰富的功能集,则可以将其部署在 Unit 之前;此处 NGINX 的一个值得注意的用例是保护 Unit 控制套接字。
使用 NGINX 作为 Unit 的前端§
在 Unit 中配置一个侦听器
{ "127.0.0.1:8080": { "pass": "...", "forwarded": { "client_ip": "X-Forwarded-For", "source": [ "127.0.0.1" ] } } }
此处,forwarded 是可选的;它用于识别从source代理的请求的原始 IP。
在 NGINX 配置中,在http上下文中创建一个上游,将侦听器的套接字添加为服务器
http {
upstream unit_backend {
server 127.0.0.1:8080;
}
server {
location /unit/ {
proxy_pass http://unit_backend;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
一个更简洁的替代方法是在您的location中使用直接proxy_pass
http {
server {
location /unit/ {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
proxy_set_header X-Forwarded-For 指令与侦听器的client_ip选项协同工作。
安全地代理 Unit 的控制 API§
默认情况下,Unit 通过 UNIX 域套接字公开其控制 API。这些套接字不可通过网络访问,因此 API 仅限于本地。要启用安全远程访问,您可以使用 NGINX 作为反向代理。
警告
避免将未受保护的控制套接字公开到公共网络。为确保安全和认证,请使用 NGINX 或 SSH 等其他解决方案。
对 NGINX 使用此配置模板(用实际值替换ssl_certificate、ssl_certificate_key、ssl_client_certificate、allow、auth_basic_user_file 和proxy_pass中的占位符)
server {
# Configure SSL encryption
listen 443 ssl;
ssl_certificate /path/to/ssl/cert.pem;
ssl_certificate_key /path/to/ssl/cert.key;
# SSL client certificate validation
ssl_client_certificate /path/to/ca.pem;
ssl_verify_client on;
# Network ACLs
allow 1.2.3.4;
deny all;
# HTTP Basic authentication
auth_basic on;
auth_basic_user_file /path/to/htpasswd;
location / {
proxy_pass http://unix:/path/to/control.unit.sock;
}
}
相同的方法适用于基于 IP 的控制套接字
location / {
proxy_pass http://127.0.0.1:8080;
}