-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
document running nuxt dev server behind nginx + SSL + proxy #12003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think it can be prevented if we change connections from |
How does your nginx config look like? |
|
@quartze A few changes have to be made to allow development to run smoothly as expected behind your NGINX Proxy with SSL. Everything below was tested and works properly, so let me know if you encounter any issues.
import { defineNuxtConfig } from "nuxt3";
export default defineNuxtConfig({
vite: {
server: {
hmr: {
protocol: "wss",
clientPort: 443,
path: "hmr/",
},
},
},
});
proxy_http_version 1.1; # required
proxy_set_header Upgrade $http_upgrade; # required
proxy_set_header Connection "upgrade"; # required Add location block to match the hmr path location /_nuxt/hmr/ {
proxy_pass http://localhost:24678;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
} @quartze I went ahead and modified your server block as needed: server {
listen 80;
listen 443 ssl;
server_name dev.domainname.pl;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types
application/atom+xml
application/geo+json
application/javascript
application/x-javascript
application/json
application/ld+json
application/manifest+json
application/rdf+xml
application/rss+xml
application/xhtml+xml
application/xml
font/eot
font/otf
font/ttf
image/svg+xml
text/css
text/javascript
text/plain
text/xml;
# This is a cache for SSL connections
# ssl_session_cache shared:le_nginx_SSL:1m;
# ssl_session_timeout 1440m;
rewrite ^/(.*) /$1 break;
location / {
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_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
proxy_buffering on;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_pass http://localhost:3000;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
}
location /_nuxt/hmr/ {
proxy_pass http://localhost:24678;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
} |
@Diizzayy working perfectly. Thanks for rewriting my code. |
Hey, I had the same issue and this helped me. |
Hi, I had the same issue with docker. I have nginx as proxy for https
and
|
I have added ssl to my server name and nuxt3 was a blank page when using npm run dev.
And also added the " location /_nuxt/hmr/ " in nginx. |
The config that @Diizzayy and @jamesallan93 mentioned was working perfect until RC-7 :( |
@kosmeln I'll have another look at the setup and update you guys accordingly on what changes may need to be made here |
Thank you in advance @Diizzayy ! |
@kosmeln I just got around to checking this out, and the setup I mentioned above works just fine still. Perhaps you can give me more details about your exact setup and what's not working properly |
Sorry for late response @Diizzayy . After upgrade the url changed to So I had to tune vite server.hmr.path setting from
as you suggested earlier to
Still not sure if this is correct and what has changed with the upgrade. Please let me know if you have any insights or what I can check further. Thanks in advance. |
Hi guys.
|
Waiting for a good answer🤖 |
The final solution: open your mind and reflect on what you really need After a week spent trying to get Nuxt3 working with SSL with Nginx for development.... I realized that I have no need of Nginx !!! My goal is to get Nuxt3 working in a domain with SSL. It was my principle that it should work behind the reverse proxy, but, after all, it is just a whim. so: package.json
In your Unbound conf or hosts file add reference to your domain and your YOUR_IP now open the browser on https://yourdomain.tld:YOUR_PORT :
|
It is reasonable only in certain circumstances. We are using Docker and a reverse proxy container to make sure our development setup is as close to our production setup. (I read somewhere that letting Nuxt do ssl in production is not safe, am I right?) |
Hi, this is my definitive and working setup for Nuxt3 with vite and Nginx nuxt.config.js
package.json "dev": "nuxt dev --host localhost --port 3035" domain.conf
enjoy :) |
Just leaving a note for anyone who may be having issues with this solution after upgrading to It seems like specifying a |
App work when removing Need to add this in
|
We also had issues on 3.7.4 --- To clarify, we actually had issues just when we went to upgrade, not even necessarily related to 3.7.4 but its dependencies.. We had the same issues on at least 3.7.0-3.7.4. Had to add these two rules to our existing nginx proxy pass to get HMR to work as per @tibesyc comment. We didn't need the cache bypass, host rule, nor did we need the http_version change.
|
For anyone trying to run nuxt dev server behind a reverse proxy using latest nuxi CLI:
see nuxt/cli#241 (comment) for a working (apache2) example and feel free to open any issues in nuxt/cli repository if still having for setup. Also worth to note, modern Nuxi can terminate SSL and listen on single port (including 80/443) or even tunnel to an external URL with custom domain. You probably can simplify your whole workflow, however hacking is always welcome! |
@JefferySJones's answer worked for me with |
So glad to see others using Nuxt / SSL / Nginx Proxy, I was going crazy trying to find the error: [Vue Router warn]: No match found for location with path "/_nuxt/" @fabko reference to @JefferySJones solution worked, redeployed NGINX proxy with new settings and worked great. THANK YOU! |
New problem in Nuxt 3.10.1, last config from 3.8.x do not work anymore. my last problem after a lot configs was: SyntaxError: Failed to construct 'WebSocket': The URL 'wss://localhost:undefined/' is invalid but now, I can not believe DevTools, hmr work !!: [vite] connecting... so:
package.json
nginx.conf
domain.conf
|
Our configuration for dev: (only nuxt specific settings)
We have a seperate nginx config for our (nginx) proxy, that does the ssl. Here we have also
|
Hi! nginx.conf
nuxt.config.ts
package.json
But I get messages in the browser console on the page
How can I fix the config to fix the error? |
|
@s00d I distilled much of the information in this thread into a blog post. Note the update at the end about the
This works great with nginx 1.27.0 and nuxt 3.12.4. |
@command-tab In some versions, there was
Yesterday, I checked all this on version 3.11.2. |
Unfortunately finding the right configuration took me a few hours, so I thought I write something to help out in case other people run into this. My setup is a Docker instance with apache 2.4.55 + backend + frontend with nuxt. The apache proxies requests to the frontend as required (same as nginx in this thread). I experienced this issue both with nuxt 3.12.13 and nuxt 3.12.14, and in both cases the fix was different both on nuxt-side and apache config. Thus this writing. First time took me quite some time to understand what was going on, second time it was rather easy. The tricky part, as other people pointed out, is that I'm using HTTPS in the local environment, thus, for HMR to work, the client needs to properly use WSS and use the proper port, so the apache inside docker needs to proxy the requests to the right address. Fix for Nuxt 3.12.14This fix only works for this version of Nuxt on my end (every package up to date as of this writing). nuxt.config.tsexport default defineNuxtConfig({
// ... long config ...
modules: [
// ... other modules ...
(_options, nuxt) => {
nuxt.hooks.hook('vite:extendConfig', (config) => {
config.server = {
...config.server,
hmr: { // <--- this is the fix on nuxt side
protocol: undefined,
clientPort: 443, // <-- this was not required on 3.12.13 but is required in 3.12.14
}
}
})
},
],
} 000-apache-site-ssl.conf # required for the frontend
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
# support WebSocket upgrade
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://127.0.0.1:24678/$1 [P,L] Normal requests get a passthrough to Nuxt 3000 port (HTTP), whereas upgrade to websockets get forwarded to port 24678 which seems to be the default, and I rather leave it like that. Although I guess it can be changed with vite.server.hmr.port. Fix for Nuxt 3.12.13Unlike with Nuxt 3.12.14 (the most recent), in the previous version the issue was that the client was using a hardcoded protocol. By setting it to "undefined" the code automatically detects the proper protocol based on the caller URL. The WebSocket server on the nuxt side was already using the same port as nuxt, thus, the configuration in apache is different as well. I'm adding this because I invested time into fixing the issue here before I realized I was running on an "older" version of Nuxt. nuxt.config.tsexport default defineNuxtConfig({
// ... long config ...
modules: [
// ... other modules, maybe ...
(_options, nuxt) => {
nuxt.hooks.hook('vite:extendConfig', (config) => {
config.server = {
...config.server,
hmr: { protocol: undefined } // <--- this is the fix on nuxt side
}
})
},
],
} 000-apache-site-ssl.conf # required for the frontend
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
# support WebSocket upgrade
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://127.0.0.1:3000/$1 [P,L] Please note that above code will proxy-pass all requests to port 3000 both for Understanding the issue / debugging the issueI'm writing an extra section just in case this might help somebody in the future. Apart of reading this issue, there were two things that helped me understand what was going on:
When the request failed, I clicked on the dev-tools on the file that was causing the issue to inspect the code (usually client.js) and then looked for the source code of that file in node_modules (and then in vite repo). E.g when I loaded nuxt in dev mode, this is what I was getting with Nuxt 3.12.13:
Then, there was a link to: https://prolocal.artypist.com/_nuxt/@vite/client around line 500ish. Culprit was on this line: const socketProtocol = "wss" || (importMetaUrl.protocol === "https:" ? "wss" : "ws"); It was forcing "wss" always, and it should have detected based on the request. Thus, setting to undefined causes vite to automatically detect, and that works In vite the code looks like this: const socketProtocol =
__HMR_PROTOCOL__ || (importMetaUrl.protocol === 'https:' ? 'wss' : 'ws') HMR_PROTOCOL is replaced in runtime by nuxt, but if we set to undefined/zero/false, the second part of the expression is evaluated. Now, the second thing that helped was to be able to perform a request from the commandline and DevTools console in order to assess whether the request was properly being proxied or not. These are the two basic pieces I used: Example using wesocat command: $ ./websocat --protocol vite-hmr ws://127.0.0.1:3000/_nuxt/
{"type":"connected"} Example using js in devtools: w = new WebSocket('ws://127.0.0.1:3000/_nuxt/', ['vite-hmr']);
w.addEventListener('open', (e) => { console.warn('open:', e) });
w.addEventListener('error', (e) => {console.warn('error:', e)}) ; With wesocat I could try to reproduce the issue both on my host machine, and inside docker instance. # Inside docker, the basic check to see if Vite was responding:
$ ./websocat --protocol vite-hmr ws://127.0.0.1:24678/_nuxt/
{"type":"connected"}
# Inside docker, the basic check to see if Vite was responding through Apache
$ ./websocat --protocol vite-hmr wss://127.0.0.1/_nuxt/
{"type":"connected"}
# In the host machine (outside docker), to see if Vite was responding
$ ./websocat --protocol vite-hmr wss://127.0.0.1/_nuxt/
{"type":"connected"} Please note that if you don't use "vite-hmr" protocol, it won't respond. WebSockets Protocol header is required. I decided to post here instead of the HMR apache issue, because regardless of the apache/nginx, this issue is more about running SSL in the local dev environment than what server is used in the backend to proxy the request, I'm sure same logic applies to nginx (but with different config). Thanks everybody for all the comments in this thread! They helped me start making sense of what was going on. I hope this writing can contribute as well and help other people. |
Whats the equivalent for nginx? My config is based on nginx. |
@fabkho I encountered this issue, too, and it was resolved when I replaced all instances of |
@command-tab that helped! Thanks! Now the full-screen nginx error page does not occur anymore, but i get still the console errors. Weird 🤔 |
@danielroe This should be closed as fixed by #29049. Tested on latest |
Environment
Linux
v16.11.1
3-3.0.0-27235451.ece0e01
Yarn
Webpack
-
-
-
Describe the bug
Trying to start development server for people on nginx engine. When i'm starting with
cli
nuxt, i'm getting infinite reloads on page with errorWebSocket connection to wss://example.com:24678/_nuxt/
failed`. Is there any chance to start nuxt with proxy on nginx?Reproduction
Just cloned default project.
Additional context
No response
Logs
Nothing in nuxt3
The text was updated successfully, but these errors were encountered: