Docker series builds a long-chain to short-chain tool based on shlink
本文最后更新于 361 天前,其中的信息可能已经有所发展或是发生改变,如有失效可到评论区留言。

Preface

Originally, I didn't have much demand for the function of converting long links to short links, because I was using cryptgeon, and the generated links could also set the number of readings, time (although in minutes), and access passwords. But later I found that in situations where cryptgeon is not suitable, some long links (especially shared download links) are not elegant and very uncool when sent directly to others or placed on web pages, so I was forced to study and chose the most suitable one for me, shlink, from 3 short link generation tools (Myurls, YOURLS, shlink).

Comparison of tools for converting long links to short links

Here is a brief comparison of these three tools:

Myurls

Simple to the extreme, the interface is as follows:

image.png

It's simple, but it's not that simple to build. It also relies on redis, which seems to be not cost-effective.

YOURLS

This function has more than Myurls, as follows:

image.png

As can be seen from the above picture, YOURLS can see more information, including the number of clicks on the short link, etc., and YOURLS has login account authentication, which is safe and convenient. In this respect, it is actually better than myurls and shlink.

shlink

Why did I finally choose shlink? No picture, no truth, the picture above:

image.png

Device-specific long URLs:
Different long links can be returned according to different access devices, such as Android, iOS, and desktop systems.
Limit access to the short URL:
You can set the maximum number of short link accesses and the effective time.
These two are already very powerful, not to mention the following: Extra checks, Validate URL can check the validity of the long link, and if it cannot be accessed, an error will be reported when clicking Save; in Configure behavior, Make it crawlable can make the short link indexed by search engines, etc. Moreover, you can view the access status of the short link through multiple dimensions, and locate the visitor's location, as shown below:
image.png

So, I finally chose shlink, for good reasons.

Register a MaxMind account

However, shlink is also the most troublesome, mainly because it is necessary to locate the source of visitors and use MaxMind's GeoLite2 database, so you need to register an account with MaxMind and obtain the key:

image.png

It is recommended to register using the second method. The registration link is as follows:
https://www.maxmind.com/en/geolite2/signup?utm_source=kb&utm_medium=kb-link&utm_campaign=kb-create-account
You can register by yourself. After successful registration, click the red box at the bottom of the picture below:
image.png

Enter a description and click confirm below to complete the generation:
image.png

Please note that the key content will only be displayed when it is successfully created for the first time, and it will not be visible afterwards. Please save it carefully.

Deploy shlink

Then the preparations are done and the formal creation process begins.

Simply put, the deployment of shlink is divided into two parts: the backend server and the frontend web-client. The two can be deployed together or separately. You can choose according to your own conditions. I choose to deploy the backend server on Tencent Cloud's lightweight server because it has port 443, which is more convenient. The frontend web-client is deployed at home, so there is no need to map ports, and the main focus is on security.

Deploy the backend server:

docker run --name shlink -d --restart=always \ -p 9040:8080 \ -e DEFAULT_DOMAIN=example.com \ -e IS_HTTPS_ENABLED=true \ -e GEOLITE_LICENSE_KEY=xxxxxxxxxxxxxxxxxxxxxxxx \ shlinkio/shlink:latest

-p 9040:8080
Specify the mapping port of the container. You can modify 9040 according to your own plan.
-e DEFAULT_DOMAIN
It is used to specify your short domain name. In fact, you don't necessarily need to buy a very short one separately. Generally speaking, your existing domain name will do, such as the domain name corresponding to your cloud host. Assuming that the domain name corresponding to your cloud host is example.com, you can directly fill in example.com here, provided that you have an A record of @ in the domain name resolution server. Of course, you can also set a separate domain name, such as a.example.com, as long as it can be resolved to your cloud host, because the format of the short link generated at the end is https://example.com/xxxxx, or https://a.example.com/xxxxx, so as long as the specified domain name can reach the 443 port of the server where you deploy shlink. So what if it is the public IP of your home broadband, and there is no 443 or 80 port, will it work? The answer is yes, for example, if the port mapped by your home broadband is 44444, then -e DEFAULT_DOMAIN=example.com:44444 will do. However, this is not ideal in some scenarios. For example, when you click a link in a WeChat chat history, you will be prompted that it is not a standard port or something. It is really annoying, so in the end I deployed the server to Tencent Cloud's lightweight server.
-e IS_HTTPS_ENABLED=true
Used to specify whether shlink generates an https link or an http link. true is https, false is http. It is recommended to use https, which is safe. However, http links will be reported as not safe links on mobile Chrome browsers, which is very annoying.
-e GEOLITE_LICENSE_KEY
This is the key you obtained after registering MaxMind. Just paste it after the =.


