域名二级目录 指向 nextjs 应用

摘要:应用场景: 考虑到多应用在一个域名下能提高该域名的SEO,所以选择通过域名二级目录形式指向 nextjs应用,这里需要修改 nginx 和 nextjs 配置
应用场景: 考虑到多应用在一个域名下能提高该域名的SEO,所以选择通过域名二级目录形式指向 nextjs应用,这里需要修改 nginx 和 nextjs 配置


条件假设:

www.helloworld.com/nextjs 指向 nextjs 目录

nextjs 端口 3000

pm2 管理production 环境


预期效果:

nextjs 路由跳转都在 www.helloworld.com/nextjs 下跳转

image 静态图片资源 src 都为 www.helloworld.com/nextjs 路劲下

node 层 页面指向都能在 www.helloworld.com/nextjs 下跳转

区分开 production 环境和 dev 环境,dev 环境下 不配置nextjs 目录


使用技术:

nginx (映射3000)

pm2 (pm2 管理 next production)

nextjs (next.config.js 配置修改)


nginx 修改

www.helloworld.com/nextjs 需要重定向 到 nextjs 3000端口 并且 一系列js css image等资源请求都指向 3000 端口

修改nginx.conf 指向next 应用

 server {
        listen       80;
        server_name   www.helloworld.com/nextjs ;
         location / {
            root   html;
            index  index.html index.htm;
        }
       location /nextjs/ {
            proxy_pass    http://127.0.0.1:3000/; #node.js port
            proxy_http_version 1.1;
            proxy_set_header Host $host;
        }
    }


pm2 配置

配置环境 区分开发环境和生成环境

全局安装 pm2 : npm install pm2 -G

nextjs 目录下创建 ecosyste.config.js并配置

module.exports = {
  apps: [
    {
      name: "nextjs", //pm2 name
      script: "./server.js", //node 启动 (node start )
      cwd: "./", // 当前工作路径
      watch: [
        ".next", // 监控变化的目录,一旦变化,自动重启
      ],
      ignore_watch: [
        // 从监控目录中排除
        "node_modules",
        "logs",
        "static",
      ],
      instances: 1,
      node_args: "--harmony",
      env: {
        NODE_ENV: "development",
        PORT: 3006, //端口
      },
      env_production: {
        NODE_ENV: "production",
        PORT: 3000, //端口
        BASE_PATH: "nextjs",
      },
      out_file: "./logs/out.log", // 普通日志路径
      error_file: "./logs/err.log", // 错误日志路径
      merge_logs: true,
      log_date_format: "YYYY-MM-DD HH:mm Z", // 设置日志的日期格式
      autorestart: true,
      watch: false,
      max_memory_restart: "1G",
    },
  ],
};

3.添加 npm scripts 生成启动 脚本, 修改package.json

...
 "scripts" : {
     ....
	 "build": "next build",
     'production' : 'pm2 start ecosystem.config.js --env production'
     ....
 }
...


next 修改

修改next.config.js , next 配置文件

封装 路径,达到不同环境返回不同basePath

const isProd = process.env.NODE_ENV === "production";

function getBasePath() {
  var basePath = "";

  if (isProd && process.env.BASE_PATH) {
    if (process.env.BASE_PATH.startsWith("/")) {
      basePath = process.env.BASE_PATH;
    } else {
      basePath = "/" + process.env.BASE_PATH;
    }
  }

  return basePath;
}

更改next.config.js配置

....
module.exports = {
 assetPrefix: getBasePath(), //加前缀
 basePath: getBasePath(), //node 
 webpack(webpackConfig) {
    webpackConfig.output.publicPath =
      getBasePath() + webpackConfig.output.publicPath; //资源生成前缀
    return webpackConfig;
  },
}
publicRuntimeConfig: {
    basePath: getBasePath(), //写入路径
  },
....

3.image 静态 路径

import getConfig from "next/config";
const { publicRuntimeConfig } = getConfig();

// image 标签 url 为image 的路劲
<img src =`${publicRuntimeConfig.basePath + url}` />

4.Link 路劲处理

import getConfig from "next/config";
const { publicRuntimeConfig } = getConfig();

//Link 标签 href 跳转 
 <Link href=`${publicRuntimeConfig.basePath }index`>
                    <a className="more">跳转</a>
 </Link>

打包+生成

next 打包 : npm run build

production 启动 项目: npm run production

pm2 日志查看 : pm2 logs

pm2 项目监看 : pm2 list

pm2 重启: pm2 reload all



本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!

链接: https://shenqiku.cn/article/FLY_8865