美文网首页
Nginx 实现 location / {} 既能访问 txt

Nginx 实现 location / {} 既能访问 txt

作者: awker | 来源:发表于2020-03-10 17:43 被阅读0次

需求背景:


原来的网关入口为 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 也可以访问了

相关文章

  • Nginx 实现 location / {} 既能访问 txt

    需求背景: 原来的网关入口为 ,也就是说 / 这个 uri 已经 proxy_pass 了后端服务, 现在要在 /...

  • 新架构第3天

    Q1、基于location实现基于域名虚拟主机 测试结果: Q2、nginx自定义日志路径,配置访问日志为json...

  • linux学习--week16--nginx-lnmp

    回顾及今日内容:1.nginx功能扩展1.1 用户访问网站流程1.2 nginx内置变量1.3 location规...

  • Nginx 匹配规则

    Nginx 内核源码解析 nginx location 练习 Nginx location 配置踩坑过程分享ngi...

  • nginx排除目录

    目的是为了禁止别人访问项目下的一些文件和目录 用到nginx的location 上述代码意为 排除nginx、lo...

  • Nginx location篇

    nginx的location和nginx.conf中location的书写顺序没有太大的关系 location 定...

  • root和alias的区别

    nginx location模块下有root和alias关键字 例如访问 *.url.com/dir/1.jpg:...

  • Nginx访问静态资源

    Nginx访问静态资源 即通过IP:端口/文件名 访问文件实现. 修改Nginx配置 重新加载Nginx 上传文件测试

  • nginx配置location [=|~|~*|~] /uri/

    nginx location语法 基本语法:location [=||*|^~] /uri/ { … } 示例

  • D-46网站服务配置过程

    一、nginx程序location配置方法 作用: 匹配指定的uri信息,可以根据访问不同的uri信息,做出不同处...

网友评论

      本文标题:Nginx 实现 location / {} 既能访问 txt

      本文链接:https://www.haomeiwen.com/subject/mmlgdhtx.html