跳至内容

Kome97's 杂七杂八

搭建v2ray+ws+TLS

Table of Contents

# 软件环境

系统: ubuntu 20.04
软件: Nginx, v2ray, certbot, python-certbot-nginx
说明: domain.com 是网站使用的域名, 下面所有使用该域名的都要替换成自己的域名

# 开始安装

## 安装除v2ray外的软件

apt install nginx certbot python-certbot-nginx -y

## 安装v2ray

# 安装可执行文件和 .dat 数据文件
bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)

# 只更新 .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

### 配置

监听65510端口, UUID使用7cdcdc07-41c0-4ec5-b65c-7614969206c5, 使用v2路径, 其他默认.

{
"inbounds": [
    {
    "port": 65510,
    "listen":"127.0.0.1",//只监听 127.0.0.1
    "protocol": "vmess",
    "settings": {
        "clients": [
        {
            "id": "UUID",
            "alterId": 0
        }
        ]
    },
    "streamSettings": {
        "network": "ws",
        "wsSettings": {
        "path": "/v2"
        }
    }
    }
],
"outbounds": [
    {
    "protocol": "freedom",
    "settings": {}
    }
]
}

### 启动 v2ray 并配置开机自启

# 启动 v2ray
systemctl start v2ray
# 开机自启 v2ray
systemctl enable v2ray

## 配置 Nginx

位于/etc/nginx下的默认配置nginx.conf不做任何改动, 在目录/etc/nginx/conf.d进行配置.

### 新建配置文件

touch domain.com.conf

### 简单进行配置

server{
    server_name doamin.com;
    location / {
        root /usr/share/nginx/html/domain.com;
        index index.html;
    }
}

### 建立站点目录和添加首页

站点目录就是配置文件中的root配置项

# 创建站点目录
mkdir -p /usr/share/nginx/html/domain.com
# 创建首页文件并写点东西
echo "This is a nginx server" > /usr/share/nginx/html/domain.com/index.html

### 测试 Nginx 配置文件

nginx -t

通过测试则一般显示

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果测试通过则继续, 不能通过则根据提示进行修改.

### 申请 SSL 证书

申请证书的时候需要保证网站能够在外部通过域名domain.com访问, 并同时打开443端口, 或者允许 HTTPS流量通过. 最简单的方法就是关闭系统防火墙, 有运营商防火墙的要打开443端口或者放行HTTPS流量.

# 关闭防火墙
systemctl stop iptables
# 或者
systemctl stop ufw
# 或者
systemctl stop firewalld
# 自己用的什么防火墙就关掉什么防火墙, 或者放行流量

使用certbot申请Let's Encrypt证书

certbot --nginx -d doamin.com 
# 之后按照提示进行填写内容

### 修改 Nginx 配置文件

在配置文件server配置块内添加以下内容:

location /v2 {  # /v2 就是 v2ray 配置中的 path
        proxy_redirect off;
        proxy_pass http://127.0.0.1:65510; # 端口号就是 v2ray 配置中的端口号
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

### 测试 Nginx 配置

nginx -t
# 测试通过则继续, 不通过根据提示修改

### 启动 Nginx, 并配置开机自启

# 启动nginx
systemctl start nginx
# 开机自启nginx
systemctl enable nginx

# 参考

fhs-install-v2ray Update: Using Free Let’s Encrypt SSL/TLS Certificates with NGINX