Nginx反代+TLS+WebSocket+web(转载)

转自:https://zorz.cc/post/nginx-tlswebsocket-web-v2ray.html

前言

之前介绍过v2ray利用CDN(cloudflare)的方法,本次介绍的方法和上次有不少的相似之处,本次介绍一下将V2RAY隐藏到正常的网站背后,这样访问域名的时候看到的时候正常的网页,但是使用v2ray链接的话则可以实现梯子的功能。当然如果要实现访问域名出现一个正常网站的功能,你需要一个网站,然而自己建立一个网站也太麻烦了,我们可以反代一下别人的网站,这样访问你域名的时候则可以显示别人网站的页面,并且可以在微型小鸡上实现这样的功能。

准备

本次以debian/ubuntu系统为例,当然centos系统的配置差不了太多。
一个域名:可以申请免费域名,freenom.com,将域名A记录指向服务器。
一台服务器:64M内存就可以用了。服务器购买:Vultr,最低2.5刀一月,注册送10刀,支持支付宝。
一个欲反代的网站:选择一个简单点的网站,因为很多大网站会有安全措施,反代起来有点麻烦。

配置nginx

卸载apache。部分服务商提供的系统模版里面自带了apche,但是我们不需要它,并且它会影响到NGINX的使用,所以这里卸载它。
  1. service apache2 stop
  2. apt-get remove apche* -y
安装nginx
debian/ubuntu
  1. apt-get install nginx -y
创建dhparam.pem文件
  1. mkdir -p/etc/nginx/ssl
  2. openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
设置nginx配置文件,vi /etc/nginx/sites-enabled/你的域名.conf
  1. server
  2. {
  3. listen 80;
  4. #listen [::]:80;
  5. server_name 你的域名 ;
  6. #将http重定向到https
  7. return 301 https://你的域名$request_uri;
  8. }
  9. server
  10. {
  11. listen 443 ssl http2;
  12. #listen [::]:443 ssl http2;
  13. server_name 你的域名 ;
  14. ssl on;
  15. ssl_certificate /etc/nginx/ssl/你的域名/fullchain.cer;
  16. ssl_certificate_key /etc/nginx/ssl/你的域名/privkey.key;
  17. ssl_session_timeout 5m;
  18. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  19. ssl_prefer_server_ciphers on;
  20. ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
  21. ssl_session_cache builtin:1000 shared:SSL:10m;
  22. # openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
  23. ssl_dhparam /etc/nginx/ssl/dhparam.pem;
  24. access_log off;
  25. location / {
  26. #向后端传递访客IP
  27. proxy_set_header X-Real-IP $remote_addr;
  28. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  29. #设定需要反代的域名,可以加端口号
  30. proxy_pass http://148.251.3.246:22900/;
  31. #替换网站内容
  32. sub_filter '你反代的对象' '你的域名';
  33. # websocket设定,V2ray使用,这里的设置要和v2ray的设置一致。
  34. location /phpmyadmin/ {
  35. proxy_redirect off;
  36. #proxy_pass http://127.0.0.1:10000;
  37. proxy_http_version 1.1;
  38. proxy_set_header Upgrade $http_upgrade;
  39. proxy_set_header Connection "upgrade";
  40. proxy_set_header Host $http_host;
  41. proxy_intercept_errors on;
  42. if ($http_upgrade = "websocket" ){
  43. proxy_pass http://127.0.0.1:10000;
  44. }
  45. }
  46. }
  47. }
上面设定中的proxy_pass http://148.251.3.246:22900/;就是设定想要反代的域名的地方,148.251.3.246:22900是我挂的一个探针页面,以作测试用,当然你也可以改成其他的网站,比如https://github.com/,如果你反代的https://github.com那么sub_filter '你反代的对象' '你的域名';就是sub_filter 'github.com' 'yourdomain.com';

申请证书

安装acme.sh,一个自动化的Let’s Encrypt证书申请程序。
  1. apt-get install socat -y
  2. curl https://get.acme.sh | sh
以下命令都要在.acme.sh的目录内执行,root用户的在/root/.acme.sh
之前我介绍过用API申请LET’S证书的方法,但是相对来说比较复杂,这里用一种简单的方法,使用nginx方法来申请证书,使用nginx方法申请证书的话,需要先保证有关于这个域名的nginx配置在本例当中,就是上面那个配置文件。
申请证书
  1. acme.sh --issue --nginx -d 你的域名 -d 你的域名
acme.sh脚本可以一次申请多个域名证书,只需要多家-d参数。
创建目录存放证书。
  1. mkdir -p /etc/nginx/ssl/你的域名
将证书安装到nginx。不建议直接在nginx中使用~/.acme.sh/你的域名里面的证书,这个目录仅供内部使用,将来可能会改变结构。
  1. acme.sh --install-cert -d 你的域名 \
  2. --key-file /etc/nginx/ssl/你的域名/privkey.key \
  3. --fullchain-file /etc/nginx/ssl/你的域名/fullchain.cer \
  4. --reloadcmd "service nginx force-reload"
这个时候访问你的域名,你应该就能看到被你反代的网站了。

配置v2ray

