You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Nginx实现非443端口访问/ui路径时自动跳转至HTTPS 443端口

Nginx 定向跳转配置方案

核心配置逻辑

需要同时匹配两个条件才触发跳转:

  • 监听端口不等于443
  • 请求路径前缀为/ui/

两个条件同时满足时才返回跳转至对应HTTPS地址,其余所有请求均正常转发。

完整配置示例

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

    # SSL 证书相关配置请自行补充
    # ssl_certificate /path/to/your/cert.pem;
    # ssl_certificate_key /path/to/your/key.pem;

    # 跳转规则:端口非443且路径以/ui/开头时跳转
    if ($server_port != 443) {
        rewrite ^/ui/(.*)$ https://$server_name/ui/$1 permanent;
    }

    # /api/ 端点代理配置
    location /api/ {
        proxy_pass http://你的上游API服务地址/;
        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;
    }

    # /ui/ 端点代理配置
    location /ui/ {
        proxy_pass http://你的上游UI服务地址/;
        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;
    }
}

规则验证说明

配置重载后可通过以下场景验证是否生效:

  • 访问http://example.com:8080/ui/:自动跳转至https://example.com/ui/,符合预期
  • 访问https://example.com/ui/:正常返回UI页面,无跳转,符合预期
  • 访问http://example.com:8080/api/:正常返回API响应,无跳转,符合预期
  • 访问https://example.com/api/:正常返回API响应,无跳转,符合预期

注:如果需要临时跳转方便调试,可以把permanent(对应301永久跳转)改为redirect(对应302临时跳转),避免浏览器缓存旧的跳转规则影响调试。

内容的提问来源于stack exchange,提问作者user7468503

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 22:09:03