nginx代理springboot前后端分离服务--接入cas客户端时内外网配置

nginx代理springboot前后端分离服务--接入cas客户端时内外网配置

我的前后端服务及cas服务端都是nginx代理的,假如我的地址如下:

c 复制代码
外网:10.213.163.219
内网:192.168.10.150

nginx配置

c 复制代码
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        #nginx监听地址和端口(内网地址)
        listen       8091;
        server_name  192.168.10.150;

        #前端
        location ^~ /portal {
            autoindex on;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            proxy_cookie_path /portal/ /cas ;
            alias /usr/share/nginx/html;
            index  index.html;
            try_files $uri $uri/ /index.html last;
        }

        #cas服务端
        location ^~  /cas {
            proxy_pass http://192.168.10.150:8081/cas/;
            add_header Access-Control-Allow-Origin *;
            proxy_cookie_path /cas/ /;  #解决nginx转发丢失cookie的问题

            # 支持 OPTIONS 请求,并设置预检请求结果的缓存时间
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }
            
         }

         #后端
         location ^~  /my_server/ {
            proxy_pass http://192.168.10.150:8082/my_server/;
            add_header Access-Control-Allow-Origin *;
            proxy_cookie_path /my_server/ /;  #解决nginx转发丢失cookie的问题

            # 支持 OPTIONS 请求,并设置预检请求结果的缓存时间
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }
            
         }
    }

    #include /etc/nginx/conf.d/*.conf;
}

前端配置

因为前端是nginx代理的,所以前端配置的后端地址,cas服务端地址都应是外网地址

示例:

json 复制代码
 window.GlobalConfig = {
  TOB_BASE_URL: `http://10.213.163.219:8091/my_server`,
  TOB_CAS_LOGIN_URL: `http://10.213.163.219:8091/cas/login?service=${window.location}`,
  TOB_CAS_COOKIE_NAME: 'TGC',
  TOB_CAS_LOGINOUT_URL: 'http://10.213.163.219:8091/cas/logout?service=http://10.213.163.219:8091/my_server/api/login/logout',
  TOB_IS_OPEN_CAS: true
}

后端配置

因为前端访问后端程序时需要进行登录验证,因为是在浏览器中访问的地址,所以跳转登录地址为外网地址才能进行访问,验证通过之后,服务端会获取票据ticket,会拿票据ticket去cas服务端验证,内网之间程序访问使用外网会不通,所以去cas服务端验证的地址应是内网的。

c 复制代码
cas:
  #cas服务端地址(内网)-nginx代理
  server-url-prefix: "http://192.168.10.150:8082/cas"
  #cas服务端地址登录地址(外网)-nginx代理
  server-login-url: "http://10.213.163.219:8091/cas/login"
  #后端服务地址(外网)-nginx代理
  client-host-url: "http://10.213.163.219:8091"
  validation-type: cas3
相关推荐
舒一笑9 小时前
程序员效率神器:一文掌握 tmux(服务器开发必备工具)
运维·后端·程序员
NineData11 小时前
数据库管理工具NineData,一年进化成为数万+开发者的首选数据库工具?
运维·数据结构·数据库
梦想很大很大1 天前
拒绝“盲猜式”调优:在 Go Gin 项目中落地 OpenTelemetry 链路追踪
运维·后端·go
Sinclair1 天前
内网服务器离线安装 Nginx+PHP+MySQL 的方法
运维
叶落阁主1 天前
Tailscale 完全指南:从入门到私有 DERP 部署
运维·安全·远程工作
十二7402 天前
前端缓存踩坑实录:从版本号管理到自动化构建
前端·javascript·nginx
用户908324602732 天前
Spring AI 1.1.2 + Neo4j:用知识图谱增强 RAG 检索(上篇:图谱构建)
java·spring boot
可观测性用观测云2 天前
云原生网关 Ingress-Nginx 链路追踪实战:OpenTelemetry 采集与观测云集成方案
nginx·kubernetes
用户8307196840822 天前
Spring Boot 集成 RabbitMQ :8 个最佳实践,杜绝消息丢失与队列阻塞
spring boot·后端·rabbitmq
Java水解3 天前
Spring Boot 视图层与模板引擎
spring boot·后端