如何在nodejs express上为gitlab配置反向代理规则

How to configure reverse proxy rules for gitlab on nodejs express

我目前正在开发 nodejs 应用程序来为私有 gitlab 服务器提供服务。我让它与 Apache 一起工作,但现在我不想用 Nodejs 应用程序替换 Apache。我几乎可以正常工作了。

网站运行正常,我可以看到我的项目等

当我尝试像这样克隆存储库时

git clone http://example.com:3000/apps/test.git

它将克隆空存储库,并给出这个

Cloning into 'test'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

存储库不为空。当我使用 Apache

克隆它时它起作用了

app.js

let express = require('express'),
    app = express(),

    PORT = process.env.PORT||3000

    httpProxy = require('http-proxy'),
    path = require('path'),
    HttpProxyRules = require('http-proxy-rules'),
    proxy = httpProxy.createProxyServer()


app.use((req, res, next) => {
    let targetUri= `http://localhost:8181${req.url}`
    let targetUri2= `http://localhost:8080${req.url}`

    let proxyRules = new HttpProxyRules({
        rules: {
            '/[\w\.-]+/[\w\.-]+/repository/archive.*': targetUri,
            '/api/v3/projects/.*/repository/archive.*': targetUri,
            '/[\w\.-]+/[\w\.-]+/(info/refs|git-upload-pack|git-receive-pack)$': targetUri,
            '/uploads': targetUri2
        },
        default: 'http://localhost:8080'

        let proxy = new httpProxy.createProxy()
        let target = proxyRules.match(req)

        if(target)
            return proxy.web(req, res { target }

    })
})
console.log(`Listening port ${PORT}`)
app.listen(PORT)

package.json

"dependencies": {
    "express": "^4.13.4",
    "http-proxy": "^1.13.2",
    "http-proxy-rules": "^1.0.1"
}

我找到了规则的工作解决方案。

let targetUri = `http://localhost:8181${req.url}`

let proxyRules = new HttpProxyRules({
    rules: {
        '/api/v3/.*': targetUri,
        '/uploads': targetUri
    },
    default: 'http://localhost:8181'
})