uniapp项目发布成H5 本地运行和发布服务器nginx代理

uniapp项目发布成H5 本地运行和发布服务器nginx代理

1.本地运行跨域设置
manifest.json的h5中添加

"devServer" : {
            "https" : true,
            "port" : 80, //端口号
            "disableHostCheck" : true
        },
         ,
                 "proxy" : {
                      "/api" : {
                          "target" : "https://xx.xxx.com", //目标接口域名
                         "changeOrigin" : true, //是否跨域
                         "secure" : false, // 设置支持https协议的代理
                          "pathRewrite" : {
                              "^/api" : "" //匹配请求路径里面有 /api 替换成 http://ceshi.dishait.cn
                         }
                      }
                  }

对应的请求路径

const requestBase = "https://xx.xxx.com/"
https://xx.xxx.com/generator/tpurchasemalladvertisemententerprise/login

替换为

api/generator/tpurchasemalladvertisemententerprise

2.部署服务器

去掉第一步中的本地运行h5的设置

h5设置中端口设置为nginx代理端口
在这里插入图片描述
发行打包 将h5整个文件夹放到服务器根目录 /h5
nginx设置

server {

    listen 80;
    server_name xxx.xxx.xxx.com;
	server_tokens off;
	location / {
	            limit_req zone=ConnLimitZone burst=50 nodelay;
				root   /h5;
				index  index.html index.htm;
				autoindex on;    
			}	


  }

后台接口springboot设置全局跨域

@Configuration
public class AuthenConfigurerAdapter implements WebMvcConfigurer {
 @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
                .maxAge(3600);
    }
}

接口请求一开始用域名 发现还是被跨域拦截
直接用ip:端口形式

const requestBase = "http://xx.xx.xx.xx:8097/"

访问http://xxx.xxx.xxx.com成功

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值