vue.js 服務端渲染nuxt.js反向代理nginx部署


 

vue.js的官方介紹里可能提到過nuxt.js,我也不太清楚我怎么找到這個的

最近項目vue.js是主流了,當有些優化需求過來后,vue還是有點力不從心,

比如SEO的優化,由於vue在初始化完成之前,頁面是沒有任何內容的,所以基本上沒有辦法滿足這個需求

比如有些訪問量較大的主頁里面,由於都是異步數據,所以在服務器數據沒有返回之前,可能只能無奈的顯示一個loadding....但是產品沒辦法理解這里面的技術流,他們只知道,能不能更快一點,甚至不用loadding~

我在vue這個環境下能做的:

把vue公共的庫打包為一個js扔cdn上,加快訪問速度的同時,全局訪問一次后,客戶端webview可以從本地緩存直接加載

簡化熱點數據請求次數,盡量在一次請求能完成的情況下一次請求完成

路由懶加載,基本每個頁都是一個單獨的chunk

可是在移動客戶端產品來看,只是加快了一點加載的速度,體驗上還是沒什么改善。

靜態化可能是一種好的選擇,

這里可能不是介紹nuxt的用法,官方中文介紹文檔已經很詳細了:https://zh.nuxtjs.org/

涉及的功能關鍵字:vue-cli, nuxt-express,nginx

我的環境還是windows,所以這里用的nginx for windows(很多人說不行,我只是試一試,何必當真)

下載一個壓縮包,解壓就可以用了,目錄結構(版本:1.13.3)

 

 注意:解壓的文件路徑,不能用中文,否則服務無法啟動

安裝vue-cli

 

npm i vue-cli -D

 

下載模板項目

vue init nuxt/express sexpress

初始化加載包

npm install

編譯

npm run build

到這里模板項目可以嘗試運行一下了

npm run start

瀏覽器http://localhost:3000可以看到結果了

當我們把cmd的窗口關掉后,這個服務就自動停止了,如果需要一個常駐的服務去啟動站點,這里需要安裝一個pm2

npm install pm2 -g

安裝成功后,pm2啟動

-----------------------------------分割線-----------------------------------------

2018年12月10日更新

這篇文章寫得很早,當時還是nuxt的1.0的版本,最近有不少人QQ直接問,所以在此做相應的修改,

現在的PM2啟動命令更改如下

  pm2 start ./node_modules/nuxt/bin/nuxt 

當然,你也可以去這個目錄下面看看這個文件的內容,自己可以寫一個簡單的啟動入口文件...

-----------------------------------分割線-----------------------------------------

修改本地host文件,站點名稱自己定義

127.0.0.1 www.mywebsite1.com

修改nginx配置文件,/conf/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;

    upstream nuxtserver1 {
            server 127.0.0.1:3000;
 }

    server {
   listen 8088;
        server_name  www.mywebsite1.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /{
            proxy_pass   http://nuxtserver1;
            index  index.html index.htm;
 }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

nginx重新啟動

nginx -s reload

nginx的日志文件在/logs下面,如果沒有啟動,可以看看error.log

成功啟動后,瀏覽器輸入http://www.mywebsite1.com:8088/

 

 

 -----------------------------------分割線-----------------------------------------

如果有問題歡迎QQ上直接問我!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM