首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NextJS FetchError ETIMEDOUT

NextJS FetchError ETIMEDOUT
EN

Stack Overflow用户
提问于 2022-07-13 10:01:43
回答 1查看 618关注 0票数 1

我把我的项目部署在centos 7上,端口转发到8080,这意味着我们使用ip的站点:8080。下面是我用于前端和后端反向代理的NGINX配置。

站点nginx config

代码语言:javascript
运行
复制
server {
        listen       80;
        listen       [::]:80;
        server_name  myapp.com etc.com;
        
        #my api
        location / {
                proxy_set_header Host               $host;
                proxy_set_header X-Real-IP          $remote_addr;
                proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto  $scheme;
                proxy_set_header X-Auth-Request-Redirect "http://api.myapp.com";
                proxy_cache_bypass                      $http_upgrade;
                proxy_pass                          http://127.0.0.1:3333;
                proxy_http_version      1.1;
                proxy_buffer_size          128k;
                proxy_buffers              4 256k;
                proxy_busy_buffers_size    256k;
                proxy_redirect                  off;
                #proxy_cookie_path / "/; SameSite=lax; HTTPOnly; Secure";
        }
        
        #my app
        location /myapp {
                proxy_set_header Host               $host;
                proxy_set_header X-Real-IP          $remote_addr;
                proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto  $scheme;
                proxy_set_header X-Auth-Request-Redirect "http://ipaddress:8080/adminpage";
                proxy_cache_bypass                      $http_upgrade;
                proxy_pass                          http://127.0.0.1:3000;
                proxy_http_version      1.1;
                proxy_buffer_size          128k;
                proxy_buffers              4 256k;
                proxy_busy_buffers_size    256k;
                #proxy_cookie_path / "/; SameSite=lax; HTTPOnly; Secure";
        }

}

我得到了一个错误

代码语言:javascript
运行
复制
 FetchError: request to http://ipaddress:8080/auth/checkauth failed, reason: connect ETIMEDOUT ipaddress:8080
     at ClientRequest.<anonymous> (/root/web/myapp/node_modules/next/dist/compiled/node-fetch/index.js:1:64142)
     at ClientRequest.emit (node:events:527:28)
     at Socket.socketErrorListener (node:_http_client:454:9)
     at Socket.emit (node:events:527:28)
     at emitErrorNT (node:internal/streams/destroy:157:8)
     at emitErrorCloseNT (node:internal/streams/destroy:122:3)
     at processTicksAndRejections (node:internal/process/task_queues:83:21) {
   type: 'system',
   errno: 'ETIMEDOUT',
   code: 'ETIMEDOUT'
 }

_middleware.ts

代码语言:javascript
运行
复制
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

export async function middleware(req: NextRequest) {
  const token = req.cookies;
  const urlClone = req.nextUrl.clone();
  urlClone.pathname = `/404`;

  const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/checkauth`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${token.app_token}`,
    },
  });

  if (res.status === 200) {
    return NextResponse.next();
  }
  return NextResponse.rewrite(urlClone);
}

该应用程序在生产中运行良好,我的Axios也是如此。我可以使用axios请求通过我的应用程序登录,但是我的中间件具有fetch api和getServerSideProps,它们也具有具有连接ETIMEDOUT错误的fetch。

到目前为止我尝试过的

adapter

  • tried使用
  • 代理代理
  • 将获取api更改为axios,同时使用url
  • 将cors设置到 API上,并设置了真正的。

我还可以使用服务器内部的curl和本地计算机上的postman调用我的api端点。

我在linux服务器上部署的另一个NextJS应用程序也是同样的过程,它有中间件中的fetch api,而且getServerSideProps工作正常,但是该服务器没有被转发到任何端口。我想知道这是否是问题所在

我使用了NextJS v12.1.6

EN

回答 1

Stack Overflow用户

发布于 2022-07-13 11:06:58

我通过将我的await fetch基url替换为服务器上的本地主机http://127.0.0.1:3333 (api主机)来解决这个问题。很适合我的案子。

代码语言:javascript
运行
复制
const res = await fetch(`http://127.0.0.1:3333/auth/checkauth`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${token.app_token}`,
    },
  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72964404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档