You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Apache反向代理配置故障排查:如何通过子域名访问跨主机的Radarr与Sonarr服务

Apache反向代理配置故障排查:如何通过子域名访问跨主机的Radarr与Sonarr服务

嘿,我来帮你搞定这个问题!你遇到的“访问子域名却跳主站”的情况,大概率是配置里的几个小细节没处理好,咱们一步步来梳理和修复,也会给你讲讲如果换用更简单的Nginx该怎么做。

一、先排查当前Apache配置的核心问题

你之前只给子域名配置了80端口的反向代理,但现在浏览器默认会自动把HTTP请求跳转到HTTPS,而你的子域名没有对应的443(HTTPS)VirtualHost配置,Apache就会把这些HTTPS请求匹配到主站的443配置上,自然就显示主站了。另外还要先确认反向代理必需的模块有没有启用。

1. 启用Apache反向代理模块

首先得确保Apache加载了反向代理相关的模块,在machine1上执行以下命令:

sudo a2enmod proxy proxy_http
sudo systemctl restart apache2

2. 修正子域名的VirtualHost配置(包含HTTPS)

你需要给每个子域名分别配置80(跳转HTTPS)和443(反向代理+SSL)的VirtualHost:

Radarr的配置示例

80端口配置(用于强制跳转HTTPS)

<VirtualHost *:80>
    ServerName radarr.example.com
    # 把所有HTTP请求永久跳转到HTTPS
    Redirect permanent / https://radarr.example.com/
    ErrorLog /var/www/radarr/logs/error.log
    CustomLog /var/www/radarr/logs/access.log combined
</VirtualHost>

443端口配置(反向代理+SSL)

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName radarr.example.com

    # 保留原始请求的主机头,避免后端服务识别错误
    ProxyPreserveHost On
    # 关闭正向代理,只启用反向代理
    ProxyRequests Off

    # 把请求转发到machine2的Radarr服务端口
    ProxyPass / http://192.168.1.20:7878/
    ProxyPassReverse / http://192.168.1.20:7878/

    # 日志配置
    ErrorLog /var/www/radarr/logs/error.log
    CustomLog /var/www/radarr/logs/access.log combined

    # SSL证书(需要给子域名单独申请,下文会讲)
    SSLCertificateFile /etc/letsencrypt/live/radarr.example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/radarr.example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Sonarr的配置示例

把上面的配置里的radarr.example.com换成sonarr.example.com,端口7878换成Sonarr的默认端口8989即可。

3. 给子域名申请SSL证书

用Certbot给两个子域名申请证书,执行以下命令(会自动配置HTTPS的VirtualHost,省得手动写):

sudo certbot --apache -d radarr.example.com -d sonarr.example.com

4. 额外检查项

  • DNS解析:确保radarr.example.comsonarr.example.com都指向machine1的公网IP(和主站example.com一致)
  • 局域网连通性:确认machine1能访问machine2的7878和8989端口,比如在machine1上执行curl http://192.168.1.20:7878,如果能返回Radarr的页面就没问题;如果不行,检查machine2的防火墙是否开放了这两个端口给machine1的局域网IP。

二、如果想放弃Apache,试试更简洁的Nginx方案

Nginx的反向代理配置更直观,步骤也简单:

1. 安装Nginx和Certbot

sudo apt update && sudo apt install nginx certbot python3-certbot-nginx

2. 创建Radarr的Nginx配置文件

/etc/nginx/sites-available/下创建radarr.example.com文件:

server {
    listen 80;
    server_name radarr.example.com;
    # 跳转HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name radarr.example.com;

    # SSL证书(后续用Certbot自动配置)
    ssl_certificate /etc/letsencrypt/live/radarr.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/radarr.example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;

    location / {
        proxy_pass http://192.168.1.20:7878;
        # 传递必要的请求头给后端服务
        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;
    }

    access_log /var/log/nginx/radarr.access.log;
    error_log /var/log/nginx/radarr.error.log;
}

3. 启用配置并重启Nginx

sudo ln -s /etc/nginx/sites-available/radarr.example.com /etc/nginx/sites-enabled/
# 同样创建并启用Sonarr的配置,替换域名和端口
sudo systemctl restart nginx

4. 申请SSL证书

sudo certbot --nginx -d radarr.example.com -d sonarr.example.com

这样配置完成后,访问radarr.example.comsonarr.example.com就能直接跳转到machine2上的对应服务了。

备注:内容来源于stack exchange,提问作者Riccardo Barbieri

火山引擎 最新活动