My firewall is configured to forward the ports 80 to 8001 and 443 to 4001. I'm using the following commands to achieve that under firewalld:
firewall-cmd --permanent --zone=public --add-forward-port=port=80:proto=tcp:toport=8001
firewall-cmd --permanent --direct --add-rule ipv4 nat OUTPUT 0 -o lo -p tcp --dport 80 -j REDIRECT --to-port 8001
firewall-cmd --permanent --direct --add-rule ipv6 nat OUTPUT 0 -o lo -p tcp --dport 80 -j REDIRECT --to-port 8001
firewall-cmd --permanent --zone=public --add-forward-port=port=443:proto=tcp:toport=4001
firewall-cmd --permanent --direct --add-rule ipv4 nat OUTPUT 0 -o lo -p tcp --dport 443 -j REDIRECT --to-port 4001
firewall-cmd --permanent --direct --add-rule ipv6 nat OUTPUT 0 -o lo -p tcp --dport 443 -j REDIRECT --to-port 4001
firewall-cmd --reload
- Build backend image
docker build -t svelte-external-api-test_backend:latest backend/
- Start backend & reverse proxy
docker compose up -d
- Add root certificate system:
sudo cp reverse-proxy/data/caddy/pki/authorities/local/root.crt /usr/local/share/ca-certificates/svelte-external-api-test.crt
andsudo update-ca-certificates
(You can add this root cert (reverse-proxy/data/caddy/pki/authorities/local/root.crt
) also to Google Chrome: Settings > Privacy and security > Security > Manage certificates > Installed by you > Import
- Backend & reverse proxy:
docker compose up -d
- Frontend:
cd frontend
andnpm run dev
No matter the URL, Svelte inherits cookies from "parent" request and does not do a second fetch request on client hydration.
relative URLs (/api/ ) |
absolute URLs (https://localhost/api/ ) |
|
---|---|---|
Cookie inheritance | ❌ | ✅ |
Runs once | ✅ | ❌ |
I guess the problem is, that vite confuses the host it runs on when using it behind a reverse proxy. I would call it a missing feature. I'm just getting rid of the reverse proxy in my dev environment, then it works. When deploying with node (in production) it works anyway.
Vite is configured to proxy the api and listens directly on 8001 (forwarded from 80). No reverse proxy needed. https is not necessary because to localhost cookies with secure flag are also sent over http.
- Build
npm run build
- Run
PROTOCOL_HEADER=x-forwarded-proto HOST_HEADER=x-forwarded-host node build