欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Nginx和pm2部署Next.js項(xiàng)目

 更新時間:2023年05月23日 16:06:46   作者:T.Y.Bao  
本文主要介紹了使用Nginx和pm2部署Next.js項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

概述

只有一臺服務(wù)器,所以上圖服務(wù)都都在一個云服務(wù)器上。其中Nginx 分別在用戶和Next服務(wù)之間代理、在Next和后臺之間代理。

常規(guī)的前臺頁面不需要這樣做,例如Vue中直接把build之后的dist文件拷貝到nginxhtml目錄并配置nginx指向即可,但是Next可以做到服務(wù)端渲染(SSR)所以Next的前臺頁面實(shí)際上是一個nodejs服務(wù),所以nginx在這里是代理用戶請求,proxy_pass到這個nodejs服務(wù)上。

而前后臺之間的nginx代理屬于反向代理,一般也通過proxy_passrewrite路徑進(jìn)行代理,我沒配置這個。

Next.js配置

在需要SSRpage中需要添加 getStaticPropsgetStaticProps這些function只能寫在page文件夾中,不可以在components中用),注意可以設(shè)置revalidate定時重新build頁面,這樣頁面也可以定期更新,如下:

export async function getStaticProps() {
	//后臺取數(shù)據(jù)
    const result = await queryTimelineList()
    // console.log(result)
    return {
        props: {
            timelineData: result.data,
        },
        // 重新繪制頁面時長 1200 ,單位秒
        revalidate: 1200
    };
}

另外,注意在動態(tài)路由的page中,需要設(shè)置fallbacktrue,且頁面中要增加對fallbackfalse的處理,否則build的時候會報錯,如下 一個名為 [postId].js的page:

const PostId = ({postId, postDetail, recentPosts,curTags}) => {
    const router = useRouter()
	// 注意,這里要處理
    if (router.isFallback) {
        return <div>Loading...</div>
    }
    ...
}
// Fetch data at build time
export async function getStaticProps(context) {
    const postId = context.params.postId
    const
        [postDetailResult,
            recentPostsResult,
            curPostTagsResult] = await Promise.all([
                queryPostDetailByPostId(postId),
                queryRecentPostList(),
                queryTagListByPostId(postId)
            ]
        )
    return {
        props: {
            postId: postId,
            postDetail: postDetailResult?.data,
            recentPosts: recentPostsResult?.data,
            curTags: curPostTagsResult?.data
        },
        revalidate: 300
    };
}
// Specify dynamic routes to pre-render pages based on data.
// The HTML is generated at build time and will be reused on each request.
export async function getStaticPaths() {
    const postIdsResult = await queryPostIdList();
    const postIdList = postIdsResult.data
    return {
        paths: postIdList.map((postId) => {
        	// 這里如果傳的是數(shù)字會報錯,必須轉(zhuǎn)為字符串
            return {params: {postId: postId.toString()}}
        }),
        // 設(shè)置為true
        fallback: true,
    };
}

Nginx配置

完整的nginx.conf配置文件如下:

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  btyhub.site, www.btyhub.site;
		# ssl兩個文件,放在 nginx的conf目錄中
        ssl_certificate      btyhub.site_bundle.pem;
        ssl_certificate_key  btyhub.site.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
		# 代理到Next的服務(wù),默認(rèn)3000端口,也可以在start的時候指定
        location / {
            proxy_pass    http://127.0.0.1:3000/;
        }
    }
    # 監(jiān)聽普通http請求,重寫至https
	server{
		listen 80;
		server_name btyhub.site, www.btyhub.site;
		rewrite ^(.*)$ https://$host$1 permanent;
	}
}

pm2啟動

ps:除了使用pm2啟動Next服務(wù)外,也可以npm run build之后直接nohup npm run start。

pm2官網(wǎng)文檔

首先要安裝node環(huán)境,安裝完了將命令添加到全局bin中:

# /usr/local/node是我的node安裝目錄
ln -s /usr/local/node/bin/npm /usr/local/bin/
ln -s /usr/local/node/bin/npx /usr/local/bin/
ln -s /usr/local/node/bin/node /usr/local/bin/

其次,全局安裝pm2 并加入全局bin:

npm install -g pm2
ln -s /usr/local/node/bin/pm2 /usr/local/bin/

測試,是否安裝成功:

pm2 -version

運(yùn)行next服務(wù)

需要將項(xiàng)目源文件上傳至云服務(wù)器:

運(yùn)行npm run build

運(yùn)行pm2 start --name yourappname npm -- start

后記:

域名配置完要隔幾天才可以給網(wǎng)站備案,備案之后才可以通過域名訪問,在這之前可以先用ip地址測試。

到此這篇關(guān)于使用Nginx和pm2部署Next.js項(xiàng)目的文章就介紹到這了,更多相關(guān)Nginx和pm2部署Next.js內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論