You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js 13(App目录)部署至IIS后NextAuth出现INVALID_URL错误

Next.js 13(App目录)部署至IIS后NextAuth出现INVALID_URL错误

我太懂这种本地正常、部署就出问题的糟心感了!从你描述的情况来看,问题根源大概率是IIS环境下Next.js的请求路由解析出了偏差,导致NextAuth依赖的登录/回调路由没被正确识别。

先梳理下你的核心配置和问题场景:

你的NextAuth路由配置(app/api/auth/[...nextauth]/route.js)

import NextAuth from 'next-auth/next';
import Auth0Provider from 'next-auth/providers/auth0';

export const authOptions = {
  providers: [
    Auth0Provider({
      clientId: process.env.AUTH0_CLIENT_ID,
      clientSecret: process.env.AUTH0_CLIENT_SECRET,
      issuer: process.env.AUTH0_ISSUER,
    }),
  ],
  callbacks: {
    async jwt({ token, account, profile }) {
      // Persist the OAuth access_token and or the user id to the token right after signin
      if (account) {
        token.accessToken = account.access_token;
        token.id = profile.id;
        token.roles = profile['https://lager.meg-transport.de/roles'];
      }
      return token;
    },
    async session({ session, token }) {
      return {
        ...session?.user,
        roles: token?.roles,
      };
    },
  },
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

问题表现

  • 本地执行yarn build + yarn start时,NextAuth登录流程完全正常
  • 部署到IIS服务器后,访问登录路由直接触发INVALID_URL错误

最终解决方案:修正自定义Server配置

你调整server.js的思路完全正确!在IIS这类反向代理环境下,Next.js需要明确的hostname和port配置,才能正确解析请求URL。下面是修正后的完整server.js代码:

const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = process.env.PORT || 3001;

// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  createServer(async (req, res) => {
    try {
      // Be sure to pass `true` as the second argument to `url.parse`.
      // This tells it to parse the query portion of the URL.
      const parsedUrl = parse(req.url, true);
      const { pathname, query } = parsedUrl;

      if (pathname === '/a') {
        await app.render(req, res, '/a', query);
      } else if (pathname === '/b') {
        await app.render(req, res, '/b', query);
      } else {
        await handle(req, res, parsedUrl);
      }
    } catch (err) {
      console.error('Error occurred handling', req.url, err);
      res.statusCode = 500;
      res.end('internal server error');
    }
  }).listen(port, err => {
    if (err) throw err;
    console.log(`> Ready on http://${hostname}:${port}`);
  });
});

额外避坑检查点

为了避免后续再出现类似问题,建议你再确认这几点:

  • IIS服务器上的环境变量(AUTH0_CLIENT_ID、AUTH0_CLIENT_SECRET、AUTH0_ISSUER)是否配置正确,没有拼写错误或遗漏
  • IIS的URL重写规则是否生效,确保所有请求都能正确转发到Next.js服务的端口(上面配置的是3001)
  • Auth0控制台里的回调URL、注销URL,是否和你部署后的站点域名完全一致,这也是URL错误的常见诱因

备注:内容来源于stack exchange,提问作者Luke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 12:48:03