英文:
Nextjs Custom HTTPs Server is Slow
问题
我正在尝试在生产环境中运行一个Next.js网站,但网站加载非常慢(每个页面加载约8秒)。这与开发环境形成鲜明对比,在开发环境中,每个页面平均加载约0.4秒。
我正在使用自定义的Next.js服务器来使用HTTPS而不是HTTP。
以下是我的服务器代码:
const http = require('http');
const https = require('https');
const fs = require('fs');
const next = require('next');
const app = next({
dev: process.env.NODE_ENV !== 'production'
});
const handle = app.getRequestHandler();
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/eliotfisk.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/eliotfisk.com/fullchain.pem')
};
app.prepare().then(() => {
https.createServer(options, (req, res) => {
handle(req, res)
}).listen(443, (err) => {
if (err) throw err
console.log('> Ready on https://localhost:443')
})
http.createServer((req, res) => {
res.writeHead(301, {
Location: `https://${req.headers.host}${req.url}`
})
res.end();
}).listen(80, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:8080')
})
})
这是否仅仅是我的服务器硬件本身的限制?我正在使用DigitalOcean VPS来运行这个服务器。
另外,我觉得这与我的服务器的构建有关,因为当我运行server.js
时,它说正在编译页面,我觉得这应该在我已经创建的构建中。
这是日志的一个示例:
joey@WebsiteDroplet:~/Website$ sudo node server
info - SWC minify release candidate enabled. https://nextjs.link/swcmin
event - compiled client and server successfully in 2s (196 modules)
wait - compiling...
event - compiled client and server successfully in 292 ms (196 modules)
> Ready on https://localhost:443
> Ready on http://localhost:8080
wait - compiling /_error (client and server)...
event - compiled client and server successfully in 436 ms (197 modules)
wait - compiling /about (client and server)...
event - compiled client and server successfully in 486 ms (205 modules)
英文:
I am trying to run a Next.js website in production, but the website is really slow to load (~8 seconds to load each page). This is in stark contrast to development, where each page takes ~0.4 seconds to load on average.
I am using a custom server with Next.js to use HTTPS instead of HTTP.
Here is my server code:
const http = require('http');
const https = require('https');
const fs = require('fs');
const next = require('next');
const app = next({
dev: process.env.NODE_ENV !== 'production'
});
const handle = app.getRequestHandler();
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/eliotfisk.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/eliotfisk.com/fullchain.pem')
};
app.prepare().then(() => {
https.createServer(options, (req, res) => {
handle(req, res)
}).listen(443, (err) => {
if (err) throw err
console.log('> Ready on https://localhost:443')
})
http.createServer((req, res) => {
res.writeHead(301, {
Location: `https://${req.headers.host}${req.url}`
})
res.end();
}).listen(80, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:8080')
})
})
Is this just a limitation of my server hardware itself? I am using a DigitalOcean VPS to run this server.
Also, I feel like this has to do with the build of my server, because when I run server.js
, it says that it is compiling the pages, and I feel like this should be in the build I already created.
I have already ran next build
, but I don't think the custom server is using this build.
Here is an example of the logs:
joey@WebsiteDroplet:~/Website$ sudo node server
info - SWC minify release candidate enabled. https://nextjs.link/swcmin
event - compiled client and server successfully in 2s (196 modules)
wait - compiling...
event - compiled client and server successfully in 292 ms (196 modules)
> Ready on https://localhost:443
> Ready on http://localhost:8080
wait - compiling /_error (client and server)...
event - compiled client and server successfully in 436 ms (197 modules)
wait - compiling /about (client and server)...
event - compiled client and server successfully in 486 ms (205 modules)
答案1
得分: 0
当我运行自定义服务器时,在以下行中,next
函数中的 dev
选项被意外地设置为 true
:
const app = next({
dev: process.env.NODE_ENV !== 'production'
});
对于生产环境,我在 next.config.js
文件中如此设置环境变量:
module.exports = {
env: {
NODE_ENV: 'production',
},
}
如Next.js文档中所述。
英文:
When I was running the custom server, the dev
option in the next
function was unintentionally set to true in the line:
const app = next({
dev: process.env.NODE_ENV !== 'production'
});
For production, I set the environment variable in the next.config.js
file like so:
module.exports = {
env: {
NODE_ENV: 'production',
},
}
As documented on the Next.js docs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论