在 ubuntu 服务器上设置 nginx proxy_pass

时间:2021-06-22 17:18:58

标签: ubuntu nginx nginx-reverse-proxy

我需要为某个特定位置设置 proxy_pass。 在本地反应我使用代理中间件,我的配置看起来像这样

app.use(
    "/firebase",
    createProxyMiddleware({
      target: "https://firebasestorage.googleapis.com/v0/b/poplco.appspot.com/o/",
      pathRewrite: { "^/firebase": "/" },
      headers: { "X-Forwarded-Prefix": "/" },
      changeOrigin: true,
    }),
  );

当我打电话时

fetch(`/firebase/logos%2F${profile.generalSettingsData[3]}?alt=media`)

它完美地工作。 但是对于生产,我使用带有 nginx 的 ubuntu 服务器并尝试设置位置规则。为了这 我试过了

location /firebase {
       proxy_pass         https://firebasestorage.googleapis.com/v0/b/poplco.appspot.com/o/;
    }

当然不行。正如我所见,我应该设置重写规则来处理来自我的请求的动态路径参数,因为文件名正在动态更改路径参数。 有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

问题已解决。最终代码 前面

axios({
          baseURL: "/v0",
          url: `/b/poplco.appspot.com/o/logos%2F${profile.generalSettingsData[3]}`,
          method: "GET",
          responseType: "blob",
        })
          .then((res) => {
            convertBlobToBase64(res.data)
              .then((base64) => setImage(base64))
              .catch((error) => console.log(error));
          })
          .catch((err) => console.log("error", { ...err }));
      }

和 Nginx

 location /v0/ {
       resolver 8.8.8.8;
       proxy_pass         https://firebasestorage.googleapis.com$request_uri?alt=media;
    }

nginx 的主要问题在于解析器 8.8.8.8。即使我在 nginx 日志中看到完全正确重写,我也会收到错误 502。在详细研究日志后,我发现了下一个错误enter image description here 除此之外,我在 %2F 方面遇到了很多麻烦,nginx 编码/解码只是在 / 或 %252F 中,但这是另一回事,我在这一点上发现了很多问题

相关问题