【鱼皮大佬API开放平台项目】Spring Cloud Gateway HTTPS 配置问题解决方案总结

问题背景

项目架构为前后端分离的微服务架构:

  • 前端部署在 8000 端口
  • API 网关部署在 9000 端口
  • 后端服务包括:
    • api-backend (9001端口)
    • api-interface (9002端口)

初始状态:

  • 前端已配置 HTTPS(端口 8000)
  • 后端服务未配置 HTTPS
  • 通过 Nginx 进行反向代理

遇到的问题

  1. 第一阶段:400 Bad Request

    This combination of host and port requires TLS

原因:前端使用 HTTPS,但后端服务未正确配置 SSL 证书。由于前端首先请求是到达api-gateway 网关层。借助Spring Cloud Gateway 进行请求转发。虽然给API gateway分配了https,但是它转发的另外两个模块的端口没有分配HTTPS导致请求转发错误。

  1. 第二阶段:403 错误

    Failed to resolve 'api-backend' after 4 queries

原因:网关路由配置使用服务名而非具体地址,导致服务发现失败。

项目配置文件

解决方案

1. SSL 证书配置

为所有服务配置 SSL:

  1. API Gateway (9000端口):
  • application.yml 配置
yaml 复制代码
server:
  port: 9000
  address: 0.0.0.0
  ssl:
    enabled: true
    key-store: /www/server/nginx/ssl/<服务器ip>/keystore.p12
    key-store-password: 1234
    key-store-type: PKCS12
  http2:
    enabled: true
spring:
  cloud:
    gateway:
      routes:
        - id: api-backend
          uri: https://<服务器ip>:9001
          predicates: 
            - Path=/api/backend/**
          filters:
            - StripPrefix=0
            - AddRequestHeader=X-Forwarded-Proto,https
        - id: api-interface
          uri: https://<服务器ip>:9002
          predicates: 
            - Path=/api/interface/**
          filters:
            - StripPrefix=0
            - AddRequestHeader=X-Forwarded-Proto,https
      ssl:
        key-store: /www/server/nginx/ssl/<服务器ip>/keystore.p12
        key-store-password: 1234
        key-store-type: PKCS12

logging:
  level:
    org:
      springframework:
        cloud:
          gateway: DEBUG

dubbo:
  application:
    name: api-gateway-dubbo
    qosEnable: false
    enable-file-cache: false
  protocol:
    name: dubbo
    port: -1
    # 注意这里ssl也需要配置
  ssl:
    enabled: true
  • application-prod.yml
yml 复制代码
spring:
  cloud:
    nacos:
      server-addr: 192.168.15.233:8848
      secure: true   # 添加这行,启用安全连接
      config:
        file-extension: yaml
  • bootstrap.yml
yml 复制代码
spring:
  cloud:
    nacos:
      server-addr: 192.168.15.233:8848
      secure: true   # 添加这行,启用安全连接
      config:
        file-extension: yaml
  application:
    name: api-interface
  1. API-backend (9001端口)
  • application.yml配置
yml 复制代码
server:
  port: 9001
  address: 0.0.0.0
  ssl:
    enabled: true
    key-store: /www/server/nginx/ssl/<服务器ip>/keystore.p12
    key-store-password: 1234
    key-store-type: PKCS12
  http2:
    enabled: true
  servlet:
    context-path: /api/backend
    session:
      cookie:
        max-age: 7d
        secure: true

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 100MB
  session:
    store-type: redis
    timeout: 7d

knife4j:
  enable: true
  basic:
    enable: true
    username: root
    password: xxx
dubbo:
  application:
    name: api-backend-dubbo
    qosEnable: false
    enable-file-cache: false
  protocol:
    name: dubbo
    port: -1
  registry:
    address: nacos://192.168.15.233:8848
  ssl:
    enabled: true # 注意这里true
  • application-prod.yml
yml 复制代码
# 客户端SDK 配置
api:
  client:
    gateway-host: https://<服务器ip>:9000
  • bootstrap.yml
yml 复制代码
spring:
  application:
    name: api-backend
  profiles:
    active: prod
  cloud:
    nacos:
      server-addr: 192.168.15.233:8848
      secure: true   # 添加这行,启用安全连接
      config:
        file-extension: yaml
  1. API- interface
  • application.yml
yml 复制代码
server:
  port: 9002
  address: 0.0.0.0
  ssl:
    enabled: true
    key-store: /www/server/nginx/ssl/<服务器ip>/keystore.p12
    key-store-password: 1234
    key-store-type: PKCS12
  http2:
    enabled: true
  servlet:
    context-path: /api/interface
#-------------------------------------------------
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
dubbo:
  ssl:
    enabled: true     
  • application-prod.yml
yml 复制代码
spring:
  cloud:
    nacos:
      server-addr: 192.168.15.233:8848
      secure: true   # 添加这行,启用安全连接
      config:
        file-extension: yaml
  • bootstrap.yml
yml 复制代码
spring:
  cloud:
    nacos:
      server-addr: 192.168.15.233:8848
      secure: true   # 添加这行,启用安全连接
      config:
        file-extension: yaml
  application:
    name: api-interface

2. Nginx 配置

更新 Nginx 配置以支持 HTTPS 代理:

nginx 复制代码
location /api {
    rewrite ^/api/(.*) /$1 break;
    proxy_pass https://<服务器ip>:9000/api;
    proxy_ssl_verify off;
    proxy_ssl_server_name on;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
}

关键配置点说明

  1. SSL 证书配置

    • 所有服务统一使用相同的 SSL 证书
    • 启用 HTTP2 以提升性能
    • 确保证书文件权限正确
  2. 网关路由

    • 使用具体的服务地址而不是服务名
    • 添加必要的请求头
    • 保持 SSL 配置一致性
  3. 服务间通信

    • Dubbo 服务启用 SSL
    • 确保所有通信链路使用 HTTPS

最佳实践建议

  1. 证书管理

    • 使用统一的证书存储位置
    • 确保证书文件权限正确
    • 定期更新证书
  2. 配置管理

    • 使用配置中心统一管理
    • 环境隔离(开发/生产)
    • 密码等敏感信息加密存储
  3. 安全增强

    • 启用 HTTP2
    • 配置安全的 SSL 协议和密码套件
    • 启用 HSTS 策略
相关推荐
速易达网络3 小时前
RuoYi、Vue CLI 和 uni-app 结合构建跨端全家桶方案
javascript·vue.js·低代码
lyj1689974 小时前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
隆里卡那唔4 小时前
在dify中通过http请求neo4j时为什么需要将localhost变为host.docker.internal
http·docker·neo4j
charlee444 小时前
nginx部署发布Vite项目
nginx·性能优化·https·部署·vite
麦兜*5 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
~山有木兮5 小时前
LiteHub中间件之限流实现
网络·http·中间件
游戏开发爱好者87 小时前
iOS App首次启动请求异常调试:一次冷启动链路抓包与初始化流程修复
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_915106327 小时前
频繁迭代下完成iOS App应用上架App Store:一次快速交付项目的完整回顾
websocket·网络协议·tcp/ip·http·网络安全·https·udp
CRMEB定制开发7 小时前
【实战】CRMEB Pro 企业版安装教程(附 Nginx 反向代理配置 + 常见问题解决)
nginx·商城系统·微信商城·crmeb
我在北京coding7 小时前
TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
前端·javascript·vue.js