在SSR模式下发布我的nuxtjs网站时出错

时间:2018-02-11 21:59:23

标签: node.js nginx nuxt.js

我在尝试发布nuxtjs网站时遇到了一些问题。通常情况下,我使用的是generate命令,但是对于这个命令,我需要完整的SSR,所以我要开始使用nuxt。

但是在构建和启动应用程序之后,这是一团糟。构建在控制台中完美运行,应用程序启动。问题是当我尝试访问该网站时,它会部分加载,但我在浏览器中收到了所有这些错误:

manifest.3a7efd91c5f63f114507.js
Failed to load resource: the server responded with a status of 404 ()
vendor.7519259bf7bdf608079e.js
Failed to load resource: the server responded with a status of 404 ()
app.a5cb9356f53306f973dc.js
Failed to load resource: the server responded with a status of 404 ()
default.1f3ad14df16ee86595af.js
Failed to load resource: the server responded with a status of 404 ()
index.260dc65b69022a31ad58.js
Failed to load resource: the server responded with a status of 404 ()
/_nuxt/pages/spot/_slug.e57cc2e78d8e0b160fe7.js
Failed to load resource: the server responded with a status of 404 ()
manifest.3a7efd91c5f63f114507.js
Failed to load resource: the server responded with a status of 404 ()
default.1f3ad14df16ee86595af.js
Failed to load resource: the server responded with a status of 404 ()
index.260dc65b69022a31ad58.js
Failed to load resource: the server responded with a status of 404 ()
vendor.7519259bf7bdf608079e.js
Failed to load resource: the server responded with a status of 404 ()
app.a5cb9356f53306f973dc.js
Failed to load resource: the server responded with a status of 404 ()

构建过程中似乎没有错。当我使用nuxt start时,我得到了这个:

$ nuxt start
  nuxt:axios BaseURL: http://localhost:3042/api (Browser: /api) +0ms

  OPEN  http://localhost:3042

这是我的服务器配置文件:

# Site global
server {
    listen 443 ssl http2;
    server_name www.mywebsite.com;
    access_log  off;

    location = /robots.txt  { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }
    location / {
        proxy_pass http://127.0.0.1:3042/;
        include /etc/nginx/conf.d/proxy.conf;
        root /var/www/mywebsite/site;
        add_header Access-Control-Allow-Origin *;
    }

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|txt|srt|swf)$ {
        root /var/www/mywebsite/site/;
        expires 30d;
    }

    ssl_certificate /etc/letsencrypt/live/www.mywebsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.mywebsite.com/privkey.pem;
}

# Redirection
server {
    listen 80;
    server_name mywebsite.com www.mywebsite.com;

    location / {
        return 301 https://www.mywebsite.com$request_uri;
    }
}

我的nuxt配置文件:

    const pkg = require('./package')

module.exports = {
    mode: 'universal',

    loading: { color: '#bb2b4d' },

    router: {
        linkActiveClass: '-active',
        base: '/'
    },

    css: ['@/assets/icons/css/icons.css', '@/assets/snickles/snickles.css'],

    plugins: ['~plugins/vue-filters.js', '~plugins/vue-modal.js'],

    minify: {
        removeEmptyAttributes: false,
        collapseWhitespace: true,
        conservativeCollapse: true,
        collapseBooleanAttributes: true,
        removeTagWhitespace: false,
        removeStyleLinkTypeAttributes: true
    },

    modules: [

        '@nuxtjs/axios'
    ],

    axios: {

    },

    env: {
        api: {
            spots: `https://rest.mywebsite.com/spots`
        }
    },

    proxy: {
    },

    build: {
        extend(config, ctx) {
            // Run ESLint on save
            if (ctx.isDev && ctx.isClient) {
                config.module.rules.push({
                    enforce: 'pre',
                    test: /\.(js|vue)$/,
                    loader: 'eslint-loader',
                    exclude: /(node_modules)/
                })
            }
        }
    },

    postcss: [require('autoprefixer')],

    vendor: ['moment', 'vue-js-modal']
}

