编译nginx stream模块

1
./configure --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --with-openssl=/root/download/openssl-1.1.1h --add-module=../ngx-fancyindex-0.5.1 --add-module=../nginx-dav-ext-module --add-module=../headers-more-nginx-module --add-module=../ngx_brotli  --with-stream_realip_module --with-stream_ssl_preread_module --with-stream

下面空上清楚点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
./configure \
--prefix=/usr/share/nginx \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--lock-path=/var/lock/nginx.lock \
--pid-path=/run/nginx.pid \
--modules-path=/usr/lib/nginx/modules \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--with-debug \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_slice_module \
--with-threads \
--with-http_addition_module \
--with-http_geoip_module=dynamic \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--with-http_sub_module \
--with-http_xslt_module=dynamic \
--with-stream_ssl_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-openssl=/root/download/openssl-1.1.1h \
--add-module=../ngx-fancyindex-0.5.1 \
--add-module=../nginx-dav-ext-module \
--add-module=../headers-more-nginx-module \
--add-module=../ngx_brotli \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-stream

注意,原来编译的时候加入动态模块--with-stream=dynamic,所以要在nginx.conf里加入load_module /usr/lib/nginx/modules/ngx_stream_module.so; 但是我还是不用动态编译,因为编译之后nginx出错。

另外,还要加入--with-stream-realip_module模块,使之可以传送真实IP

修改nginx.conf文件

加入LOG格式,使/var/log/nginx/access.log里显示真实IP

1
2
3
4
5
6
7
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$proxy_protocol_addr:$proxy_protocol_port';

access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;

这里注意,若需要传递真实IP,需要在server{}里加入以下内容:

1
listen 443 ssl http2 proxy_protocol;

加入stream内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
stream {
# 这里就是 SNI 识别,将域名映射成一个配置名
map $ssl_preread_server_name $backend_name {
web.xxx web;
web1.xxx web1;
vless.xx.xx vless;
trojan.xx.xx trojan;
# 域名都不匹配情况下的默认值
default web;
}

# web,配置转发详情
upstream web {
server 127.0.0.1:10240;
}

# web,配置转发详情
upstream web1 {
server 127.0.0.1:10243;
}

# trojan,配置转发详情
upstream trojan {
server 127.0.0.1:10241;
}

# vmess,配置转发详情
upstream vless {
server 127.0.0.1:10242;
}

# 监听 443 并开启 ssl_preread
server {
listen 443 reuseport;
listen [::]:443 reuseport;
proxy_pass $backend_name;
ssl_preread on;
proxy_protocol on;
}
}

这里注意,这里要加入proxy_protocol on; 这句才能传递真实IP

然后,在/etc/nginx/conf.d/下加入网站配置文件

比如,web.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
server {
listen 80;
server_name web.xx;
return 301 https://$http_host$request_uri;
}

