NetAlertX项目反向代理配置全指南

NetAlertX项目反向代理配置全指南

NetAlertX 💻🔍 WIFI / LAN intruder detector. Scans for devices connected to your network and alerts you if new and unknown devices are found. NetAlertX 项目地址: https://gitcode.com/gh_mirrors/ne/NetAlertX

前言

NetAlertX是一款优秀的网络设备监控工具,在实际部署中经常需要通过反向代理来提供更灵活的访问方式。本文将全面介绍如何为NetAlertX配置反向代理,涵盖NGINX、Apache以及容器化环境下的配置方案。

基础概念

在开始配置前,我们需要了解几个关键概念:

  1. 反向代理:作为客户端和服务器之间的中介,接收客户端请求并转发给后端服务器
  2. 直接路径访问:通过根路径(如http://example.com/)直接访问应用
  3. 子路径访问:通过特定路径(如http://example.com/netalertx/)访问应用

NGINX配置方案

HTTP直接路径配置

  1. 创建配置文件/etc/nginx/sites-available/netalertx
  2. 添加以下内容:
server {
  listen 80;
  server_name netalertx;
  proxy_preserve_host on;
  proxy_pass http://localhost:20211/;
  proxy_pass_reverse http://localhost:20211/;
}
  1. 重新加载NGINX配置:
    nginx -s reload
    
    systemctl restart nginx
    

HTTP子路径配置

对于需要通过子路径访问的场景:

server {
  listen 80;
  server_name netalertx;
  proxy_preserve_host on;
  location ^~ /netalertx/ {
    proxy_pass http://localhost:20211/;
    proxy_pass_reverse http://localhost:20211/;
    proxy_redirect ~^/(.*)$ /netalertx/$1;
    rewrite ^/netalertx/?(.*)$ /$1 break;
  }
}

使用ngx_http_sub_module模块

当应用中有硬编码路径时,需要使用内容替换:

location ^~ /netalertx/ {
  # 基础代理配置
  proxy_pass http://localhost:20211/;
  proxy_pass_reverse http://localhost:20211/;
  proxy_redirect ~^/(.*)$ /netalertx/$1;
  rewrite ^/netalertx/?(.*)$ /$1 break;
  
  # 内容替换配置
  sub_filter_once off;
  sub_filter_types *;
  sub_filter 'href="/' 'href="/netalertx/';
  sub_filter '(?>$host)/css' '/netalertx/css';
  sub_filter '(?>$host)/js' '/netalertx/js';
  sub_filter '/img' '/netalertx/img';
  sub_filter '/lib' '/netalertx/lib';
  sub_filter '/php' '/netalertx/php';
}

HTTPS配置

HTTPS配置与HTTP类似,只需添加SSL相关参数:

server {
  listen 443;
  server_name netalertx;
  ssl on;
  ssl_certificate /etc/ssl/certs/netalertx.pem;
  ssl_certificate_key /etc/ssl/private/netalertx.key;
  
  # 其余配置与HTTP版本相同
  proxy_preserve_host on;
  proxy_pass http://localhost:20211/;
  proxy_pass_reverse http://localhost:20211/;
}

Apache配置方案

HTTP直接路径配置

  1. 创建文件/etc/apache2/sites-available/netalertx.conf
  2. 添加以下内容:
<VirtualHost *:80>
  ServerName netalertx
  ProxyPreserveHost On
  ProxyPass / http://localhost:20211/
  ProxyPassReverse / http://localhost:20211/
</VirtualHost>
  1. 启用配置并重启Apache:
    a2ensite netalertx
    service apache2 reload
    

HTTPS配置

HTTPS配置需要添加SSL相关指令:

<VirtualHost *:443>
  ServerName netalertx
  SSLEngine On
  SSLCertificateFile /etc/ssl/certs/netalertx.pem
  SSLCertificateKeyFile /etc/ssl/private/netalertx.key
  
  ProxyPreserveHost On
  ProxyPass / http://localhost:20211/
  ProxyPassReverse / http://localhost:20211/
</VirtualHost>

容器化环境配置

使用SWAG容器

SWAG(LinuxServer的Web应用网关)是流行的反向代理解决方案:

  1. 创建文件/config/nginx/proxy-confs/netalertx.subfolder.conf
  2. 添加以下内容:
location /netalertx {
  return 301 $scheme://$host/netalertx/;
}

location ^~ /netalertx/ {
  include /config/nginx/proxy.conf;
  include /config/nginx/resolver.conf;
  
  set $upstream_app netalertx;
  set $upstream_port 20211;
  set $upstream_proto http;
  
  proxy_pass $upstream_proto://$upstream_app:$upstream_port;
  proxy_set_header Accept-Encoding "";
  
  proxy_redirect ~^/(.*)$ /netalertx/$1;
  rewrite ^/netalertx/?(.*)$ /$1 break;
  
  # 内容替换配置
  sub_filter_once off;
  sub_filter_types *;
  sub_filter 'href="/' 'href="/netalertx/';
  sub_filter '(?>$host)/css' '/netalertx/css';
  sub_filter '(?>$host)/js' '/netalertx/js';
  sub_filter '/img' '/netalertx/img';
  sub_filter '/lib' '/netalertx/lib';
  sub_filter '/php' '/netalertx/php';
}

使用Traefik配置

对于使用Traefik作为反向代理的用户:

  1. dynamic.toml中添加路由配置:
[http.routers]
  [http.routers.netalertx-router]
    entryPoints = ["websecure"]
    rule = "Host(`www.domain.com`) && PathPrefix(`/netalertx`)"
    service = "netalertx-service"
    middlewares = "auth,netalertx-stripprefix"
    [http.routers.netalertx-router.tls]
       certResolver = "section31"
       [[http.routers.netalertx-router.tls.domains]]
         main = "www.domain.com"

[http.services]
  [http.services.netalertx-service]
    [[http.services.netalertx-service.loadBalancer.servers]]
      url = "http://internal-ip-address:20211/"

[http.middlewares]
  [http.middlewares.netalertx-stripprefix.stripprefix]
    prefixes = "/netalertx"
    forceSlash = false
  1. 修改NetAlertX容器中的默认Nginx配置:
server {
    listen 80 default_server;
    root /var/www/html;
    index index.php;
    add_header X-Forwarded-Prefix "/netalertx" always;
    proxy_set_header X-Forwarded-Prefix "/netalertx";

    location ~* \.php$ {
      fastcgi_pass unix:/run/php/php8.2-fpm.sock;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
      fastcgi_connect_timeout 75;
      fastcgi_send_timeout 600;
      fastcgi_read_timeout 600;
    }
}

常见问题解决

  1. 静态资源加载失败:确保使用了sub_filter正确替换资源路径
  2. 重定向循环:检查proxy_redirectrewrite规则是否正确
  3. HTTPS混合内容警告:确保所有资源都通过HTTPS加载

最佳实践建议

  1. 优先使用HTTPS配置以确保通信安全
  2. 对于生产环境,建议使用子路径配置以便管理多个应用
  3. 容器化部署时,考虑使用环境变量来动态配置反向代理路径
  4. 定期检查证书有效期并设置自动续期

通过以上配置,您可以根据实际环境需求灵活地为NetAlertX设置反向代理,无论是直接部署还是容器化环境都能获得良好的访问体验。

NetAlertX 💻🔍 WIFI / LAN intruder detector. Scans for devices connected to your network and alerts you if new and unknown devices are found. NetAlertX 项目地址: https://gitcode.com/gh_mirrors/ne/NetAlertX

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魏侃纯Zoe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值