【问题标题】:How to deploy Next.js static export with Nginx? (deep links not working)如何使用 Nginx 部署 Next.js 静态导出? (深层链接不起作用)
【发布时间】:2023-01-14 05:43:50
【问题描述】:

我将 next.js 导出到 out 文件夹中。

文件夹结构为:

  • 出来
    • index.html
    • 条款.html
    • privacy.html

我将 nginx 设置为从该文件夹提供文件:

server {
    root /var/www/myproject/out;
    index index.html index.htm index.nginx-debian.html;

    server_name myproject.com;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

主页(索引)打开正常。从应用程序内导航到 myproject.com/privacy 之类的网址可以正常工作。问题是如果我尝试直接打开这些链接,它将提供主页(索引)而不是实际页面,因为文件夹中不存在这些 url。直接打开隐私页面的唯一方法是在 url 中添加 html 扩展名:myproject.com/privacy.html

当有人输入 myproject.com/privacy url 时,如何配置 nginx 为实际页面 myproject.com/privacy.html 提供服务?

【问题讨论】:

  • 在 try_files 中包含 $uri.html
  • @Nayan 保佑你,我的朋友,它按预期工作。您想创建一个正确的答案以便我接受吗?
  • 当然。添加了答案。

标签: nginx next.js


【解决方案1】:

问题在try_files

由于当前配置包括:

  • /(默认路由到根路径的 index.html)
  • index.html
  • /index.html
  • 测试/*.html

要访问不带扩展名的页面路由路径名,即 /privacy,该格式应包含在 try_files insdie location /

尝试这个:

try_files $uri $uri.html /$uri /index.html

【讨论】:

    【解决方案2】:

    由于 NextJS 在 url 中执行 id 的方式,我需要第二个位置块。 NextJS 将包含[id].html 之类的文件。

        location / {
            try_files $uri $uri.html /$uri /index.html;
        }
    
        location ~* /(.*)(d+)$ {
            try_files $1/[id].html /$1/[id].html /index.html;
        }
    

    所以我需要第二个块来捕获/whatever/etc/5 形式的 url 并将 nginx 重定向到 /whatever/etc/[id].html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      相关资源
      最近更新 更多