VPS部署好后常常一年半载都不会再登录修改。本人记性不好,每次迁移往往需要重新查资料部署服务,因此记录一下个人常用配置,随时更新,以免再忘。
Nginx
本节的主要目的是使我们部署的网站支持HTTP2和TLSv1.3,并开启一些必要的功能,保证网站安全。
安装
一些比较早期的博客会提到需要使用支持TLS1.3的openssl库进行编译。几年后的现在,ubuntu自带的包管理器下载到的nginx已经直接支持开启TLS v1.3了,版本信息如下:
1nginx version: nginx/1.18.0 (Ubuntu)
2built with OpenSSL 3.0.2 15 Mar 2022
3TLS SNI support enabled
4configure arguments: --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-zctdR4/nginx-1.18.0=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -Wl,-z,now -fPIC' --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-compat --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 --add-dynamic-module=/build/nginx-zctdR4/nginx-1.18.0/debian/modules/http-geoip2 --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_moduleHTTP请求转发
正式开始前,我先禁掉了HTTP访问,将所有HTTP请求重定向到对应的HTTPS版本,确保用户始终通过加密连接访问网站。后续我们假设域名为x.domain.com:
1server {
2 listen 80;
3 listen [::]:80;
4 server_name x.domain.com www.x.domain.com y.x.domain.com;
5
6 location / {
7 return 301 https://$host$request_uri;
8 }
9}启用HTTP2和TLSv1.3
nginx默认配置路径在/etc/nginx/nginx.conf这里,进入文件编辑:
1http {
2 # ...
3
4 ##
5 # SSL Settings
6 ##
7 ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # 检查TLSv1.3是否打开
8 ssl_prefer_server_ciphers on;
9
10 # ...
11
12 # custom
13 # ...
14 map $scheme $hsts_header {
15 https "max-age=63072000;includeSubDomains; preload";
16 }
17 server {
18 listen 443 ssl http2;
19 listen [::]:443 ssl http2;
20 server_name x.domain.com www.x.domain.com;
21
22 ssl_certificate /root/cert/x.domain.com.cer;
23 ssl_certificate_key /root/cert/x.domain.com.key;
24 add_header Strict-Transport-Security $hsts_header always;
25 ssl_protocols TLSv1.2 TLSv1.3;
26 ssl_ciphers TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-256-GCM-SHA384:TLS13-AES-128-GCM-SHA256:EECDH+CHACHA20:EECDH+AESGCM:EECDH+AES;
27 ssl_prefer_server_ciphers on;
28 ssl_session_cache shared:SSL:1m;
29 ssl_verify_depth 10;
30 ssl_session_timeout 30m;
31 ssl_stapling on;
32 ssl_stapling_verify on;
33 ssl_trusted_certificate /root/cert/fullchain.cer;
34
35 location / {
36 root /var/www/html;
37 }
38 }配置中有以下几个要点:
要添加http2,在对应的ssl监听端口上加上
http2字段即可。对应listen 443 ssl http2;添加TLS1.3则是在
ssl_protocols指令中加上TLSv1.3,即ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers这里的配置由于只启用了TLS v1.2和TLS v1.3,因此直接弃用了大量旧的和弱的加密套件
为了使用加密需要申请证书,此处我使用了放在cert文件夹里从Let's Encrypt申请的现成证书,并设置好脚本自动更新。
反向代理
VPS上部署有网页应用,在指定端口(假设为1234)上监听。为了方便访问,我添加了反向代理使得应用能通过y.x.domain.com访问:
1server {
2 listen 443 ssl http2;
3 listen [::]:443 ssl http2;
4 server_name y.x.domain.com;
5 ssl_certificate /root/cert/x.domain.com.cer;
6 ssl_certificate_key /root/cert/x.domain.com.key;
7 # ...
8 location / {
9 proxy_pass https://localhost:1234;
10 proxy_set_header Host $proxy_host;
11 proxy_set_header X-Real-IP $remote_addr;
12 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
13 add_header Strict-Transport-Security $hsts_header always;
14 }
15}其他设置与上一节保持一致,配置的最后将对于y.x.domain.com的请求转发到本机1234端口上的服务。
IPv6地址申请
有些VPS厂家提供的机器没有自带IPv6,然而某些pt站除去教育网外只支持境外IPv6访问,因此对我来说有个IPv6地址还是很重要的。
目前我使用的解决方案是wgcf,一个Cloudflare warp的非官方CLI工具,可以模拟warp客户端注册账号,并生成通用的WireGuard配置文件。
安装与注册
warp本身是Cloudflare提供的一项基于WireGuard的网络流量安全及加速服务,因此配置第一步是安装WireGuard。
1sudo apt install wireguard接着从GitHub release界面获取最新版本的wgcf后并安装:
1install -Dm755 ./wgcf /usr/local/bin/wgcf运行wgcf register注册warp账户。运行wgcf generate生成WireGuard配置文件。现在文件夹下就会出现两个配置文件:wgcf-account.toml以及wgcf-profile.conf。
配置与测试
打开profile文件,默认配置如下:
1[Interface]
2PrivateKey = CLK...Wmg=
3Address = 172.16.0.2/32
4Address = 2606:...:9f84/128
5DNS = 1.1.1.1
6MTU = 1280
7[Peer]
8PublicKey = bm...yo=
9AllowedIPs = 0.0.0.0/0
10AllowedIPs = ::/0
11Endpoint = engage.cloudflareclient.com:2408AllowedIPs = 0.0.0.0/0使得VPS的IPv4流量被WireGuard接管,并以NAT方式访问外部网络。同理,AllowedIPs = ::/0使WireGuard接管VPS所有的IPv6流量。
因此,为了添加IPv6地址,只需删除AllowedIPs = 0.0.0.0/0这一行。并在DNS行按需添加合适的IPv6 DNS服务器。
最后复制配置文件:
1cp wgcf-profile.conf /etc/wireguard/wgcf.conf开启网络接口测试效果:
1wg-quick up wgcfcurl一下IPv6的测试网站,返回地址说明VPS成功开启IPv6:
1curl 6.ipw.cn # 返回一个cloudflare提供的IPv6地址如果想在浏览器上测试效果,也可以直接访问ipw.cn或者test-ipv6.com。
完成测试后关闭接口:
1wg-quick down wgcf使用systemd正式开启服务:
1systemctl enable wg-quick@wgcf --now另外需要注意的是,这样的操作只能让VPS能够访问IPv6 only的网站,而用户并不能通过获取的IPv6地址访问VPS。需要区分两者之间的区别。