簡體   English   中英

如何使用 Nginx 在同一台服務器上部署后端和前端但路徑不同

[英]How to deploy backend and frontend on the same server but different path with Nginx

我需要在 https://service.domain.it 中部署前端,在https://service.domain.it/api部署后端。 我已經完成了部署配置,但我遇到了 spring 安全性 + JWT 身份驗證的問題。 這是 WebSecurityConfigurerAdapter 的配置方法

@Override
public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            .authorizeRequests()
            .antMatchers("/public/**").permitAll() //login/logout/recoverypassw
            .antMatchers("/user/**").hasAnyRole("USER","ADMIN")
            .antMatchers("/admin/**").hasRole("ADMIN")
            .anyRequest().authenticated()
            .and().exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).
            and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).
            and().addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}

這是 nginx 配置

#service.domain.it
server{
        listen 443 ssl;
        server_name smartgrid.russi.ovh;
        ssl_certificate /etc/letsencrypt/live/service.domain.it/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/service.domain.it/privkey.pem;
        location /api/ {
                #backend
                proxy_pass http://localhost:8085;
        }

       location / {
               #frontend
               index index.html;
                alias /var/www/html/Service/;
       }
}
server{
        listen 80;
        server_name service.domain.it;
        return 301 https://service.domain.it$request_uri;
}

我應該怎么辦?

您的后端服務不了解您的 proxy_configuration 中使用的/api 在將 URI 發送到后端之前,您必須重寫它。

location /api {

   rewrite ^/api/(.*) /$1 break;
   proxy_pass http://backend/;

}

這將在將/api發送到后端之前從 URI 中刪除它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM