【问题标题】:Next.js change directly from url not workingNext.js 直接从 url 更改不起作用
【发布时间】:2022-04-05 21:26:05
【问题描述】:

Next.js 版本 10.0.6 - 节点版本 12.18.4 - Linux 18.04

在我的服务器上,我试图将 URL 直接更改为 about 页面(通过键入 URL - 而不是单击链接),但它给了我一个 502 错误网关。任何复制粘贴到 URL 都不起作用。当我点击“关于”链接时,只有在我的导航栏中才能导航到关于页面。

例子:

www.example.com/about

在我的 PC 上,开发模式下的操作运行良好。问题出在我的服务器上。

我的部署步骤:

package.json 文件:

"scripts": {
    "dev": "next dev",
    "build": "next build && next export",
    "start": "next start"
  },

npm run build

我从“out”文件夹中取出所有文件并将它们放在我的服务器上。

nginx 配置文件:

server {
    listen myip:80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;

location / {
 proxy_pass http://localhost:3000;
 proxy_http_version 1.1;
 proxy_set_header Upgrade $http_upgrade;
 proxy_set_header Connection 'upgrade';
 proxy_set_header Host $host;
 proxy_cache_bypass $http_upgrade;
 }

location ~ /.well-known {
  allow all;
}

 location ~* "/\.(htaccess|htpasswd)$" {
        deny    all;
        return  404;
    }

    location /vstats/ {
        alias   /home/admin/web/example.com/stats/;
        include /home/admin/conf/web/example.com.auth*;
    }

    include     /etc/nginx/conf.d/phpmyadmin.inc*;
    include     /etc/nginx/conf.d/phppgadmin.inc*;
    include     /etc/nginx/conf.d/webmail.inc*;

    include     /home/admin/conf/web/nginx.example.com.conf*;
}

我重启Nginx的服务,启动pm2。

sudo service nginx restart

pm2 start npm --name "next" -- start

我做错了什么?我的配置正确吗?

如果您需要任何其他信息,请随时问我。

【问题讨论】:

  • 在 URL 末尾添加.html 会使页面正常工作吗?
  • 不,它不起作用

标签: nginx next.js


【解决方案1】:

您可以将trailingSlash: true 添加到您的next.config.js,以便/about 可以路由到HTML 文件。这是 Next.js 9 之前的默认行为,但现在需要显式启用。

// next.config.js

module.exports = {
    trailingSlash: true
}

【讨论】:

  • 这行不通。现在整个网站都挂了。尝试重新启动服务器和 pm2 但没有。
  • 很公平。您的问题可能需要nginx 解决方案,遗憾的是我并不熟悉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
相关资源
最近更新 更多