server {
listen 10243 ssl http2 proxy_protocol;
server_name web.xx;

location / {
proxy_pass http://127.0.0.1:10080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_set_header X-Forwarded-Proto https;
port_in_redirect off;
proxy_connect_timeout 300;
}

ssl_certificate /etc/letsencrypt/live/web.xx/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/web.xx/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

然后是web1.xx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
server {
listen 80;
server_name web1.xx;
return 301 https://$http_host$request_uri;
}

server {
listen 10240 ssl http2 proxy_protocol;
server_name web1.xx;
root /var/www/html/;

ssl_certificate /etc/letsencrypt/live/web1.xx/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/web1.xx/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

#Brotli Compression
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

location / {
root /var/www/web1/; #修改成自己存放的对应web1(别名)的文件路径
index index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {

}
add_header Strict-Transport-Security "max-age=63072000" always;
include /etc/nginx/default.d/*.conf;
}

vless.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
return 301 https://$http_host$request_uri;
}

server {
listen 127.0.0.1:81; #http/1.1 server,监听本地81端口。
listen 127.0.0.1:82 http2; #h2c server,监听本地82端口。

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; #启用HSTS

server_name vless.xx.xx;
root /var/www/xxx/; #回落网站

ssl_certificate /etc/ssl/xray/ve.xxx.xx/fullchain.pem;
ssl_certificate_key /etc/ssl/xray/ve.xxx.xx/privkey.pem;
}

此时trojan不需要配置,只需要在config.json里监听即可

配置config.json

config.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
{
"log": {
"loglevel": "warning",
"error": "/var/log/xray/error.log", //若使用v2ray,此处目录名称xray改成v2ray。
"access": "/var/log/xray/access.log" //若使用v2ray,此处目录名称xray改成v2ray。
},
"inbounds": [
{
"listen": "127.0.0.1",
"port": 10242, //监听端口
"protocol": "vless",
"settings": {
"clients": [
{
"id": "048e0bf2-dd56-11e9-aa37-5600024c1d6a", //修改成自己的UUID
"flow": "xtls-rprx-direct", //启用xtls,必须增加此条参数;否则删除。v2ray从v4.33.0版开始删除了xtls应用,如要使用此应用,建议选用xray。
"email": "vless@xx.xx"
}
],
"decryption": "none",
"fallbacks": [
{
"alpn": "h2", //h2回落匹配
"dest": "82" //回落给trojan+tcp处理,即@trojan-tcp程。
},
{
"dest": "81" //http/1.1回落进程
}
]
},
"streamSettings": {
"network": "tcp",
"security": "xtls", //如启用xtls,tls必须改成xtls;否则恢复tls。
"xtlsSettings": { //如启用xtls,tlsSettings必须改成xtlsSettings;否则恢复tlsSettings。
"alpn":[
"h2", //启用h2连接,web回落也需配置支持h2回落;否则不一致(裸奔),容易被墙探测出,从而被封。
"http/1.1" //启用http/1.1连接,web回落也需配置支持http/1.1回落;否则不一致(裸奔),容易被墙探测出,从而被封。
],
"minVersion": "1.2", //最低tls版本设置,Xray v1.1.4版及以后支持(可选项参数,可以不配置。)。目前v2ray不支持,若使用v2ray做服务端必须删除此项参数。
"preferServerCipherSuites": true, //首选服务端密码套件开关(与下面密码套件配合),Xray v1.1.4版及以后支持(可选项参数,可以不配置。)。目前v2ray不支持,若使用v2ray做服务端必须删除此项参数。
"cipherSuites": "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", //密码套件,Xray v1.1.4版及以后支持(可选项参数,可以不配置。)。目前v2ray不支持,若使用v2ray做服务端必须删除此项参数。
"certificates": [
{
"ocspStapling": 3600, //ocspStapling检查更新时间间隔。Xray v1.2.0版及以后支持(可选项参数,可以不配置。)。目前v2ray不支持,若使用v2ray做服务端必须删除此项参数。
"certificateFile": "/etc/ssl/xray/ve.xx.xx/fullchain.pem", //换成你的证书,绝对路径。
"keyFile": "/etc/ssl/xray/ve.xx.xx/privkey.pem" //换成你的私钥,绝对路径。
}
]
}
}
},
{
"listen": "127.0.0.1", //只监听本机,避免本机外的机器探测到下面端口。
"port": 10241, //监听端口
"protocol": "trojan",
"settings": {
"clients": [
{
"password":"user_password", //修改成自己密码
"flow": "xtls-rprx-direct", //启用xtls,必须增加此条参数;否则删除。v2ray从v4.33.0版开始删除了xtls应用,如要使用此应用,建议选用xray。
"email": "trojan@gmail.com"
}
],
"fallbacks": [
{
"dest": 82, //h2回落端口
"xver": 0 //关闭PROXY protocol发送,不发送真实来源IP和端口给nginx。另默认为0,此条参数可以省略不写。
}
]
},
"streamSettings": {
"network": "tcp",
"security": "xtls", //如启用xtls,tls必须改成xtls;否则恢复tls。
"xtlsSettings": { //如启用xtls,tlsSettings必须改成xtlsSettings;否则恢复tlsSettings。
"alpn":[
"h2" //启用h2连接,web回落也需配置支持h2回落;否则不一致(裸奔),容易被墙探测出,从而被封。
],
"minVersion": "1.2", //最低tls版本设置,Xray v1.1.4版及以后支持(可选项参数,可以不配置。)。目前v2ray不支持,若使用v2ray做服务端必须删除此项参数。
"preferServerCipherSuites": true, //首选服务端密码套件开关(与上配合),Xray v1.1.4版及以后支持(可选项参数,可以不配置。)。目前v2ray不支持,若使用v2ray做服务端必须删除此项参数。
"cipherSuites": "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", //密码套件,Xray v1.1.4版及以后支持(可选项参数,可以不配置。)。目前v2ray不支持,若使用v2ray做服务端必须删除此项参数。
"certificates": [
{
"ocspStapling": 3600, //ocspStapling检查更新时间间隔。Xray v1.2.0版及以后支持(可选项参数,可以不配置。)。目前v2ray不支持,若使用v2ray做服务端必须删除此项参数。
"certificateFile": "/etc/ssl/xray/ve.xx.xx/fullchain.pem", //换成你的证书,绝对路径。
"keyFile": "/etc/ssl/xray/ve.xx.xx/privkey.pem" //换成你的私钥,绝对路径。
}
]
}
},
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls"
]
}
}
],
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"type": "field",
"protocol": [
"bittorrent"
],
"outboundTag": "blocked"
}
]
},
"outbounds": [
{
"protocol": "freedom",
"settings": {}
},
{
"tag": "blocked",
"protocol": "blackhole",
"settings": {}
}
]
}

certbot申请证书

1
certbot certonly --standalone -d vless.xx.xx -d trojan.xx.xx

注意:由于某软件使用的是nobody账户,所以证书提示没有权限。

先建立一个nobody:nogroup的目录/etc/ssl/xray/vless.xx.xx

1
install -d -o nobody -g nogroup /etc/ssl/xray/vless.xx.xx

然后把certbot生成的证书转移过来

1
2
install -m 644 -o nobody -g nogroup /etc/letsencrypt/live/vless.xx.xx/fullchain.pem -t /etc/ssl/xray/vless.xx.xx
install -m 644 -o nobody -g nogroup /etc/letsencrypt/live/vless.xx.xx/privkey.pem -t /etc/ssl/xray/vless.xx.xx

没有解决的问题

目前起码是知道,fail2ban没有正常工作