nginx解决跨域问题

一、
在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("*")
        ;
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值