angular propxy.conf代理,可前台解决跨域
- 1.配置proxy.conf.json
- 2.更改package.json文件内容
- 3.angular.json中添加内容
- 4.重启
- 5. 生产环境
- 6.参考博客[666](https://blog.csdn.net/weixin_42417934/article/details/112802768?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_v2~rank_aggregation-1-112802768.pc_agg_rank_aggregation&utm_term=proxy.conf.json&spm=1000.2123.3001.4430)
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