Skip to content

Commit

Permalink
fix(server): proxy throw error 403 (#8828)
Browse files Browse the repository at this point in the history
* fix(server): proxy throw error 403

* Update packages/server/src/Server/Server.ts

Co-authored-by: chencheng (云谦) <[email protected]>
citrusjunoss and sorrycc authored Aug 3, 2022
1 parent 50a0269 commit 8cdb9bc
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/server/src/Server/Server.ts
Original file line number Diff line number Diff line change
@@ -271,13 +271,19 @@ class Server {
// It is possible to use the `bypass` method without a `target`.
// However, the proxy middleware has no use in this case, and will fail to instantiate.
if (proxyConfig.target) {
const target =
typeof proxyConfig.target === 'object'
? url.format(proxyConfig.target)
: proxyConfig.target;
return createProxyMiddleware(context!, {
...proxyConfig,
onProxyReq(proxyReq) {
// 修改 host 模拟域名,避免 cors 只允许固定域名时报错
if (proxyReq.getHeader('origin')) {
proxyReq.setHeader('origin', target);
}
},
onProxyRes(proxyRes, req: any, res) {
const target =
typeof proxyConfig.target === 'object'
? url.format(proxyConfig.target)
: proxyConfig.target;
const realUrl = new URL(req.url || '', target)?.href || '';
proxyRes.headers['x-real-url'] = realUrl;
proxyConfig.onProxyRes?.(proxyRes, req, res);

0 comments on commit 8cdb9bc

Please sign in to comment.