一、
在nginx的配置文件nginx.conf文件中
server {
listen 80;
server_name localhost;
location / {
root /home/nginx/html/ryApplyUi;
try_files $uri $uri/ /index.html late;
index index.html index.htm;
#解决跨域
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT';
add_header 'Access-Control-Allow-Headers' *;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
return 204;
}
}
#large-model是后端接口的前缀
location /large-model/ {
proxy_pass http://47.106.106.54:8083/; #后端服务器的ip地址和端口
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、如果前端是用docker部署
那么也要在前端的default.conf文件中加上。
3、重启nginx和重新构建前端容器和镜像。
二、后端写一个配置类
package com.rykj.largemodel.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(false)
.allowedMethods("POST","GET","DELETE","PUT","OPTIONS")
.allowedOrigins("*")
;
}
}