写在前面
Nginx使用的Tengine分支,进行编译安装。tengine是淘宝发起的项目,本文基于Tengine-2.3.3
nginx负载采用的是反代ws的方式实现,可以理解成转发
其中/usr/local/nginx/tengine.conf
文件即为配置文件,修改该文件达到“转发”的目的
与以往惯例一样,本文基于CentOS7 x64。其他系统请自行参考修改
*本文不介绍如何安装v2ray,如果你不知道如何安装v2ray请移步->v2ray对接ssp教程
正文
升级系统并安装编译所需组件
yum update -y
yum install epel-release -y
yum install gcc gcc-c++ autoconf automake -y
yum install pcre-devel -y
yum install openssl-devel -y
yum -y install libmcrypt libmcrypt-devel mcrypt mhash
编译安装Tengine
wget http://tengine.taobao.org/download/tengine-2.3.3.tar.gz
tar zxvf tengine-2.3.3.tar.gz
cd /root/tengine*
./configure --without-http_upstream_keepalive_module --with-stream --with-stream_ssl_module --with-stream_sni --add-module=modules/ngx_http_upstream_* --add-module=modules/ngx_debug_* --add-module=modules/ngx_http_slice_module --add-module=modules/ngx_http_user_agent_module --add-module=modules/ngx_http_reqstat_module --add-module=modules/ngx_http_proxy_connect_module --add-module=modules/ngx_http_footer_filter_module
make
make install
修改默认配置文件(nginx.conf)
使用vi打开配置文件
vi /usr/local/nginx/conf/nginx.conf
清空原有内容并填入下面内容
#user nobody;
worker_processes auto;
worker_rlimit_nofile 51200;
#pid logs/nginx.pid;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 2333;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#vmess-ws
include /usr/local/nginx/*.conf;
}
创建vmess+ws反代所需配置文件
创建配置文件
vi /usr/local/nginx/tengine.conf
填入下列内容
upstream yunyi{
ip_hash; #IP一致性模块
dynamic_resolve fallback=stale fail_timeout=60s; #动态域名解析
server isyunyi.com:443; #目的地址(vmess),可有多个
check interval=5000 rise=2 fall=2 timeout=3000 type=tcp; #主动健康检查,不支持动态域名
}
server {
listen 6666; #监听端口
location / {
proxy_pass http://yunyi;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /status { #监控页面
check_status;
access_log off;
}
}
创建systemd文件,启动nginx并开机自启
创建service文件
vi /lib/systemd/system/nginx.service
填入下述内容
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启动并查看启动状态
systemctl start nginx
systemctl status nginx
设置开机自启
systemctl enable nginx
至此,安装完成。只需要修改 /usr/local/nginx/tengine.conf
文件,即可实现转发
小尾巴-这次我很重要
tengine.conf
配置文件使用的是upstream
模块,其中主动健康检测是不支持动态域名的,上面只是演示了全部功能。如果要移除自动检测模块,需要将健康页面一并移除。监控页面的代码内容如下
location /status { #监控页面
check_status;
access_log off;
}
监控页面只需要一个即可,访问 nginx所在机器的ip:6666/status
即可访问状态页面
upstream的具体参数可以网络查阅,有机会我会写一下。