搭建代理服务器

介绍在配置代理服务器时的步骤与方法

这里添加 git 使用代理 dnf 使用代理 curl 使用代理的方法

使用代理加速 Git clone


export http_proxy="http://127.0.0.1:8889"
export https_proxy="http://127.0.0.1:8889"

#socks5
git config --global http.proxy 'socks5://127.0.0.1:1089'
git config --global https.proxy 'socks5://127.0.0.1:1089'

#http
git config --global http.proxy 'http://127.0.0.1:1089'
git config --global https.proxy 'http://127.0.0.1:1089'

https://qv2ray.net/lang/zh/getting-started/step5.html#%E8%B0%83%E6%95%B4%E8%B7%AF%E7%94%B1%E8%AE%BE%E7%BD%AE
首选项 - 入站设置 - 把监听地址从127.0.01改成0.0.0.0  实现局域网代理上网,ubuntu 代理设置地址为 fedora 本机地址   https://github.com/Qv2ray/Qv2ray/issues/414

git config --global --unset http.proxy
git config --global --unset https.proxy

开启 BBR

内核要4.9以上,centos7 内核是3.10

修改 kernel 参数配置文件 sysctl.conf 并指定开启 BBR

使用了其他 Linux 发行版,那么建议你建立 /etc/sysctl.d/ 文件夹,并在这个文件夹内建立自己的配置文件,形如 /etc/sysctl.d/vpsadmin.conf

把下面的内容添加进去

net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr

grep -qxF 'net.core.default_qdisc=fq' /etc/sysctl.conf || echo 'net.core.default_qdisc=fq' >> /etc/sysctl.conf
grep -qxF 'net.ipv4.tcp_congestion_control=bbr' /etc/sysctl.conf || echo 'net.ipv4.tcp_congestion_control=bbr' >> /etc/sysctl.conf
sysctl -p

echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p
lsmod | grep bbr

重启 VPS、使内核更新和BBR设置都生效

sudo reboot

检查 BBR 是否开启

确认BBR开启

如果你想确认 BBR 是否正确开启,可以使用下面的命令:

lsmod | grep bbr

此时应该返回这样的结果:

tcp_bbr

如果你想确认 fq 算法是否正确开启,可以使用下面的命令:

lsmod | grep fq

此时应该返回这样的结果:

sch_fq

V2ray 安装方法

安裝和更新 V2Ray

// 安裝執行檔和 .dat 資料檔
# bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)


安裝最新發行的 geoip.dat 和 geosite.dat

// 只更新 .dat 資料檔
# bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-dat-release.sh)

移除 V2Ray
# bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh) --remove

V2ray 安装结果信息

installed: /usr/local/bin/v2ray
installed: /usr/local/bin/v2ctl
installed: /usr/local/share/v2ray/geoip.dat
installed: /usr/local/share/v2ray/geosite.dat
installed: /usr/local/etc/v2ray/config.json
installed: /var/log/v2ray/
installed: /var/log/v2ray/access.log
installed: /var/log/v2ray/error.log
installed: /etc/systemd/system/v2ray.service
installed: /etc/systemd/system/v2ray@.service
removed: /tmp/tmp.fQI1BUqol5
info: V2Ray v4.45.0 is installed.
You may need to execute a command to remove dependent software: dnf remove curl unzip
Please execute the command: systemctl enable v2ray; systemctl start v2ray

V2ray 配置文件

