nuxt / nuxt

The Intuitive Vue Framework.
https://nuxt.com
MIT License
53.03k stars 4.87k forks source link

Nginx location rules doesn't work with preset: 'service-worker' #21275

Open ork821 opened 1 year ago

ork821 commented 1 year ago

Environment

Reproduction

  1. Build nuxt project with ssr: false and nitro.preset: 'service-worker'

  2. Configure nginx config like ` server {

    server_name site.ru www.site.ru;
    
    root   /var/www/site/public;
    index index.html;
    
    location /api/ {
            proxy_pass http://localhost:3000/api;
    }
    
    location / {
            try_files $uri $uri/ @rewrites;
    }
    
    location @rewrites {
            rewrite ^(.+)$ /index.html last;
    }
    access_log /var/log/nginx/vue-app-access.log;
    error_log  /var/log/nginx/vue-app-error.log;

    } `

  3. If app fetches http://site.ru/api/auth, then I get html, but not correct response from http://localhost:3000/api

Describe the bug

Hello eveeryone!

I use nginx as my reverse proxy to get access to my frontend (which is nuxt3 built with nitro.preset: 'service-worker') and some backend on localhost:3000.

When my app tries to get "/api/auth", I get response with html content (I think this happens from serviceworker).

Is there any ways to exclude paths "/api/....." from routing inside serviceWorker?

P.S. If I use default nitro.preset and use proxy_pass for my frontend, it works great. So nginx routing also works correctly

Additional context

Logs

No response

Hebilicious commented 1 year ago

I think the service worker preset wouldn't work in your case as you're serving your Nuxt app with node. Have you tried one of the node presets ? https://nitro.unjs.io/deploy/node