• 在使用 Nginx 进行 WebSocket 负载均衡时的问题

    WebSocket 协议需要特定的 Nginx 配置来正确处理连接。确保你的 Nginx 配置文件中包含以下内容:

    Nginx 配置问题

    upstream websocket {
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
    }
    
    server {
        listen 80;
    
        location /ws/ {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }

    }

    关键配置解释-WebSocket 协议升级

    WebSocket 协议通过 HTTP 协议进行初始握手,然后升级到 WebSocket 协议。确保 Nginx 正确处理了 UpgradeConnection 头。

    proxy_http_version 1.1; 是WebSocket连接必须的
    proxy_set_header Upgrade $http_upgrade; 之后这两行是将 头协议从HTTP协议级成WebSocket协议
    proxy_set_header Connection $connection_upgrade;

    超时设置

    WebSocket 连接通常是长连接,确保 Nginx 的超时设置足够长。

    proxy_connect_timeout 7d;
    proxy_send_timeout 7d;
    proxy_read_timeout 7d;

  • Linux中申请Let’s Encrypt通配符证书

    1. 使用的工具,使用certbot申请并使用acme-dns-client作为manual-auth-hook执行更新证书的验证工作。
    • 安装certbot:在不同的Linux发行版中可以使用apt install certbot, yum install epel-release 之后再yum install certbot以及 dnf install certbot
    • 安装certbot之后会有以下提示,自动证书更新服务没有启动,但是新版本的话当使用certbot申请证书后,certbot-renew.timer服务会自动开启

    ###Certbot auto renewal timer is not started by default.

    ###Run ‘systemctl start certbot-renew.timer’ to enable automatic renewals.

    systemctl status certbot-renew.timer

    • 使用acme-dns-client注册需要申请证书的域名,每一个执行一次并按提示修改DNS记录,因为CAA记录验证属于有风险操作所以 {必选参数 –dangerous},例子如下

    acme-dns-client register -d cn-it.org -s https://auth.acme-dns.io –dangerous

    acme-dns-client register -d 51azure.com -s https://auth.acme-dns.io –dangerous

    acme-dns-client register -d yuushatech.com -s https://auth.acme-dns.io –dangerous

    acme-dns-client register -d maoutech.com -s https://auth.acme-dns.io –dangerous

    acme-dns-client register -d 5i818.com -s https://auth.acme-dns.io –dangerous

    acme-dns-client register -d 51aws.com -s https://auth.acme-dns.io –dangerous

    每一次命令输入完都会有一个通过DNS验证域名所有权的过程,需要添加CNAME记录,如下

    _acme-challenge.cn-it.org.     IN      CNAME   6185f842-efb2-4c65-9813-a0c410ea36ac.auth.acme-dns.io

    • 在验证完CNAME记录之后会提示验证CAA记录,需要在DNS中添加CAA记录

    caa         @            letsencrypt.org 0 issue  (单域名证书验证记录)

    caa         @            letsencrypt.org 0 issuewild (通配符证书验证记录)

    阿里云DNS注意事项,DNS记录值中的 “letsencrypt.org” 一定要有””

    • 验证CNAME的过程中可能会有警告信息,说明这个服务器以前做过相同操作留有旧的账户信息。

    查看 /etc/letsencrypt/accounts/  目录下可能有两个账号,删掉老的。

    Do you wish to set up a CAA record with accounturi now? [y/N]: y

    [i] Found a total of 2 ACME account(s) on this system:

     [Certbot] URI: https://acme-staging-v02.api.letsencrypt.org/acme/acct/177419874

    • 在所有域名验证完成后,申请证书。Challenges模式是 dns,auth-hook 是acme-dns-client。由于新的证书安全性需要,通配符证书必须还要配合根域域名证书才能信任,所以要同时申请。

    certbot certonly –manual –preferred-challenges dns \

    –manual-auth-hook ‘acme-dns-client’ -d “*.cn-it.org” -d cn-it.org -d “*.51azure.com” -d 51azure.com -d “*.yuushatech.com” -d yuushatech.com -d “*.maoutech.com” -d maoutech.com -d “*.5i818.com” -d 5i818.com -d “*.51aws.com” -d 51aws.com

    • 如果之前的CNAME记录及CAA记录都完好可查的情况下,证书应该可以顺利签署。

    使用命令 certbot renew –dry-run 可以测试证书是否可以正常renew,使用命令 systemctl status certbot-renew.timer 可以查看自动renew服务的状态

    1. 关于配置文件及自动运行脚本

    配置文件位置 /etc/letsencrypt/renewal/cn-it.org.conf

    自动运行脚本 /etc/letsencrypt/renewal-hooks/post/pkcs12convert.sh

    脚本内容如下,每次自动更新证书后会自动运行renewal-hooks/post 中的脚本

    #!/bin/sh

    openssl pkcs12 -export -out /etc/letsencrypt/live/cn-it.org/cn-it.org.pfx -inkey /etc/letsencrypt/live/cn-it.org/privkey.pem -in /etc/letsencrypt/live/cn-it.org/cert.pem -certfile /etc/letsencrypt/live/cn-it.org/chain.pem -passout pass:XXXxxxXXX

    echo “pkcs#12 generated!”

    systemctl restart nginx

    echo “Nginx restarted”

  • 转:BBR for CentOS 7/debian8 整合分享

    Centos7转自源网站
    http://www.hostloc.com/thread-342505-1-1.html
    Debian8转自源网站
    http://www.awkxy.com/archives/721
     


    Centos7
    先把/etc/sysctl.conf 文件中 关于 net.ipv4.tcp_congestion_control的配置注释掉。(Azure的CentOS 7本来就没有)
    wget http://mirrors.kernel.org/debian/pool/main/l/linux/linux-image-4.13.0-1-amd64_4.13.4-1_amd64.deb
    ar x linux-image-4.13.0-1-amd64_4.13.4-1_amd64.deb
    tar -Jxf data.tar.xz
    install -m644 boot/vmlinuz-4.13.0-1-amd64 /boot/vmlinuz-4.13.0-1-amd64
    cp -Rav lib/modules/4.13.0-1-amd64 /lib/modules
    depmod -a 4.13.0-1-amd64
    dracut -f -v –hostonly -k ‘/lib/modules/4.13.0-1-amd64’ /boot/initramfs-4.13.0-1-amd64.img 4.13.0-1-amd64
    grub2-mkconfig -o /boot/grub2/grub.cfg
    #开启bbr
    echo “net.core.default_qdisc=fq” >> /etc/sysctl.conf
    echo “net.ipv4.tcp_congestion_control=bbr” >> /etc/sysctl.conf
    #调整内核启动顺序
    grub2-set-default “CentOS Linux (4.13.0-1-amd64) 7 (Core)”
    grub2-editenv list
    grub2-mkconfig -o /boot/grub2/grub.cfg
    然后reboot


    ubuntu/debian系统
    下载新内核:
    wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.11.2/linux-image-4.11.2-041102-generic_4.11.2-041102.201705201036_amd64.deb
    安装内核:
    dpkg -i linux-image-4.11.[Tab补全]
    删除其他内核:
    dpkg -l|grep linux-image
    apt-get remove linux-image-4.9.0-040900rc8-generic #删4.11.0以外的旧内核
    apt-get remove linux-image-4.11.0-trunk-amd64 #删4.11.0以外的旧内核
    更新 grub 系统引导文件并重启(Azure虚机执行update-grub报错时看下面)
    update-grub
    reboot
    开启BBR
    echo “net.core.default_qdisc=fq” >> /etc/sysctl.conf
    echo “net.ipv4.tcp_congestion_control=bbr” >> /etc/sysctl.conf
    sysctl -p
    sysctl net.ipv4.tcp_available_congestion_control
    查看下是否有BBR:lsmod | grep bbr


    Azure的Debian8 没有dracut
    apt-get install dracut
    Azure的Debian8 grub安装不全
    sudo apt-get update; sudo apt-get install –reinstall grub
    mkdir /boot/grub
    Linux小技巧
    1,查看发行版
    lsb_release -a
    2,查看内核
    cat /proc/version
    uname -a
    uname -r
    3,查看系统位数
    file /bin/ls
    4,验证BBR
    sysctl net.ipv4.tcp_available_congestion_control
    sysctl net.ipv4.tcp_congestion_control
     

  • 转:Custom linux boot up screen

    Displaying an image during boot instead of the default command line scrolling text

    This is based on the guide here.
    This solution works but there are a few seconds of text shown before the boot image appears.

    Install fbi
    
    sudo apt-get install fbi
    
    Copy the splashscreen image to be used

    Copy your custom splash image into: /etc/ and name it “splash.png”.
    Presumably the resolution to use is 1920x1080px.
    Create A Script

    
    sudo nano
    

    Paste the following into the text editor:

    
    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides:          asplashscreen
    # Required-Start:
    # Required-Stop:
    # Should-Start:
    # Default-Start:     S
    # Default-Stop:
    # Short-Description: Show custom splashscreen
    # Description:       Show custom splashscreen
    ### END INIT INFO
    do_start () {
        /usr/bin/fbi -T 1 -noverbose -a /etc/splash.png
        exit 0
    }
    case "$1" in
      start|"")
        do_start
        ;;
      restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
      stop)
        # No-op
        ;;
      status)
        exit 0
        ;;
      *)
        echo "Usage: asplashscreen [start|stop]" >&2
        exit 3
        ;;
    esac
    :
    

    IMPORTANT – If copying and pasting via SSH check it has pasted in correctly (pasting via FiseSSH for us caused the # lines and the esac line to be altered and need modifying back to be correct)
    Exit and save the file as: /etc/init.d/asplashscreen
    (using a name starting with ‘a’ will ensure it runs first)
    Finally make the script executable and install it for init mode:

    
    sudo chmod a+x /etc/init.d/asplashscreen
    sudo insserv /etc/init.d/asplashscreen
    

    Thats it:

    
    sudo reboot
    

     

    Getting Out Of Black Screen

    If you get a black screen at the end of booting (if you’ve not setup auto running the GUI etc) use CTRL + ALT + F2 to get the command prompt