安装v2ray
使用官方的安装脚本
  1. bash <(curl -L -s https://install.direct/go.sh)
如果提示:-bash: curl: command not found那么安装curl
  1. apt-get install curl -y
清空/etc/v2ray/config.json的内容,然后粘贴以下内容进去,vi /etc/v2ray/config.json
  1. {
  2. "log": {
  3. "access": "/var/log/v2ray/access.log",
  4. "error": "/var/log/v2ray/error.log",
  5. "loglevel": "info"
  6. },
  7. "inbound": {
  8. "port": 10000,
  9. "listen": "127.0.0.1",
  10. "protocol": "vmess",
  11. "allocate": {
  12. "strategy": "always"
  13. },
  14. "settings": {
  15. "clients": [{
  16. "id": "6d8a82b9-94d6-442e-a340-2b9cd5752c77",
  17. "level": 1,
  18. "alterId": 64,
  19. "security": "chacha20-poly1305"
  20. }]
  21. },
  22. "streamSettings": {
  23. "network": "ws",
  24. "wsSettings": {
  25. "connectionReuse": false,
  26. "path": "/phpmyadmin/"
  27. }
  28. }
  29. },
  30. "outbound": {
  31. "protocol": "freedom",
  32. "settings": {}
  33. },
  34. "outboundDetour": [{
  35. "protocol": "blackhole",
  36. "settings": {},
  37. "tag": "blocked"
  38. }],
  39. "routing": {
  40. "strategy": "rules",
  41. "settings": {
  42. "rules": [{
  43. "type": "field",
  44. "ip": ["0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/24", "192.0.2.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "::1/128", "fc00::/7", "fe80::/10"],
  45. "outboundTag": "blocked"
  46. }]
  47. }
  48. }
  49. }
上面"id": "6d8a82b9-94d6-442e-a340-2b9cd5752c77",里面的ID可以通过https://www.uuidgenerator.net/这里自助生成,强烈建议修改该值。

客户端的使用

https://github.com/v2ray/v2ray-core/releases查看并下载适合于自己系统的程序。
(注:V2RAY客户端和服务端一体,只是配置方式的不同,因此此处服务端和客户端在同一个地方下载,但是服务端和客户端的系统可能不同,因此应下载不同版本的程序。windows系统应下载:v2ray-windows-32.zip或者v2ray-windows-64.zip)
修改 config.json 配置为以下内容,注意 address (第25行)填写网站域名。
  1. {
  2. "log": {
  3. "access": "",
  4. "error": "",
  5. "loglevel": ""
  6. },
  7. "inbound": {
  8. "port": 1085,
  9. "listen": "0.0.0.0",
  10. "protocol": "socks",
  11. "settings": {
  12. "auth": "noauth",
  13. "udp": true,
  14. "ip": "127.0.0.1",
  15. "clients": null
  16. },
  17. "streamSettings": null
  18. },
  19. "outbound": {
  20. "tag": "agentout",
  21. "protocol": "vmess",
  22. "settings": {
  23. "vnext": [
  24. {
  25. "address": "网站域名",
  26. "port": 443,
  27. "users": [
  28. {
  29. "id": "6d8a82b9-94d6-442e-a340-2b9cd5752c77",
  30. "alterId": 64,
  31. "security": "chacha20-poly1305"
  32. }
  33. ]
  34. }
  35. ]
  36. },
  37. "streamSettings": {
  38. "network": "ws",
  39. "security": "tls",
  40. "tcpSettings": null,
  41. "kcpSettings": null,
  42. "wsSettings": {
  43. "connectionReuse": true,
  44. "path": "/phpmyadmin/",
  45. "headers": null
  46. }
  47. },
  48. "mux": {
  49. "enabled": true
  50. }
  51. },
  52. "inboundDetour": null,
  53. "outboundDetour": [
  54. {
  55. "protocol": "freedom",
  56. "settings": {
  57. "response": null
  58. },
  59. "tag": "direct"
  60. },
  61. {
  62. "protocol": "blackhole",
  63. "settings": {
  64. "response": {
  65. "type": "http"
  66. }
  67. },
  68. "tag": "blockout"
  69. }
  70. ],
  71. "dns": {
  72. "servers": [
  73. "8.8.8.8",
  74. "8.8.4.4",
  75. "localhost"
  76. ]
  77. },
  78. "routing": {
  79. "strategy": "rules",
  80. "settings": {
  81. "domainStrategy": "IPIfNonMatch",
  82. "rules": [
  83. {
  84. "type": "field",
  85. "port": null,
  86. "outboundTag": "direct",
  87. "ip": [
  88. "0.0.0.0/8",
  89. "10.0.0.0/8",
  90. "100.64.0.0/10",
  91. "127.0.0.0/8",
  92. "169.254.0.0/16",
  93. "172.16.0.0/12",
  94. "192.0.0.0/24",
  95. "192.0.2.0/24",
  96. "192.168.0.0/16",
  97. "198.18.0.0/15",
  98. "198.51.100.0/24",
  99. "203.0.113.0/24",
  100. "::1/128",
  101. "fc00::/7",
  102. "fe80::/10"
  103. ],
  104. "domain": null
  105. },
  106. {
  107. "type": "field",
  108. "port": null,
  109. "outboundTag": "direct",
  110. "ip": null,
  111. "domain": [
  112. "geosite:cn"
  113. ]
  114. },
  115. {
  116. "type": "field",
  117. "port": null,
  118. "outboundTag": "direct",
  119. "ip": [
  120. "geoip:cn"
  121. ],
  122. "domain": null
  123. }
  124. ]
  125. }
  126. }
  127. }
此时启用你的客户端,应该就可以实现利用CDN中转了。
客户端运行 v2ray 或 v2ray.exe,直接运行即可
本地 socks 代理为 127.0.0.1:1085
第三方客户端配置 address(网站域名)、port(443)、id(很长那串)、alterId(64)、security(chacha20-poly1305)、network(ws)、path(/v2ray/)、底层传输安全(tls)
参考链接:
https://github.com/iMeiji/shadowsocks_install/wiki/Project-V-%E9%85%8D%E7%BD%AE-WebSocket-TLS-Web-CDN