How I concurred CORS and got shlink
and shlink-web-client
to work - the easy way.
#793
Replies: 5 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.
-
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
TC to allow only authorized users access.Hosting
I host my solution on TrueNAS SCALE using
shlink
gh andshlink-web-client
gh TrueCharts apps.While the details below are focus on TrueNAS SCALE, the solution can be generilzed for other hosting and reverse-proxy solution.
Reverse Proxy configuration
The reverse proxy of choice for TrueNAS SCALE using TrueCharts is TraefikTC, however, unlike plain docker or docker-compose implementations, the middlewares and routes are defined in the TrueNAS SCALE WebUI, which limits the way things can be configured.
Let's Encrypt acme certificates are handled by clusterissuerTC, if you're using NPMgh or configuring Traefikgh on your own.
I'll assume we've passed that stage, and focusing on how to route the services correctly.
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:
As I mentioned above, this has been↗️ driving 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 middlware 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 TrueCharts configuration of the web-client) 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 mulit-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