我忘了什么吗? 最奇怪的是,当我在自己的电脑上而不是在我的服务器上做同样的事情时,它的效果非常好。我检查了npm和节点版本,它们是相同的(最新的)。此外,如果使用NuxtJS中的演示模板进行测试,则它与完全相同的服务器配置完美配合。

顺便说一下,服务器是debian 8,所有包都是最新的。

提前感谢任何提示。

编辑:如有任何用处,请输入错误日志:

2018/02/14 19:12:54 [error] 12981#12981: *239930 open() "/var/www/mywebsite/site/_nuxt/pages/spot/_slug.e57cc2e78d8e0b160fe7.js" failed (2: No such file or directory), client: xxx.xxx.xxx.xxx., server: www.mywebsite.com, request: "GET /_nuxt/pages/spot/_slug.e57cc2e78d8e0b160fe7.js HTTP/2.0", host: "www.mywebsite.com", referrer: "https://www.mywebsite.com/"
2018/02/14 19:12:57 [error] 12981#12981: *239930 open() "/var/www/mywebsite/site/_nuxt/manifest.3a7efd91c5f63f114507.js" failed (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: www.mywebsite.com, request: "GET /_nuxt/manifest.3a7efd91c5f63f114507.js HTTP/2.0", host: "www.mywebsite.com", referrer: "https://www.mywebsite.com/"
2018/02/14 19:12:57 [error] 12981#12981: *239930 open() "/var/www/mywebsite/site/_nuxt/vendor.7519259bf7bdf608079e.js" failed (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: www.mywebsite.com, request: "GET /_nuxt/vendor.7519259bf7bdf608079e.js HTTP/2.0", host: "www.mywebsite.com", referrer: "https://www.mywebsite.com/"
2018/02/14 19:12:57 [error] 12981#12981: *239930 open() "/var/www/mywebsite/site/_nuxt/app.a5cb9356f53306f973dc.js" failed (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: www.mywebsite.com, request: "GET /_nuxt/app.a5cb9356f53306f973dc.js HTTP/2.0", host: "www.mywebsite.com", referrer: "https://www.mywebsite.com/"

同样,它与其他nuxt项目完美搭配,具有类似的配置。实际上它无法在这个文件夹中找到这些文件,因为它们不在其中 - 这是完全正常的。应用程序可以通过相同的目录输出来获取这些文件的路径,这通常做得很好(正如我所说,它应该不在_nuxt文件夹中)。

感谢。

1 个答案:

答案 0 :(得分:3)

这是用nginx标记的,因此,这是解决问题的nginx方法。

通过评论进行问题排查后,您报告在error_log

中收到以下内容:

2018/02/14 19:12:57 [error] 12981#12981: *239930 open() "/var/www/mywebsite/site/_nuxt/manifest.3a7efd91c5f63f114507.js" failed (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: www.mywebsite.com, request: "GET /_nuxt/manifest.3a7efd91c5f63f114507.js HTTP/2.0", host: "www.mywebsite.com", referrer: "https://www.mywebsite.com/"

随后,运行find / -type f -name manifest.3a7efd91c5f63f114507.js或类似内容会导致相应的文件位于/var/www/mywebsite/site/.nuxt/dist

因此,您的nginx配置错误,因为您在不正确的文件夹中查找这些内容 - 您的配置改为root /var/www/mywebsite/site/;

正确的方式 可能会使用基于前缀的locationalias指令:

location /_nuxt/ {
    alias /var/www/mywebsite/site/.nuxt/dist/;
}

但是,如果/_nuxt/的内容可能必须proxy_pass到上游,并且您希望继续使用基于pcrelocation你已经有了原始配置,那么下面的 替代解决方案 也是一个选项(否则,你显然必须将其删除为冗余,以确保先前基于前缀的位置有效,请参阅http://nginx.org/r/location):

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|txt|srt|swf)$ {
    rewrite ^/_nuxt(/.*) $1 break; 
    root /var/www/mywebsite/site/.nuxt/dist/;
}