如何解决如何使用 Nginx 在同一服务器但不同路径上部署后端和前端
我需要在 https://service.domain.it 中部署前端,在 https://service.domain.it/api 中部署后端。 我已经完成了部署配置,但我在 Spring Security + 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/;
}
这将从 URI 中删除 /api
,然后再将其发送到后端。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。