• 轻松搭建短域名短链接服务系统,可选权限认证,并自动生成证书认证把nginx的http访问转换为https加密访问,完整步骤和代码


    轻松搭建短域名短链接服务系统,可选权限认证,并自动生成证书认证把nginx的http访问转换为https加密访问,完整步骤和代码。

    在这里插入图片描述
    在这里插入图片描述
    在互联网信息爆炸的时代,网址复杂而冗长,很难在口头告知他人,也难以分享到社交媒体上。因此,网址缩短服务应运而生。本文将介绍其中的一种常用服务——短位链接。

    短位链接是将原本冗长的网址转换为较短的字符串,这样人们就能方便地将其传递到社交媒体、电子邮件、短信等平台上。短链接具有简短、美观、易记、友好等特点,使得分享和传播网址变得更容易,也可以提高点击率和转化率。

    短位链接服务通常需要用户将原始网址复制粘贴到它们的网站或应用程序中,然后生成一个短链接。这些短链接都是独一无二的,因为它们是由随机字符和数字组成的。同时,由于短链接服务器位于云端,因此可以提供高达99.9%的稳定性和可用性。

    使用短位链接服务是非常简单的。当您要分享网址时,您可以将原始网址复制到短位链接服务中,然后生成一个短链接。然后,您可以将这个短链接复制到任何您希望分享的位置,例如社交媒体、文本消息、电子邮件等等。

    最酷的是,大多数短位链接服务在提供基本转换服务之外还提供了一些附加的功能。例如,有些可以跟踪短链接的点击次数和转化率,以便能够优化推广效果。有些可以自定义短链接,使得链接更易于被识别并与品牌或公司相关联。

    另外,有些短位链接服务还提供一些额外的功能,如密码保护、短链接的到期日期、链接的防伪造、链接的加密等,以保护短链接的安全性。

    总之,短位链接是一种轻巧、方便、实用的网址转换服务。使用短位链接服务可以使我们的互联网世界更加便捷,分享和传播信息也变得更加易于操作。但也要注意,不同的短链接提供商可能会有不同的可靠程度和安全性,因此选择一个可靠的短链接提供商是非常重要的。

    以CentOS Linux系统为例,安装必要的工具依赖:

    yum -y install git docker docker-compose
    
    • 1

    下载代码:

    git clone https://github.com/AbnerEarl/shorturl.git
    
    • 1

    安装数据库脚本:

    #!/bin/bash
    
    yum install -y  mariadb-server
    
    service_limit_file="/etc/systemd/system/mariadb.service.d/limits.conf"
    if [[ ! -f ${service_limit_file} ]]; then
        mkdir -p /etc/systemd/system/mariadb.service.d/
        echo "[Service]" >> ${service_limit_file}
        echo "Service LimitNPROC 300000" >> ${service_limit_file}
        echo "Service LimitNOFILE 300000" >> ${service_limit_file}
    
        systemctl daemon-reload
    fi
    
    cat /etc/my.cnf |grep max_connections &> /dev/null
    if [[ $? -ne 0 ]]; then
        sed -i '/^\[mysqld\]$/amax_connections=300000' /etc/my.cnf
    fi
    systemctl enable mariadb.service
    systemctl restart mariadb.service
    
    # set root pwd
    mysqladmin password 'Look-good[123]'
    mysql -uroot -p'Look-good[123]' -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Look-good[123]' WITH GRANT OPTION;FLUSH PRIVILEGES;"
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    生成配置文件:

    cd shorturl/build/durl
    sh gen-config.sh
    
    
    • 1
    • 2
    • 3

    启动服务:

    systemctl start docker
    systemctl enable docker
    docker-compose up -d
    
    • 1
    • 2
    • 3

    停止服务:

    docker-compose stop
    
    • 1

    删除服务:

    docker-compose rm -f
    
    • 1

    配置Nginx代理:

    yum install -y nginx httpd-tools
    
    • 1

    配置用户密码:

    htpasswd -c /etc/.htpasswd admin
    
    • 1

    修改配置文件:

    vi /etc/nginx/nginx.conf
    
    • 1

    内容如下所示,注意替换为自己的域名"baidu.com":

    # For more information on configuration, see:
    #   * Official English Documentation: http://nginx.org/en/docs/
    #   * Official Russian Documentation: http://nginx.org/ru/docs/
    
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    
    # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;
    
    events {
        worker_connections 1024;
    }
    
    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 4096;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    
        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # See http://nginx.org/en/docs/ngx_core_module.html#include
        # for more information.
        include /etc/nginx/conf.d/*.conf;
    
        server {
        
            listen       80;
            listen       [::]:80;
            server_name baidu.com;
            location / {
            proxy_pass http://127.0.0.1:8082;
            }
    
    }
    
    
    
        server {
            listen       1080;
            listen       [::]:1080;
    
            location  / {
            proxy_pass http://127.0.0.1:8083;
            auth_basic "authentication";
            auth_basic_user_file /etc/.htpasswd;
            }
        }
    
    
       server {
            listen       2080;
            listen       [::]:2080;
            location  / {
            proxy_pass http://127.0.0.1:8080;
            auth_basic "authentication";
            auth_basic_user_file /etc/.htpasswd;
            }
        }
    
    
    
    # Settings for a TLS enabled server.
    #
    #    server {
    #        listen       443 ssl http2;
    #        listen       [::]:443 ssl http2;
    #        server_name  _;
    #        root         /usr/share/nginx/html;
    #
    #        ssl_certificate "/etc/pki/nginx/server.crt";
    #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
    #        ssl_session_cache shared:SSL:1m;
    #        ssl_session_timeout  10m;
    #        ssl_ciphers HIGH:!aNULL:!MD5;
    #        ssl_prefer_server_ciphers on;
    #
    #        # Load configuration files for the default server block.
    #        include /etc/nginx/default.d/*.conf;
    #
    #        error_page 404 /404.html;
    #            location = /40x.html {
    #        }
    #
    #        error_page 500 502 503 504 /50x.html;
    #            location = /50x.html {
    #        }
    #    }
    
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    启动Nginx代理服务:

    systemctl enable nginx
    systemctl restart nginx
    systemctl status nginx
    
    • 1
    • 2
    • 3

    生成https证书关联:

    sudo yum install epel-release -y
    sudo yum install snapd -y
    sudo systemctl enable --now snapd.socket
    sudo ln -s /var/lib/snapd/snap /snap
    
    sudo snap install core
    sudo snap refresh core
    
    # sudo apt-get remove certbot
    # sudo dnf remove certbot
    # sudo yum remove certbot
    
    sudo snap install --classic certbot
    
    sudo ln -s /snap/bin/certbot /usr/bin/certbot
    
    sudo certbot --nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    之后会弹出对话框,输入 邮箱、同意协议、确认域名、生成证书,全部选 Y 即可。

    会自动配置到Nginx,只需要重启服务生效即可:

    systemctl restart nginx
    
    • 1

    之后可以通过 https 访问短链接服务,访问 http 也会自动转换为 https 加密证书连接。

    对外短链接访问:

    https://你的域名/唯一码
    
    • 1

    生成短链接访问:

    http://你的域名:2080
    
    • 1

    管理短链接访问:

    http://你的域名:1080
    
    • 1

    后面两个服务访问都会进行身份认证后进行操作。

    在这里插入图片描述

  • 相关阅读:
    一文带你了解线程池原理
    中石化/中石油 油卡充值接口API文档分享
    xargs如何保留文本中的引号
    前端css实现特殊日期网页变灰功能
    AtCoder Beginner Contest 266 ABC题解
    IDEA2023创建SpringMVC项目
    Emacs之高亮显示超过80个字符部分(一百三十)
    golang本地缓存库之bigcache
    Vue3 之 reactive ref toRef toRefs
    ATV32变频器在堆垛机应用
  • 原文地址:https://blog.csdn.net/u014374009/article/details/134448774