需求背景:
原来的网关入口为 https://testgw.foobar.cn/,也就是说 / 这个 uri 已经 proxy_pass 了后端服务,
现在要在 / 这个 uri 加上微信公众号的js接口安全域名校验文件 MP_verify_7UJT32UzCOGkaUNB.txt,实现能访问 https://testgw.foobar.cn/MP_verify_7UJT32UzCOGkaUNB.txt 这个地址,
那么 nginx 要如何实现访问 / 这个 uri,既能访问到 /MP_verify_7UJT32UzCOGkaUNB.txt,又能 proxy_pass 到后端服务呢?
1、原来的回调网关配置
# cat testgw.foobar.conf
server {
listen 443;
server_name testgw.foobar.cn;
access_log logs/testgw_access.log;
ssl on;
ssl_certificate /usr/local/openresty/nginx/conf/cert/foobar.pem;
ssl_certificate_key /usr/local/openresty/nginx/conf/foobar.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8000;
}
}
2、现在要在 location /{}加上校验文件的访问,访问路径为 https://testgw.foobar.cn/MP_verify_7UJT32UzCOGkaUNB.txt,实现的配置如下
# cat testgw.foobar.cn.conf
server {
listen 443;
server_name testgw.foobar.cn;
access_log logs/testgw_access.log;
ssl on;
ssl_certificate /usr/local/openresty/nginx/conf/cert/foobar.pem;
ssl_certificate_key /usr/local/openresty/nginx/conf/cert/foobar.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
root /data/wx; # MP_verify_7UJT32UzCOGkaUNB.txt 文件放在了 /data/wx 目录下
location / {
# 访问 https://testgw.foobar.cn/MP_verify_7UJT32UzCOGkaUNB.txt 时 --> try_files $uri --> try_files /MP_verify_7UJT32UzCOGkaUNB.txt --> /data/wx/MP_verify_7UJT32UzCOGkaUNB.txt --> 实现了访问
# 访问 https://testgw.foobar.cn/网关转发的uri/xxx 时 --> try_files $uri --> try_files /网关转发的uri/xxx --> /data/wx/网关转发的uri/xxx 不存在 --> try_files @gateway --> location @gateway --> proxy_pass http://127.0.0.1:8000 --> 实现了访问
try_files $uri @gateway;
}
location @gateway {
proxy_set_header Host $http_host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8000;
}
}
3、实现效果
现在 https://testgw.foobar.cn/MP_verify_7UJT32UzCOGkaUNB.txt 可以访问了,https://testgw.foobar.cn/网关转发的uri/xxx 也可以访问了
网友评论