Angular代理路径重写失效:HTTP GET请求未按配置转发
Hey there, I’ve run into similar proxy forwarding issues with Angular before—let’s walk through how to get this sorted out.
First, let’s break down the likely problems and solutions step by step:
1. Adjust the Path Matching in Proxy Config
Your current proxy uses "/sap" as the path match, which only targets the exact /sap endpoint. But your request is going to /sap/opu/odata/..., a subpath. Angular’s proxy relies on http-proxy-middleware, which uses micromatch for path patterns—so you need to use "/sap/**" to match all paths starting with /sap.
Update your proxy.config.json to this:
{ "/sap/**": { "target": "http://server.domain.com:8002", "secure": false, "pathRewrite": { "^/sap": "" }, "changeOrigin": true, "logLevel": "debug" } }
2. Verify Proxy Loading & Logs
When you start the app with ng serve --proxy-config proxy.config.json, check your terminal for debug logs from the proxy. You should see a line like:
Proxy created: /sap/** -> http://server.domain.com:8002
If you don’t see this, double-check:
- The path to
proxy.config.jsonis correct (it should live in your project root directory) - There are no typos in the
--proxy-configflag (make sure it’s not--proxyConfigwith no hyphen) - Your JSON config is valid (use a quick online JSON validator if you’re unsure)
3. Clean Up Unnecessary Request Headers
You’ve added Access-Control-Allow-Methods to your request headers—but this is a response header (sent by the server to the client), not something the client should send. Including it doesn’t break the proxy, but it’s unnecessary and can cause confusion. Remove that line:
var headers = new Headers(); headers.append('Content-Type', 'application/json'); headers.append('Accept', 'application/json'); headers.append('X-Requested-With','XMLHttpRequest');
4. Bypass Browser Caching
Sometimes browsers cache old request routes, especially if you’ve been testing different configurations. Try:
- Opening your app in an incognito/private window
- Clearing your browser’s cache and hard reloading (Ctrl+Shift+R or Cmd+Shift+R)
5. Check for Angular.json Overrides
If you’ve set proxyConfig in your angular.json under projects.your-project-name.architect.serve.options, make sure it points to the correct file. If this is set, you don’t need to include --proxy-config in your ng serve command—but if it’s missing or incorrect, the proxy won’t load.
After making these changes, restart your ng serve process and test the request again. You should see the request being forwarded to http://server.domain.com:8002/opu/odata/sap/ABC_FD_SRV/ID_GEN_Set?sap-client=010 instead of localhost.
内容的提问来源于stack exchange,提问作者Manoj Kumar

