NGINX双栈反代配置
先决条件
家里无公网ipv4却又想让纯ipv4用户访问,并且使ipv6用户或者双栈用户请求家里的公网ipv6服务器,节省服务器资源。有公网ipv4这篇不用看了….
云服务器具有双栈,即具有公网ipv6+公网ipv4(这里笔者使用的是hostyun的香港-epcy-vps系列,主要是高带宽+高流量)
家庭网络具备公网ipv6(网上找教程,运营商申请+路由转桥接,笔者这里不用向运营商申请,改了桥接自带公网ipv6)
系统:ubuntu2204
云服务端开启ipv6
原配置文件
vim /etc/netplan/01-netcfg.yaml
# Created By Cloud System
network:
ethernets:
ens3:
match:
macaddress: ae:bb:0c:7d:9a:b4
addresses: [公网ipv4/24]
gateway4: 公网ipv4网关
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
dhcp4: false
dhcp6: false
version: 2
renderer: networkd
#2
开启ipv6配置文件
vim /etc/netplan/01-netcfg.yaml
# Created By Cloud System
network:
ethernets:
ens3:
match:
macaddress: ae:bb:0c:7d:9a:b4
addresses: [公网ipv4/24, '公网ipv6']
gateway4: 公网ipv4网关
gateway6: 公网ipv6网关
nameservers:
addresses: [8.8.8.8, 8.8.4.4, 2001:4860:4860::8888, 2001:4860:4860::8844]
dhcp4: false
dhcp6: false
version: 2
renderer: networkd
netplan apply
设置访问网络时ipv4优先(我这台机子的ipv6访问很慢,不知为啥)
vim /etc/gai.conf
precedence ::ffff:0:0/96 100
netplan apply
配置反代
将ipv6用户的访问直接转到家里公网,节省服务器资源
proxy_pass https://10.1.1.2;
nextcloud服务所在地址,此步骤是通过使用内网穿透,将家庭局域网(无公网ipv4)代理到云服务器
**如若想代理到特定的端口号,可添加$server_port
**参数
sudo vim /etc/nginx/conf.d/nextcloud.conf
server {
listen 80;
listen [::]:80;
server_name example.com;
rewrite ^(.*) https://$host$1 permanent;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
index index.php index.html index.htm;
server_name_in_redirect off;
access_log /var/log/nginx/access.log; # 请修改为你的log地址
error_log /var/log/nginx/error.log; # 请修改为你的log地址
# 可以设置独立的 SSL 认证
ssl_certificate /opt/ssl/fullchain.cer;
ssl_certificate_key /opt/ssl/cert.key;
client_max_body_size 5120000M;
client_body_timeout 300s;
fastcgi_buffers 128 8K;
ssl_session_timeout 5m;
ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384';
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# 判断是否是手机端
location / {
if ($http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)") {
return 302 https://nc6.glxaa.com;
}
# 判断用户使用的是否是ipv6
if ($remote_addr ~* "^([a-f0-9:]+)$") {
return 302 https://nc6.glxaa.com;
}
tcp_nodelay on;
proxy_set_header Host $host;
add_header Access-Control-Allow-Origin *;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
# 设置是否允许 cookie 传输
add_header Access-Control-Allow-Credentials true;
# 允许请求地址跨域 * 做为通配符
add_header Access-Control-Allow-Origin * always;
# 允许跨域的请求方法
add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_connect_timeout 600;
proxy_redirect off;
proxy_pass https://10.1.1.2;
if ($request_method = 'OPTIONS') {
return 204;
}
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
一:nextcloud27+nginx +ssl+各种优化+双栈的傻瓜式安装教程
三:nextcloud27+nginx 使用家庭公网ipv6+云服务器公网ipv4配置双栈并将特定的请