How I concurred CORS and got shlink
and shlink-web-client
to work - the easy way.
#793
Replies: 6 comments 20 replies
-
Thanks for sharing! However, you shouldn't need to do all this, as Shlink handles CORS out of the box. When this happens it is usually because the proxy in front of Shlink is stripping out the CORS headers. I believe this has been reported in the past by some other people also using traefik. |
Beta Was this translation helpful? Give feedback.
-
@Lockszmith-GH very nice! I was in the same boat as you #796 and your solution above worked for my use case too 😄 @acelaya thank you for the amazing project and for all your support! |
Beta Was this translation helpful? Give feedback.
-
@Lockszmith-GH I have a similar issue, but my setup is a bit different: |
Beta Was this translation helpful? Give feedback.
-
@Lockszmith-GH could you please explain how to do this in the UI of the truecharts deployment template? I have attempted this in the web server container, where shlink.website.com = shlink web server, and url.website.com = shlink server then I added shlink.website.com to the webserver config as you state: no luck, I must be missing something. Thank you! |
Beta Was this translation helpful? Give feedback.
-
Thank you for your new explanation, I was able to get this to work, finally! When I'm adding auth middleware (Authelia) on shlink-web-client, I'm getting a level=error msg="Target URL does not appear to have a relevant session cookies configuration" error="unable to retrieve session cookie domain provider: no configured session cookie domain matches the url 'https://shlink.***.***/'" method=GET path=/api/verify remote_ip=*** target_url="https://shlink.***.***/" How were you able to get auth working? |
Beta Was this translation helpful? Give feedback.
-
This seems to have worked for me as well. Same context: TrueNAS Scale apps for both Shlink and Shlink web-client using Traefik app for ingress, SSL cert manager (Let's Encrypt). Just to explicitly reiterate the solution bit for TrueNAS Scale:
EDIT: Don't forget: if you've already added your shlink server in the shlink-web-client, you will need to edit the server entry and change the URL to use the web client host, i.e. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
UPDATE 2025-02: TrueChrats does not exist anymore in the way my article used to describe, and I don't use it anymore - the architecture is still solid, and might help you in a non-TrueChart setting - so I'm keeping it here, just updating the article removing references to TrueChart.
I've been struggling with a proper implementation of Shlink-Web-Client on my self-hosted setup of Shlink for some time, until finally everything clicked.
tl;dr: skip to The Solution section at the bottom to skip the whole story.
I might be naive, but I feel this is something other might struggle with, and since the documentation didn't lead me directly to the solution, I thought I'll share my solution.
Goal
https://example.com/<slug>
https://shlink.example.com
or*
https://shlink.mydomain.com
authentik
to allow only authorized users access.Reverse Proxy configuration
The reverse proxy of my choice is Traefik, and in certain situations - like using the TrueNAS SCALE WebUI, there are limitations to the way things can be configured.
Below I describe how I setup the traefik routing:
The basic setup:
example.com
with path/
over https toslink
serviceshlink.example.com
with path/
over https toshlink-web-client
The problem
At this point, when accessing
https://shlink.example.com
I found that adding theshlink
server API key pointing at an IP address, internal hostname or externalhttps://example.com
would fail the connection when I'm accessing via the reverse-proxy https connection. The following message would show up:Inspecting the browser's Developer Tools Console, the following was visible:
After letting it drive me↗️ nuts, and while I understood what CORS is, I was focused on my inability to modify headers 'easily' within TrueNAS SCALE and I completely missed the solution that now seems very obvious to me.
The solution
shlink
service/app/container from theshlink-web-client
hostnameshlink.example.com
(or*shlink.mydomain.com
) for the/rest/
path.Then, in the web-client, add the server with
https://shlink.example.com
(*https://shlink.mydomain.com
).Because of the
/rest/
path pointing to theshlink
service's API, there is no Cross-Origin Request, thus not triggering any CORS related issue.Summary
The three routes that are required, are:
For the
shlink
BE server, the following ingress definition:IMPORTANT: no authentication middleware for the main
shlink
service, that is protected by the API key.example.com
with path/
shlink.example.com
(or *shlink.mydomain.com
) with path/rest/
For the
shlink-web-client
FE (the one WITH the authentication middleware):shlink.example.com
(or[<sup>*</sup>](#note-about-authentication-proxy)
shlink.mydomain.com) with path
/`.In the Web Client (or the container's envrionment) the server URL would be:
https://shlink.example.com
(or[<sup>*</sup>](#note-about-authentication-proxy)
shlink.mydomain.com), even though it will generate short URLs at
https://example.com/...`* NOTE about authentication proxy:
If your authentication proxy is served from another domain name (let's say
auth.mydomain.com
), unless it supports multi-domain authorization, yourshlink-web-client
must use the same domain as the proxy.This means, that instead of
shlink.example.com
as described above, you should useshlink.mydomain.com
.Beta Was this translation helpful? Give feedback.
All reactions