nuxt-alt / proxy

An alternative module to @nuxtjs/proxy
MIT License
24 stars 5 forks source link

Doesn't work with self-signed certificates #4

Closed chrisnoden closed 1 year ago

chrisnoden commented 1 year ago

When testing locally, my target (back-end) server is running with a self-signed cert.

Even when I start nuxt with NODE_TLS_REJECT_UNAUTHORIZED=0 the proxy fails to connect to the back-end server:

http proxy error:Error: self signed certificate in certificate chain                                                                                                                               15:35:33
    at TLSSocket.onConnectSecure (node:_tls_wrap:1535:34)
    at TLSSocket.emit (node:events:513:28)
    at TLSSocket._finishInit (node:_tls_wrap:949:8)
    at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:730:12) {
  timestamp: true,
  error: Error: self signed certificate in certificate chain
      at TLSSocket.onConnectSecure (node:_tls_wrap:1535:34)
      at TLSSocket.emit (node:events:513:28)
      at TLSSocket._finishInit (node:_tls_wrap:949:8)
      at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:730:12) {
    code: 'SELF_SIGNED_CERT_IN_CHAIN'
  }
}
chrisnoden commented 1 year ago

Also tried the "global" npm config set strict-ssl=false before running nuxt dev, but nothing seems to clear the above problem only affecting the proxy

Denoder commented 1 year ago

what does the proxy config look like?

chrisnoden commented 1 year ago

config:

    proxy: {
        proxies: {
            '/image': {
                target: process.env.SERVER_URL,
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/image/, '')
            },
        }
    },

the env can either be an IP or a hostname or a locally proxied hostname https://127.0.0.1:8000/api or https://localcert.dev:8000/api or https://localcert.dev/api

However, if I use a service such as ngrok, it's fine: env value would be something like https://4cff-194-35-99-18.ap.ngrok.io/image

Denoder commented 1 year ago

Try using secure: false In the proxy's options

chrisnoden commented 1 year ago

That works. Thanks. I didn't spot that in the code when I tried to look for any undocumented options. It would be good to bring that option forward.

Denoder commented 1 year ago

@chrisnoden this module uses https://github.com/refactorjs/http-proxy which is a refactor of https://github.com/http-party/node-http-proxy while trying to get rid of the bugs it incorporates and features that were left unused in pull requests.