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

时间:2016-05-09 12:03:26

标签: node.js apache express gitlab reverse-proxy

我目前正在开发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"
}

1 个答案:

答案 0 :(得分:0)

我找到了规则的工作决议。

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

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