我找到了一个使用 NGINX 反向代理带有基本路径的 URL 的解决方案。
有用的链接
应用变更
依赖关系
- next-images :为了在使用反向代理时从“公共”导入静态图像
- @zeit/next-css : 为了使用样式表文件
- 以及通常的 NextJS 依赖项
next.config.js
在应用程序的根目录下添加“next.config.js”文件,以便您可以指定“assetPrefix”和“publicRuntimeConfig.basePath”
- assetPrefix : NextJS 在访问组件、样式表、页面等时使用
- publicRuntimeConfig.basePath : 用在 s 中所以指定要添加到链接的前缀,在使用公共图像时用于 "" 元素的 "src" 标签中
例子
const isProd = process.env.NODE_ENV === 'production'
// Enable importing of css stylesheets
const withCSS = require("@zeit/next-css");
const withImages = require('next-images');
/*
* Gets the BASE_PATH from the command used to start this app.
* If BASE_PATH is specified but it does not start with a "/"
* then add it.
*/
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;
}
}
console.log("getBasePath() : isProd = " + isProd);
console.log("getBasePath() : basePath = " + basePath);
return basePath
}
module.exports = withCSS(withImages({
assetPrefix: getBasePath() ,
publicRuntimeConfig: {
basePath: getBasePath() ,
},
}));
静态图片
使用“next-images”来导入图像并在 src 标签中引用导入的对象
更改对静态图像(/public 文件夹中的那些)的任何引用,使其具有基本路径前缀。例如,我的“页脚”组件具有以下内容
import '../stylesheets/main.css';
import img1 from '../public/image_name1.png'
import img2 from '../public/image_name2.png'
export default class o extends React.Component {
render(){
var prefix = publicRuntimeConfig.basePath
return (
<div >
<a className="icon" href="http://www.image_name.com" >
<img src={img1} alt="image_name1"/>
</a>
<a className="icon" href="http://www.image_name2.com">
<img src={img1} alt="image_name2"/>
</a>
</div>
);
}
}
注意:我尝试使用 publicRuntimeConfig.basePath 作为 src URL 的前缀(如下所示),但这在我部署的环境中不起作用(见下文)
import getConfig from 'next/config'
const { publicRuntimeConfig } = getConfig()
...
...
<a className="icon" href="http://www.image_name.com" >
<img src={`${publicRuntimeConfig.basePath}/image_name1.png`} alt="image_name1"/>
</a>
链接
更改您的链接以使用基本路径前缀,例如在我的“标题”组件中,我有以下内容
import Link from 'next/link';
import '../stylesheets/main.css';
import getConfig from 'next/config'
const { publicRuntimeConfig } = getConfig()
const detailId1 = "banana"
const Header = () => (
<div>
<div>
<Link href={`${publicRuntimeConfig.basePath || ''}/`}>
<a className="linkStyle">Home</a>
</Link>
<Link href={`${publicRuntimeConfig.basePath || ''}/about`} >
<a className="linkStyle">About</a>
</Link>
<Link href={`${publicRuntimeConfig.basePath || ''}/details/[id]`}
as= {`${publicRuntimeConfig.basePath || ''}/details/${detailId1}`} >
<a className="linkStyle">Details Var 1</a>
</Link>
</div>
</div>
);
export default Header;
注意:在博客https://levelup.gitconnected.com/deploy-your-nextjs-application-on-a-different-base-path-i-e-not-root-1c4d210cce8a 中,它包含一个“Link.tsx”,它会为您添加前缀,因此您只需使用该链接组件(从“./Link.tsx 导入链接” ";) 而不是 nextJS 版本(从 'next/link' 导入链接;)。但是,当我的链接 URL 中有变量时,“Link.tsx”对我不起作用。
运行你的 nextjs 应用
当您不想要基本路径时在本地运行您的应用程序时,您可以直接运行
npm run dev
由于没有指定 BASE_PATH,您的应用程序应该可以从“http://localhost:3000”访问,并且您的 src 值应该是“/image_name1.png”,当您将鼠标悬停在您的 s 上时,您会看到链接是“http://localhost:3000/pagename”
当您想使用基本路径运行时,请执行以下操作
export BASE_PATH=a/b
npm run dev
注意:由于某种原因,在我的环境中,如果我指定“export BASE_PATH=/a/b”(/ 在路径的开头),我会在路径的开头添加一个文件夹目录。因此,我在没有开始 / 的情况下指定它,并且 next.config.js 中的代码添加了开始 / 如果需要。
您无法通过“http://localhost:3000”访问您的应用,因为您设置了基本路径/assetPrefix/publicRuntimeConfig.basePath。现在你需要一个反向代理。
NGINX:反向代理
我发现最简单的设置是使用 NGINX docker 映像。您需要使用包含重定向到 NextJS 应用程序的配置来运行 NGINX。
创建一个文件夹并在该文件夹中添加一个“default.conf”文件。确保您在“位置”中输入的路径与您在启动 nextjs 应用程序时为 BASE_PATH 指定的路径相同。
server {
listen 80;
server_name localhost;
location /a/b/ {
proxy_pass http://myhost:3000/;
}
}
重要提示:
- 您必须在您的 proxy_pass URL 末尾添加 /,否则其他路径不会传递到您的 NextJS 应用程序中
- 如果您在位置中使用变量,则必须确保包含传递路径
例子
location ~ /a/b/(.*)$ {
set $upstream http://myhost:3000/$1;
proxy_pass $upstream;
}
在该目录的命令提示符下运行 NGINX docker 映像,告诉它使用您的配置文件。
docker run --name mynginx1 -v C:/zNGINX/testnginx/conf:/etc/nginx/conf.d -p 80:80 -d nginx
- docker 容器的名称是“mynginx1”
- v 参数告诉它把你电脑上“C:/zNGINX/testnginx/conf”中的所有文件复制到docker容器的“/etc/nginx/conf.d”目录下。这会将您的“default.conf”复制到 docker 容器中,NGINX 将读取该配置文件。
- 注意:确保您的路径中有“conf.d”作为 docker 位置(“:/etc/nginx/conf.d”),我阅读的博客没有包含这部分,它只指定了“: /etc/nginx/",没有它,图像将无法启动。
- p 参数告诉在端口 80 上运行 NGINX
转到以下网址
http://localhost:80/a/b/
NGINX 会将该 URL 重定向到“http://localhost:3000”。因此,您的应用程序现在应该可以从带有基本路径的 URL 访问。点击 s 应该可以工作,链接应该包含指向 NGINX 的基本路径,该路径会重定向回应用程序,剥离基本路径,留下任何其他路径。
使用 Docker 部署真实世界的服务器
如果您将应用程序部署到服务器,而不是在本地运行,您可以构建应用程序,然后将相关文件/文件夹复制到服务器计算机。确保在构建和运行应用程序时设置了 BASE_PATH
export BASE_PATH=a/b
npm run build
cp package*.json [server_location]
cp next.config.js [server_location]
cp ./next [server_location]
然后在该服务器位置运行
npm install
export BASE_PATH=a/b
npm run start
注意:如果您在应用程序中引用了“公共”图像,请使用“next-images”并导入图像,而不是使用 publicRuntimeConfig.basePath 作为前缀。当我做后者时,找不到图像。有关示例,请参阅关于图像的部分。