0

我Nginx的代理具有以下配置:如何nginx的剝離上下文路徑,zuul和OAuth合併

upstream gateway_server { 
    ip_hash; # sticky session on as there will be more than one destination 
    server app.server:8080; 
} 

server { 

    listen     0.0.0.0:80; 
    server_name    my.domain.net; 
    root     /var/www/gateway; 

    proxy_redirect   off; 
    proxy_buffering   off; 
    proxy_connect_timeout 5s; 
    proxy_read_timeout  120s; 
    proxy_next_upstream  error timeout http_502 http_503 http_504; 
    proxy_set_header  X-Real-IP $remote_addr; 
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header  X-Forwarded-Host $host; 
    proxy_set_header  X-Forwarded-Proto $http_x_forwarded_proto; 
    proxy_set_header  Host $host; 
    proxy_intercept_errors on; 

location/{ 
    proxy_pass    http://gateway_server/my-application/; 
} 

location /loginPage { 
    proxy_pass    http://gateway_server/loginPage; 
} 

location ^~ /login { 
    proxy_pass    http://gateway_server/login; 
} 

location /logout { 
    proxy_pass    http://gateway_server/logout; 
} 

location /css { 
    proxy_pass    http://gateway_server/css; 
} 


error_page 502 503 504 @maintenance; 
location @maintenance { 
    rewrite ^(.*)$   /maintenance.html break; 
} 

error_page 404 /404.html; 
location = /404.html { 
    rewrite ^(.*)$   /404.html break; 
} 

error_page 500 /500.html; 
location = /500.html { 
    rewrite ^(.*)$   /500.html break; 
} 
} 

網關服務器我zuul代理,這是註釋簡單SpringBoot應用程序指向與@EnableZuulProxy。路由被配置如下:

# Zuul configuration 
zuul: 
    host: 
    socket-timeout-millis: 300000 # 5 minutes 
    sensitiveHeaders: 
    routes: 
    my-application: 
     path: /my-application/** 
     serviceId: my-application 
     stripPrefix: false 

我的應用是一個Spring啓動應用具有角前端結合。除了路由,網關服務器也負責認證 - 它與OAuth2多提供商通信。當用戶未通過身份驗證,並嘗試呼叫my.domain.net Nginx代理對zuul的呼叫(app.server:8080/my-application/網關服務器啓動Oauth2協議,並轉發用戶到登錄頁面,在那裏你可以選擇他/她的提供者(臉書,谷歌,內部)。整個身份驗證是由春天管理的 - 我不做任何自定義的事情。成功登錄後,用戶被重定向到my.domain.net/my-application而不是my.domain.net。 你知道我的基礎設施中的哪個元素添加了這個上下文路徑嗎?你有什麼想法,我怎麼能刪除它?

回答

0

你只需要使用stripPrefix:正確的,因爲你的配置:

path: /my-application/** 
serviceId: my-application 
stripPrefix: false 

告訴彈簧保持/my-application/重定向後(這是在zuul理解「前綴」)。

+0

我無法在此處使用stripPrefix = true,因爲「/ my-application」是此服務中的上下文路徑(這對於獲取大量資源而言非常重要,因爲應用程序使用ajax調用,位於此路徑下) –