nuxt 服务端部署

1、打包,打包完把下面文件夹挪到服务器器上

// 打包
npm run build

在这里插入图片描述

// nuxt.config.js
module.exports = {
    mode: 'universal',
    /*
    ** Headers of the page
    */
    head: {
      title: process.env.npm_package_name || '商盟',
      meta: [
        { charset: 'utf-8' },
        { name: 'viewport', content: 'width=device-width, initial-scale=1' },
        { hid: 'description', name: 'description', content: process.env.npm_package_description || '' }
      ],
      link: [
        { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
      ]
    },
    /*
    ** Customize the progress-bar color
    */
    loading: { color: '#fff' },
    /*
    ** Global CSS
    */
    css: [
      'element-ui/lib/theme-chalk/index.css',
      '~/assets/styles/index.scss'
    ],
    /*
    ** Plugins to load before mounting the App
    */
    plugins: [
      '@/plugins/element-ui'
    ],
    /*
    ** Nuxt.js dev-modules
    */
    devModules: [
    ],
    /*
    ** Nuxt.js modules
    */
    modules: [
      '@nuxtjs/axios'
    ],
    axios: {
      proxy: true
    },
    proxy: {
      '/saas-pc': {
        target: 'http://localhost:8083',
        // pathRewrite: { '^/api': '' }
      }
    },
    /*
    ** Build configuration
    */
    build: {
      publicPath: '/saas/',   // 如果在这里指定了其他的,那么生产环境打包配置的无效果
      transpile: [/^element-ui/],
      /*
      ** You can extend webpack config here
      */
      extend(config, ctx) {
      }
    }
}

注意:publicPath,如果在这里配置了,那要跟打包前的文件名对应,否则js、图片访问不到,要么把这里配置的去掉

// package.json
{
  "name": "nuxt114",
  "version": "1.0.0",
  "description": "pc-project",
  "author": "ffc",
  "private": true,
  "scripts": {
    "build": "nuxt build && npm start",
    "start": "nuxt start"
  },
  "dependencies": {
    "cross-env": "^5.2.0",
    "koa": "^2.6.2",
    "element-ui": "^2.4.11",
    "nuxt": "^2.0.0"
  },
  "devDependencies": {
    "@nuxtjs/axios": "^5.5.4",  // 这些都必须要
    "@nuxtjs/proxy": "^1.3.3",
    "node-sass": "^4.12.0",
    "nodemon": "^1.18.9",
    "sass-loader": "^7.1.0"
  }
}

2、启动

npm run start

3、nignix配置

#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;
    gzip            on;
    gzip_min_length 1000;
    gzip_proxied    expired no-cache no-store private auth;
    gzip_types      text/plain application/x-javascript application/javascript text/css application/xml text/javascript 

application/x-httpd-php image/jpeg image/gif image/png;

   upstream backa {

       #ip_hash;

       server 127.0.0.1:8084;        

    }

   upstream backb {

       server 127.0.0.1:3000;   
       keepalive 64;   

    }

    server {
        listen       80;
        server_name  a.xxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #     index  index.html index.htm;
        # }
  
	location /saas-pc/api {
	     proxy_set_header Host $host;
	     proxy_set_header X-Real-IP $remote_addr;
	     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	     proxy_buffering off; 
	     proxy_pass http://backa;     
	}

	location / {  
	     proxy_http_version 1.1;
	     proxy_set_header Upgrade $http_upgrade;
	     proxy_set_header Connection "upgrade";
	     proxy_set_header Host $host;
	     proxy_set_header X-Nginx-Proxy true;
	     proxy_cache_bypass $http_upgrade;
	     proxy_pass http://backb;     
	  
	}


        #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;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么要学习服务渲染 nuxt.js ? 现在我们的项目大多数都是SPA(单页面应用),在实际开发过程中单页面应用比之前的模板渲染要好很多,首先单页面应用是前后分离,架构清晰,前负责交互逻辑,后负责数据,前后单独开发,独立测试。但是,SPA不利于SEO(搜索引擎优化)。让搜索引擎更为信任该网站,通过提升排名获得更多网站流量,对于某些类型的网站是非常有必要的。目前大部分的Vue项目本质是 SPA 应用,React、Angular也都是SPA应用。SPA应用广泛用于对SEO要求不高的场景中。在我们开发的过程中,我们有 SEO 的需求,我们需要搜索引擎更多地抓取到我们的项目内容,此时我们需要SSR。SSR保证用户尽快看到基本的内容,也使得用户体验性更好。 Nuxt.js 是一个 Node 程序,基于vue.js开发的一套服务渲染的框架,必须使用 Node 环境。我们对 Nuxt.js 应用的访问,实际上是在访问这个 Node.js 程序的路由,程序输出首屏渲染内容 + 用以重新渲染的 SPA 的脚本代码,而路由是由 Nuxt.js 约定好的 pages 文件夹生成的,开发只需要遵循一定的约定,直接使用vue.js开发我们项目也是非常轻松的。 课程案例 (1) HOME PAGE (2) Jokes Page  (3)About Page  课程概述 在本课程中,大喵将使用 nuxt.js + bootstrapVue + json-server 开发实战性质一个入门级项目,带着大家来体验服务渲染(SSR )项目构建的过程;介绍 nuxt.js项目目录的结构,每个文件夹和文件的基本概念和作用,以及nuxt.config.js 配置文件的基本介绍;页面公共结构处理,路由页面跳转配置处理;axios 接口请求;带着大家来熟悉及掌握 bootstrapVue UI组件库的使用;
Nuxt.js 是一个基于 Vue.js 的服务渲染框架,用于构建同构应用程序,可以帮助我们快速构建高质量的 Vue 应用程序。下面是 Nuxt.js 部署上线的详细过程: 1. 安装 Node.js 和 npm:在你的服务器上安装 Node.js 和 npm,如果已经安装则跳过此步骤。 2. 安装 PM2:PM2 是一个 Node.js 进程管理器,可以帮助我们管理和监控应用程序。使用以下命令安装 PM2: ``` npm install pm2 -g ``` 3. 构建应用程序:在本地使用以下命令构建应用程序: ``` npm run build ``` 这个命令将会把我们的应用程序打包成静态文件。 4. 启动应用程序:使用以下命令启动应用程序: ``` pm2 start npm --name "app-name" -- run start ``` app-name 是你的应用程序的名称。这个命令将会在后台启动你的应用程序,如果你需要停止应用程序,可以使用以下命令: ``` pm2 stop app-name ``` 5. 配置 Nginx:Nginx 是一个高性能的 HTTP 和反向代理服务器,可以帮助我们实现负载均衡和静态文件的缓存。使用以下命令安装 Nginx: ``` sudo apt-get install nginx ``` 安装完成后,需要修改 Nginx 的配置文件,添加以下内容: ``` server { listen 80; server_name example.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://localhost:3000; } } ``` 这个配置文件将会把所有的 HTTP 请求代理到本地的 3000 口,其中 example.com 是你的域名。 6. 启动 Nginx:使用以下命令启动 Nginx: ``` sudo systemctl start nginx ``` 完成以上步骤后,你的 Nuxt.js 应用程序就已经部署上线了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值