首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Next-Pwa在Nginx等生产服务器上不起作用

Next-Pwa在Nginx等生产服务器上不起作用
EN

Stack Overflow用户
提问于 2020-10-23 13:33:35
回答 2查看 2.9K关注 0票数 2

我正在工作的Next.js示例应用程序喜欢下面的链接

https://github.com/vercel/next.js/tree/canary/examples/progressive-web-app

这是我的next.config.js

代码语言:javascript
复制
const withPWA = require('next-pwa')
const runtimeCaching = require('next-pwa/cache')

module.exports = withPWA({
    pwa: {
        dest: 'public',
        register: true,
        runtimeCaching,
    }
})

这是manifest.json

代码语言:javascript
复制
{
  "name": "nex-pwa",
  "short_name": "app",
  "display": "fullscreen",
  "orientation": "portrait",
  "theme_color": "#3056de",
  "background_color": "#3056de",
  "scope": "/",
  "start_url": "/",
  "icons": [
    {
      "src": "/icons/android-chrome-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "/icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    },
    {
      "src": "/icons/homescreen48.png",
      "sizes": "48x48",
      "type": "image/png"
    }, {
      "src": "/icons/homescreen72.png",
      "sizes": "72x72",
      "type": "image/png"
    }, {
      "src": "/icons/homescreen96.png",
      "sizes": "96x96",
      "type": "image/png"
    }, {
      "src": "/icons/homescreen144.png",
      "sizes": "144x144",
      "type": "image/png"
    }
  ],
  "splash_pages": null
}

还有一个Nginx server文件

代码语言:javascript
复制
server 
{
    root /var/www/domain.com/html/pwa;

    server_name domain.com www.domain.com;

    error_log /var/www/domain.com/html/pwa/log/error.log;

    location ~/images(.*$) { 
        proxy_pass http://localhost:3035/images$1; 
        proxy_redirect off; 
    }

    location ~/fonts(.*$) { 
        proxy_pass http://localhost:3035/fonts$1; 
        proxy_redirect off; 
    }

    location ~/icons(.*$) { 
        proxy_pass http://localhost:3035/icons$1; 
        proxy_redirect off; 
    }

    location ~/sw.js(.*$) { 
        proxy_pass http://localhost:3035/sw.js$1; 
        proxy_redirect off; 
    }

    location ~/workbox-c2b5e142.js(.*$) { 
        proxy_pass http://localhost:3035/workbox-c2b5e142.js$1; 
        proxy_redirect off; 
    }

    location ~/_next(.*)$ {
        proxy_pass http://localhost:3035/_next$1;
        proxy_redirect off;
    }

    location / {
        proxy_pass http://localhost:3035;
        proxy_redirect off;
    }

    location ~ /\.ht {
        deny all;
    }
}

它在本地开发服务器上工作,但当我使用nginx部署到像DigitalOcean这样的生产环境时,它不再工作了,我的意思是web应用程序工作,但安装图标在浏览器上不显示。

我在这里做错了什么。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-31 12:38:18

我曾经遇到过这种错误,但我在这个阶段已经克服了&准确地分享了我的配置,这些配置在我的站点和使用Nginx的生产服务器上工作得很好。

第一步:我发现你的manifest.json文件没问题。

步骤2:在next.config.js

代码语言:javascript
复制
const withPWA = require('next-pwa')

module.exports = withPWA({
    pwa: {
        dest: 'public'
    }
})

保存并运行/重新启动开发服务器,如npm run dev,然后它将生成sw.js & workbox-*.js如果生成了这些文件,则再次停止开发服务器并打开next.config.js,如下所示更新文件

代码语言:javascript
复制
module.exports = withPWA({
    pwa: {
        disable: process.env.NODE_ENV === 'development',
        // dest: 'public', // comment out this line
        register: true,
        sw: '/sw.js'
    }
})

现在项目上传到服务器& Nginx服务器更新,但我看到server文件没问题,只需根据您的文件更新这一部分

代码语言:javascript
复制
location ~/workbox-c2b5e142.js(.*$) {  // from public folder
    proxy_pass http://localhost:3035/workbox-c2b5e142.js$1; 
    proxy_redirect off; 
}

然后重新启动服务器、构建项目、重新启动pm2 &就是这样。

我想它会起作用的。

如果你有任何困惑,请告诉我。

谢谢

票数 2
EN

Stack Overflow用户

发布于 2020-10-31 05:41:04

您在评论中提到的控制台错误提到了/css/animate.min.css,并为该文件显示了一个404错误...此外,您的nginx配置似乎没有/css规则。也许你需要加上这一点,比如:

代码语言:javascript
复制
server 
{
    root /var/www/domain.com/html/pwa;

    server_name domain.com www.domain.com;

    error_log /var/www/domain.com/html/pwa/log/error.log;

    location ~/css(.*$) { 
        proxy_pass http://localhost:3035/css$1; 
        proxy_redirect off; 
    }

    # ... your other location rules
}

也就是说,我想知道为什么您要代理将每个目录分别传递到相同的目的地。为什么不在静态目录上使用try_files,只使用服务器/next实际需要呈现的代理调用。我猜localhost:3035是你的节点/下一台服务器?您可以尝试如下所示:

代码语言:javascript
复制
server 
{
    root /var/www/domain.com/html/pwa;

    server_name domain.com www.domain.com;

    error_log /var/www/domain.com/html/pwa/log/error.log;

    location / {
        root /var/www/domain.com/html/pwa/static;
        index index.html;
        try_files $uri $uri/ @proxy;
    }

    location @proxy {
        proxy_pass http://localhost:3035;
        proxy_redirect off;
    }

    location ~ /\.ht {
        deny all;
    }
}

这假设您的构建过程生成的静态文件位于/var/www/domain.com/html/pwa/static中,如果不是,请将location /中的root设置调整为您的image/fonts/等实际所在的根文件夹。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64494398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档