{
  "inbounds": [
    {
      "port": 18888,
      "listen": "127.0.0.1",
      "protocol": "vmess",
      "settings": {
        "clients": [
          {
            "id": "f3819c92-e335-409f-b1f4-d0ac630039a2",
            "level": 1,
            "alterId": 2
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "wsSettings": {
          "path": "/out"
        }
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom",
      "settings": {}
    },
    {
      "protocol": "blackhole",
      "settings": {},
      "tag": "blocked"
    }
  ],
  "routing": {
    "rules": [
      {
        "type": "field",
        "ip": ["geoip:private"],
        "outboundTag": "blocked"
      }
    ]
  }
}

Nginx 代理

通过 http/https 访问 api.echoxu.cn 将解析到 nginx/html 中的正常网站目录,通过 vmess/vless 协议访问会解析到 api.echoxu.cn/out

安装 Nginx

sudo yum install yum-utils

sudo vim /etc/yum.repos.d/nginx.repo

添加:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

启用 nginx 源

sudo yum-config-manager --enable nginx-mainline

安装 nginx

sudo yum install nginx

开机启动 nginx

systemctl enable nginx

配置 Nginx

server{
    listen 80;
    server_name api.echoxu.cn;
    rewrite ^/(.*)$ https://api.echoxu.cn/$1 permanent;
}

server {
    listen       443 ssl http2;
    server_name  api.echoxu.cn;

    ssl_certificate    ssl/6649371_api.echoxu.cn.pem;
    ssl_certificate_key    ssl/6649371_api.echoxu.cn.key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;
    ssl_session_tickets off;

    ssl_protocols         TLSv1.2 TLSv1.3;
    ssl_ciphers           ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

location / {
            root /usr/share/nginx/html/wwwdoc;     # 此项配置是部署静态网站,使访问域名时能正常访问
            index index.html index.htm;
}

location /out {
    if ($http_upgrade != "websocket") {       # 不是ws流量返回404
        return 404;
    }
    proxy_pass       http://127.0.0.1:18888;  # 反向代理到v2ray,端口需和v2ray配置的一样
    proxy_redirect             off;
    proxy_http_version         1.1;
    proxy_set_header Upgrade   $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    # Show real IP in v2ray access.log
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

防火墙配置

firewall-cmd --zone=public --add-port=443,80/tcp --permanent

firewall-cmd --zone=public --remove-port=18888/tcp --permanent

firewall-cmd --reload

安装 Xray

wget https://github.com/XTLS/Xray-install/raw/main/install-release.sh

sudo bash install-release.sh


-   `/etc/xray/config.json`:配置文件
-   `/usr/bin/xray`:Xray 主程序
-   `/usr/local/share/xray/geoip.dat`:IP 数据文件
-   `/usr/local/share/xray/geosite.dat`:域名数据文件

## [#](https://xtls.github.io/document/install.html#%E6%9B%B4%E5%A4%9A%E6%9B%B4%E5%A4%9A)


xray 客户端
-   在 Windows 和 macOS 中,配置文件通常是 Xray 同目录下的 `config.json` 文件。
    -   直接运行 `Xray``Xray.exe` 即可。
-   在 Linux 中,配置文件通常位于 `/etc/xray/``/usr/local/etc/xray/` 目录下。
    -   运行 `xray run -c /etc/xray/config.json`
    -   或使用 systemd 等工具将 Xray 作为服务在后台运行。

代理服务器优化

服务器优化之二:开启 HTTP 自动跳转 HTTPS

之前我们已经搭建了 80 端口的 http 网页,并以此申请了 TLS 证书。

但如果你尝试过用浏览器访问我们的这个界面,就会发现 http 访问并不会像大多数网站一样自动升级为 https 访问。换言之,我们现在的设置下,http(80) 和 https(443) 之间完全是独立的。如果要解决这个问题,就需要做一些修改。

编辑 Nginx 的配置文件

sudo nano /etc/nginx/nginx.conf

在我们设置过的 80 端口 Server 中加入下面的语句,并保存退出(可同时删除root和index两行)

return 301 https://$http_host$request_uri;

在与 80 端口同级的位置增加一个本地端口监听来提供网页展示。本文以 8080 端口做演示。(可以是任意端口)

server {
   listen 127.0.0.1:8080;
   root /home/vpsadmin/www/webpage;
   index index.html;
   add_header Strict-Transport-Security "max-age=63072000" always;
}

重启 Nginx 服务

sudo systemctl restart nginx

修改 Xray 的回落设置,将回落从 80 端口改为 8080 端口。(找到 "dest": 80, 并改成 "dest": 8080)

sudo nano /usr/local/etc/xray/config.json

重启 Xray 服务,即完成了设置

sudo systemctl restart xray

上次更新:
贡献者: iEchoxu