-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Allow Dev Server to Work Behind a Proxy #19219
Comments
(Edit: Variable name changed to |
I guess you can tell the reverse proxy to not to strip the prefix. If that's not acceptable for some reason, probably setting base to |
I have no control over the reverse proxy. (I use a private cloud solution that allows me to create development VMs.) When I request
Unfortunately not. The request for To summarize: The problem is that the URLs of the resources requested by the client must contain the But by setting a We need a way to tell the Vue dev server to keep adding a base URL to the resources URLs ( |
Then, I think a plugin like this would work: import { defineConfig } from 'vite';
export default defineConfig({
base: '/proxy/5050/',
plugins: [
{
name: 'strip-base',
apply: 'serve',
configureServer({ middlewares }) {
middlewares.use((req, res, next) => {
if (!req.url.startsWith('/proxy/5050/')) {
req.url = '/proxy/5050' + req.url;
}
next();
});
},
},
],
}); https://stackblitz.com/edit/vitejs-vite-mzubcv8v?file=vite.config.js&terminal=dev |
Well done @sapphi-red, it is working! Thanks! |
I reopen this issue because I think implementing the support of the It would be nice to support dev and production envs. (In dev, adding a base to the URL of the resources and changing the incoming requests inside the middleware to add the base striped by the proxy, and in production, just add a base to the URL of the resources.) Feel free to close this issue if this change is too complicated to implement / unecessary. |
Description
I am developing a Vue app.
The app is served behind a proxy in development—not in production.
Problem: The proxy removes a part at the start of the path—confusing Vite dev server.
Current flow of a page request from the browser (to test a change during development):
Suggested solution
Vite dev server should allow us to indicate a
REVERSE_PROXY_BASE_URL
, in addition of theBASE_URL
:BASE_URL
:/proxy/5050/
)<script src="/proxy/5050/src/main.ts">
from the browser's perspective./proxy/5050/
)GET /proxy/5050/src/main.ts
to retrieve the resources.REVERSE_PROXY_BASE_URL
:/proxy/5050/
)<script src="/proxy/5050/src/main.ts">
from the browser's perspective./
)GET /src/main.ts
to retrieve the resources (would work from the proxy's perspective)We could set both variables at the same time:
BASE_URL="/base/"
REVERSE_PROXY_BASE_URL="/proxy/5050/"
In this case, if both variables are set:
<script src="/proxy/5050/base/src/main.ts">
from the browser's perspective (combining both variables.)GET /base/src/main.ts
to retrieve the resource from the proxy.BASE_URL
only, sinceREVERSE_PROXY_BASE_URL
has been stripped by the proxy).Alternative
A temporary workaround is to avoid using the Vite dev server in this case—at the cost of a bad developer experience.
Build the app, and use a third-party static server to serve the
dist/
directory from the/
url:Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: