V2Ray+WebSocket Ubuntu Tutorial

V2Ray is a popular method to cross the Great Firewall (GFW) of China. WebSocket (WS) is a protocol that upgrades an HTTP connection to provide full-duplex communication over a single TCP connection. V2Ray has the capability to communicate using the HTTP-like WebSocket protocol. This assists in camouflaging the communication between your client and your server.

In this tutorial, you’ll see how to set up V2Ray+WebSocket on an Ubuntu server and an Ubuntu client.

Ubuntu Linux is released every six months, with long-term support (LTS) releases every two years. The version you’ll see demonstrated in this tutorial is the most recent LTS version, which at the time of writing is Ubuntu 18.04.

V2Ray logo

1. Set Up Ubuntu Server

Log on to your Ubuntu Linux server.

1.1. Install Nginx

The first thing you need to do is to install the Nginx web server:

sudo apt install nginx

Open /etc/nginx/sites-available/default in your favorite text editor, such as the vi editor or the nano editor. For example:

sudo vi /etc/nginx/sites-available/default

or

sudo nano /etc/nginx/sites-available/default

Set the server_name to your domain name. In our examples here, we will use www.example.com.

server_name www.example.com;

Write the file to disk, and quit the editor.

1.2. Install V2Ray

Now install V2Ray using the official installation script:

sudo apt install wget unzip

mkdir ~/Downloads

cd ~/Downloads

wget https://install.direct/go.sh

sudo bash go.sh

1.3. Configure V2Ray

Toward the end of the installation script run, you will see the generated values for your server port and your universally unique id (UUID). We’ll use 12345 and YOUR-SERVER-UNIVERSALLY-UNIQUE-ID in the examples that follow. Note down your actual values, as you will need them in a few moments.

Edit the V2Ray configuration file. If you prefer the vi editor, the command would be:

sudo vi /etc/v2ray/config.json

If you prefer the nano editor, you would enter instead the command:

sudo nano /etc/v2ray/config.json

The V2Ray configuration file is in a format called JavaScript Object Notation (JSON, pronounced JAY-sun). JSON is a standard data interchange format that presents attribute–value pairs and arrays in a readable format.

Add the listen line, which makes V2Ray listen on localhost (127.0.0.1) only. Also add the streamSettings parameters, substituting in your actual choice for the PATH directory:

{
  "inbounds": [
    {
      "port": 12345,
      "listen": "127.0.0.1",
      "protocol": "vmess",
      "settings": {
        "clients": [
          {
            "id": "YOUR-SERVER-UNIVERSALLY-UNIQUE-ID",
            "level": 1,
            "alterId": 64
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "wsSettings": {
          "path": "/PATH/"
        }
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom",
      "settings": {}
    },{
      "protocol": "blackhole",
      "settings": {},
      "tag": "blocked"
    }
  ],
  "routing": {
    "rules": [
      {
        "type": "field",
        "ip": ["geoip:private"],
        "outboundTag": "blocked"
      }
    ]
  }
}

Write the file to disk, and quit the editor.

1.4. Reconfigure Nginx

Edit the Nginx default site configuration file, /etc/nginx/sites-available/default.

Add a new location block with proxy rules. In the example below, replace PATH and 12345 with actual values.

location /PATH/ {
            proxy_redirect off;
            proxy_pass http://127.0.0.1:12345;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $http_host;
        }

Write the file to disk, and quit the editor.

1.5. Restart Server

Start V2Ray and restart Nginx:

sudo systemctl start v2ray

sudo systemctl restart nginx

2. Set Up Ubuntu Client

Now go to your Ubuntu Linux client PC.

2.1. Download V2Ray

Open the Firefox web browser. Download the latest V2Ray for 64-bit Linux from https://github.com/v2ray/v2ray-core/releases. The file to download for a 64-bit PC running Linux is v2ray-linux-64.zip.

Open a terminal emulator (Ctrl+Alt+t). Unzip V2Ray for 64-bit Linux with these commands:

cd ~/Downloads

mkdir v2ray

sudo apt install unzip

unzip v2ray-linux-64.zip -d v2ray

cd v2ray

2.2. Configure V2Ray

In the terminal or in a text editor, edit the V2Ray configuration file, which is config.json in the v2ray directory.

Add the placeholder text below, substituting in your actual values for YOUR.SERVER.IP.ADDRESS, YOUR-SERVER-UNIVERSALLY-UNIQUE-ID, PATH, and www.example.com:

{
  "log": {
    "loglevel": "warning"
  },
  "inbounds": [{
    "port": 1080, 
    "listen": "127.0.0.1",
    "protocol": "socks",
    "settings": {
      "auth": "noauth",
      "udp": false
    },
    "sniffing": {
      "enabled": true,
      "destOverride": ["http", "tls"]
    }
  }],
  "outbounds": [{
    "protocol": "vmess",
    "settings": {
      "vnext": [{
        "address": "YOUR.SERVER.IP.ADDRESS", 
        "port": 80,
        "users": [
          { "id": "YOUR-SERVER-UNIVERSALLY-UNIQUE-ID",
            "alterId": 64 }
        ]
      }]
    },
    "streamSettings": {
      "network": "ws",
      "wsSettings": {
        "path": "/PATH/",
        "headers": {
          "Host": "www.example.com"
        }
      }
    }
  },{
    "protocol": "freedom",
    "tag": "direct",
    "settings": {}
  }],
  "routing": {
    "domainStrategy": "IPOnDemand",
    "rules": [{
      "type": "field",
      "ip": ["geoip:private"],
      "outboundTag": "direct"
    }]
  }
}

Write the file to disk, and quit the editor.

2.3. Run V2Ray

In your terminal emulator, start the V2Ray client running:

./v2ray -config=config.json

2.4. Reconfigure Firefox

Open your Firefox web browser. From the hamburger menu, select Preferences. In the Network Settings section, click Settings.

Set the network settings as follows:

Click OK.

Firefox now sends requests to the V2Ray client, which sends them to the V2Ray server.