angular propxy.conf代理,可前台解决跨域

1.配置proxy.conf.json

{
    "/api": {
        "target": "http://10.57.30.200:8092/",
        "logLevel":"debug",
        "secure": false,
        "pathRewrite": { 
            "^/api": ""
        }
      },
      "/java_url": {
        "target": "http://10.58.241.10:7001/flow_system-2021_01_03/",
        "secure": false,
        "pathRewrite": { 
            "^/java_url": ""
        }
      }    
}

注:踩坑:
(1)当时写的时候先写了/api后写了/api1来设定java后台可一直报错,添加属性logLevel后才知晓/api1识别成了/api+1导致无法识别java后台
以后命名时要避免相似性
(2)logLevel打印不在控制台,在运行的黑窗口显示

2.更改package.json文件内容

start:
"start": "ng serve --proxy-config proxy.conf.json",

3.angular.json中添加内容

"options": { "browserTarget": "FLOW-UI:build", "proxyConfig": "proxy.conf.json" },

4.重启

使用npm start启动命令
注:只有重启才会生效


以上配置只能在开发环境适用,生产环境中不生效

5. 生产环境

nginx的conf.g文件内容更改即可

server {
    listen       4214;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/local/etc/dist/cxm_test;
        index  index.html index.htm;
               try_files $uri $uri/ /index.html;
    }
    location /api{
       proxy_pass http://10.57.30.200:8092/;
       proxy_set_header Host $http_host;
   }
   location /java_url{
       proxy_pass http://10.58.241.10:7001/flow_system-2021_01_03/;
       proxy_set_header Host $http_host;
   }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    error_page 405 =200 $uri;
   
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

注:踩坑记录:当时在开发环境中运行好的代码直接发布到nginx中,但运行的时候会报 405 not allowed,百度过后说是因为nginx请求的静态文件采用的是post方法,nginx是不允许post访问静态资源
解决方案:nginx.conf文件中添加
error_page 405 =200 $uri;
但发现是假的,当报405时确实显示的是200 OK,但数据无法获取

6.参考博客666

1.开发环境:https://blog.csdn.net/weixin_42417934/article/details/112802768?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2aggregatepagefirst_rank_v2~rank_aggregation-1-112802768.pc_agg_rank_aggregation&utm_term=proxy.conf.json&spm=1000.2123.3001.4430
2.生产环境:
https://my.oschina.net/u/930306/blog/769840

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值