V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
stille
V2EX  ›  NGINX

求教一个 Nginx 配置,有伪静态还想 sub_filter

  •  
  •   stille ·
    stilleshan · 2019-11-25 20:47:40 +08:00 · 3157 次点击
    这是一个创建于 1586 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前提

    有个文件目录源码 Directory Lister 我装到 LNMP 环境下.按照要求需要配伪静态

    location / {
     rewrite /(.*)/$ /index.php?dir=$1 last;
     }
    

    挂载了 COS

    尝试挂载了腾讯云 COS 到这个网站程序主目录,打开网站能正常列出 COS 里的目录.并且可以下载.

    需求

    现在想用sub_filter语法把页面显示的下载路径替换为 COS 的域名.于是打算用

        location / {
            sub_filter '<script src="https://xxx.xxx.com'  ' <script src="https://cdn.xxx.com';
            sub_filter_once off;
             }
    

    但是无法生效.

    Nginx 肯定是支持的,另外一个目录程序可以用sub_filter替换成功,但是那个程序不需要伪静态,所以有可能是这方面冲突..主要另外一个程序无法索引挂载的 COS 里的文件,所以才尝试使用 Directory Lister

    因为不是很懂这一块,试了很多办法,都不行...特来求大神们帮帮忙

    6 条回复    2019-11-25 22:28:06 +08:00
    eason1874
        1
    eason1874  
       2019-11-25 21:10:35 +08:00 via Android
    你重写到哪个路径就在哪个路径下 sub_filter 就行了,像你重写到 index.php ,那你肯定还有一个 location 是匹配 php 把它转发给 php 后端的,你就在那里写就可以了。
    stille
        2
    stille  
    OP
       2019-11-25 21:22:59 +08:00
    @eason1874 老哥,我把 Nginx 配置和网站目录结构发出来了...麻烦帮看下 sub_filter 改写在哪..

    图中下面那个 index.php 是根目录的,上面那个应该是 rewrite 过去真正的 index.php.

    结构图
    https://img.ioiox.com/2019/11/25/15746879941916.jpg

    Nginx 配置
    ```
    server
    {
    listen 80;
    #listen [::]:80;
    server_name www.xxx.com ;
    index index.html index.htm index.php default.html default.htm default.php;
    root /home/wwwroot/www.xxx.com;

    location / {
    rewrite /(.*)/$ /index.php?dir=$1 last;
    }

    include enable-php.conf;

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires 30d;
    }

    location ~ .*\.(js|css)?$
    {
    expires 12h;
    }

    location ~ /.well-known {
    allow all;
    }

    location ~ /\.
    {
    deny all;
    }

    access_log off;
    }

    server
    {
    listen 443 ssl http2;
    #listen [::]:443 ssl http2;
    server_name www.xxx.com ;
    index index.html index.htm index.php default.html default.htm default.php;
    root /home/wwwroot/www.xxx.com;

    ssl_certificate /usr/local/nginx/conf/ssl/xxx.crt;
    ssl_certificate_key /usr/local/nginx/conf/ssl/xxx.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
    ssl_session_cache builtin:1000 shared:SSL:10m;
    # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
    ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

    include rewrite/none.conf;
    #error_page 404 /404.html;

    # Deny access to PHP files in specific directory
    #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

    include enable-php.conf;

    location / {
    rewrite /(.*)/$ /index.php?dir=$1 last;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires 30d;
    }

    location ~ .*\.(js|css)?$
    {
    expires 12h;
    }

    location ~ /.well-known {
    allow all;
    }

    location ~ /\.
    {
    deny all;
    }

    access_log off;
    }

    ```
    eason1874
        3
    eason1874  
       2019-11-25 21:27:49 +08:00 via Android
    @stille 这个跟你网站代码没有关系。

    看样子你的 index.php 是在 enable-php.conf 这个文件里的 location 段,php 转发一般就是 \.php$ 结尾,你加到里面试试
    stille
        4
    stille  
    OP
       2019-11-25 22:05:48 +08:00
    @eason1874 谢谢老哥,现在 sub_filter 可以了..不过还有个问题出现了..不知道能否解决
    就是在当前目录有文件夹,也有文件时....文件替换成 cdn 地址了..文件夹地址也替换了..那肯定就进不去...这个不知道有办法解决么?
    <a href="/projects/ 替换为 <a href="https://cdn.xxx.com/projects/
    下载地址成功变为 https://cdn.xxx.com/projects/1.zip
    但是 321 目录也变为 https://cdn.xxx.com/321/ 了这样是无法访问的..

    https://img.ioiox.com/2019/11/25/15746906328803.jpg


    <li data-name="321" data-href="/projects/321/">
    <a href="/projects/321/" class="clearfix" data-name="321">

    <li data-name="1.zip" data-href="/projects/1.zip">
    <a href="/projects/1.zip" class="clearfix" data-name="1.zip">
    eason1874
        5
    eason1874  
       2019-11-25 22:21:46 +08:00
    @stille #4 如果我没记错的话,sub_filter 是不支持正则匹配的,只能替换字符串。

    如果你这个网页只是自己用用,告诉你一个比较简单的方法,通过 sub_filter 替换 </body> 或者修改源代码引入一个 JS 文件,通过 JS 代码去修改路径。

    或者用 ngx_lua 模块。
    stille
        6
    stille  
    OP
       2019-11-25 22:28:06 +08:00
    @eason1874 再次感谢,已经很好解决问题了..我去掉伪静态,文件夹就不是绝对路径了.这样就和文件的绝对路径区分开了,就可以 sub_filter 了...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2861 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 12:59 · PVG 20:59 · LAX 05:59 · JFK 08:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.