By default, shlink will use the sqlite database. If you want to use other existing databases, you can specify it using the -e parameter as follows:
-e DB_DRIVER=maria \
-e DB_NAME=shlink \
-e DB_USER=shlink \
-e DB_PASSWORD=xxx\
-e DB_HOST=xxx \
The types of DB_DRIVER can be: mysql, maria, postgres, mssql, and the shlink library and the corresponding user and password must be configured in advance in the database to be used.


After the above steps, the server side has been completely set up, and then reverse the 443 port of example.com to 127.0.0.1:9040 on the reverse proxy (the port is based on your actual designation) and you are done (for the reverse proxy setting method, please refer to my other 2 articles:Linux panel series configure reverse proxy and use non-443 port for publishingandDocker series uses Docker to build its own reverse proxy based on NPM).https://example.com, the following 404 output will appear, which is considered successful:

image.png

Then execute the following command to obtain the api-key of the shlink backend server and save the output result. This needs to be configured in the front-end web-client:

docker exec -it shlink shlink api-key:generate

Deploy the front-end web-client:

Note: The usage of shlink's web-client front end is slightly different from that of general containers. There are two options:
1. When creating a container, use the -e parameter to specify the server address and key
The advantage of this is that you can see the saved server information whenever you change the login device

2. When creating a container, do not specify server information in advance. Log in and add it after creation.
In this way, the server information you added will be saved in the cookie of the current login device. In other words, if you clear the cookie or change the login device, you need to add the backend server information again.

You can choose different methods according to the environment.

First way:

docker run --name shlink-web-client -d --restart=always \ -p 9050:80 \ -e SHLINK_SERVER_URL=http(s)://example.com \ -e SHLINK_SERVER_API_KEY=xxxxxxxxxxxxxx \ shlinkio/shlink-web -client

Second way:

docker run --name shlink-web-client -d --restart=always \ -p 9050:80 \ shlinkio/shlink-web-client

This is very simple. After the container is built, use it directlyhttp://ip:9050Port access is OK.

Configuring shlink via web-client

If you use the second deployment method, you will see the following interface when you log in for the first time:

image.png

Click Add a server:
image.png

The name is arbitrary, and the url is the access address of the backend server:https://example.com, fill in the key obtained by the previous command in API key, and then click Create server, as shown below:
image.png

Click the three vertical dots on the right and select the first Connect:
image.png

Successfully log in to the server:
image.png

After entering, the Overview interface appears. On the right, you can see some statistical information, including the number of short links, the number of tags, and the number of visits. In this interface, you can also use Create a short URL on the right to start creating short links.
image.png

Click on the advanced options in the red box, and you can see the interface we saw earlier:
image.png

The URL to be shortened in the red box on the left is where you can fill in the long link. You can study the many other options on your own. Finally, click save at the bottom to generate a short link.

It's a great job.

Summary: In fact, changing a long link to a short link means that when you access the backend server through the generated short link, the backend server will return a 301 or 302 redirect to the original long link based on the long link corresponding to the short link. It is that simple. If the number of visits is large, the performance consumption of the server cannot be underestimated. Everyone should pay attention to this.

The content of the blog is original. Please indicate the source when reprinting! For more blog articles, you can go toSitemapUnderstand. The RSS address of the blog is:https://blog.tangwudi.com/feed, welcome to subscribe; if necessary, you can joinTelegram GroupDiscuss the problem together.
No Comments

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠(ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ°Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
Emoticons
Emoji
Little Dinosaur
flower!
Previous
Next
       
error:
en_US
Spring Festival
hapiness