我们知道,AAPanel反代功能非常方便,那么在HestiaCP 如何设置反向代理呢?

从官方文档能看到,HestiaCP 支持创建自定义Web 模板。

Web Templates and FastCGI/Proxy Cache | Hestia Control Panel
Open-source web server control panel.

创建模板

首先到服务器的这个路径

/usr/local/hestia/data/templates/web/nginx

因为它们会被更新覆盖,所以要复制 default.tpl 和 default.stpl ,复制之后的文件要记得重命名成自己看的懂的。

比如,我想给运行在32000端口的qbittorrent-nox添加反代,我可以重命名成 qb.tpl 和 qb.stpl。其中,.tpl 文件用于 http,而 .stpl 用于 https。

注意要让新文件保持在从“/usr/local/hestia/data/templates/web/nginx”复制的相同路径中。

修改模板内容

我这里分别提供了两份配置文件,自行参考配置就好。

tpl

修改解释:

  • proxy_pass 设置为 http://127.0.0.1:32000 以反向代理到本地运行的 qBittorrent Nox 服务。
  • 添加了 proxy_set_header 相关指令,以确保请求头在代理过程中被正确转发。
#=========================================================================
# Default Web Domain Template
# DO NOT MODIFY THIS FILE! CHANGES WILL BE LOST WHEN REBUILDING DOMAINS
# https://hestiacp.com/docs/server-administration/web-templates.html
#=========================================================================

server {
	listen      %ip%:%proxy_port%;
	server_name %domain_idn% %alias_idn%;
	error_log   /var/log/%web_system%/domains/%domain%.error.log error;

	include %home%/%user%/conf/web/%domain%/nginx.forcessl.conf*;

	location ~ /\.(?!well-known\/|file) {
		deny all;
		return 404;
	}

	location / {
		proxy_pass http://127.0.0.1:32000;  # 反向代理到本地的 qBittorrent Nox 端口
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;

		location ~* ^.+\.(%proxy_extensions%)$ {
			try_files  $uri @fallback;

			root       %docroot%;
			access_log /var/log/%web_system%/domains/%domain%.log combined;
			access_log /var/log/%web_system%/domains/%domain%.bytes bytes;

			expires    max;
		}
	}

	location @fallback {
		proxy_pass http://127.0.0.1:32000;  # Fallback 到 qBittorrent
	}

	location /error/ {
		alias %home%/%user%/web/%domain%/document_errors/;
	}

	include %home%/%user%/conf/web/%domain%/nginx.conf_*;
}

stpl

修改解释:

  • proxy_pass http://127.0.0.1:32000;:将请求代理到本地 qBittorrent Nox 服务。由于 qBittorrent Nox 通常不使用 SSL,所以这里使用 http:// 而不是 https://
  • SSL 证书配置 仍然保持不变,Nginx 与客户端之间的通信将继续通过 HTTPS 进行加密,但 Nginx 与本地 qBittorrent Nox 的通信是通过 HTTP。
  • 头部设置proxy_set_header 用于确保代理请求中正确传递头部信息。
#=========================================================================
# Default Web Domain Template
# DO NOT MODIFY THIS FILE! CHANGES WILL BE LOST WHEN REBUILDING DOMAINS
# https://hestiacp.com/docs/server-administration/web-templates.html
#=========================================================================

server {
	listen      %ip%:%proxy_ssl_port% ssl;
	server_name %domain_idn% %alias_idn%;
	error_log   /var/log/%web_system%/domains/%domain%.error.log error;

	ssl_certificate     %ssl_pem%;
	ssl_certificate_key %ssl_key%;
	ssl_stapling        on;
	ssl_stapling_verify on;

	# TLS 1.3 0-RTT anti-replay
	if ($anti_replay = 307) { return 307 https://$host$request_uri; }
	if ($anti_replay = 425) { return 425; }

	include %home%/%user%/conf/web/%domain%/nginx.hsts.conf*;

	location ~ /\.(?!well-known\/|file) {
		deny all;
		return 404;
	}

	location / {
		proxy_pass http://127.0.0.1:32000;  # 反向代理到本地的 qBittorrent Nox 端口
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;

		location ~* ^.+\.(%proxy_extensions%)$ {
			try_files  $uri @fallback;

			root       %sdocroot%;
			access_log /var/log/%web_system%/domains/%domain%.log combined;
			access_log /var/log/%web_system%/domains/%domain%.bytes bytes;

			expires    max;
		}
	}

	location @fallback {
		proxy_pass http://127.0.0.1:32000;  # Fallback 到 qBittorrent
	}

	location /error/ {
		alias %home%/%user%/web/%domain%/document_errors/;
	}

	proxy_hide_header Upgrade;

	include %home%/%user%/conf/web/%domain%/nginx.ssl.conf_*;
}

应用更改

在 WEB 选项卡中选择编辑域设置,找到最下面的高级选项。在代理模板中选择刚刚添加的新模板,然后应用。

如果要申请证书的话,就在这个选项卡里勾选上启用SSL就行。

包含的标签:

教程, HestiaCP

最后更新: September 20, 2024