繁体   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