uni-app:发布为h5站点时服务端的api配置

一,开发环境中对接口跨域的配置

manifest.json中添加

    "h5" : {
        "devServer" : {
            "https" : false,
            "disableHostCheck" : true,
            "proxy" : {
                "/api" : {
                    "target" : "http://base.lhdtest.com", //域名
                    "changeOrigin" : true,
                    "secure" : false,
                    "pathRewrite" : {
                        "^/api" : ""
                    }
                }
            }
        },
        "title" : "小学生古诗词背诵"
    },

说明:刘宏缔的架构森林是一个专注架构的博客,

网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/06/05/uniapp-fa-bu-wei-h5-zhan-dian-shi-fu-wu-duan-di-api-pei-zhi/

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,生产环境对接口配置

1,接口站的nginx配置

[lhdop@blog conf.d]$ more baseport.conf
server {
    listen       18880;
    server_name  127.0.0.1;
    root         /web/base/tpapibase/public;
    index  index.php;
    access_log      /data/logs/nginxlogs/baseport.access_log;
    error_log       /data/logs/nginxlogs/baseport.error_log;
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?s=$1 last;
             break;
        }
        location / {
           index  index.html index.php;
        }
        location ~ \.php {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }
}

2,h5站的配置:

[lhdop@blog conf.d]$ more site40.conf
server {
    listen       80;
    server_name  site40.lhdtest.com;
    root         /data/web/40/html;
    index         index.html;
    location /api {
        rewrite  ^/api/(.*)$ /$1 break;
        proxy_pass  http://localhost:18880;
        proxy_redirect off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
    }
    location / {
        try_files $uri $uri/ /index.html;
    }
    access_log      /data/logs/nginxlogs/site40_web.access_log;
    error_log       /data/logs/nginxlogs/site40_web.error_log;
}

注意:针对/api的访问通过代理转到了接口站的18880端口

三,测试效果

 

posted @ 2023-03-09 18:38  刘宏缔的架构森林  阅读(613)  评论(0编辑  收藏  举报