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

2019-11-25 20:47:40 +08:00
 stille

前提

有个文件目录源码 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

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

3181 次点击
所在节点    NGINX
6 条回复
eason1874
2019-11-25 21:10:35 +08:00
你重写到哪个路径就在哪个路径下 sub_filter 就行了,像你重写到 index.php ,那你肯定还有一个 location 是匹配 php 把它转发给 php 后端的,你就在那里写就可以了。
stille
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
2019-11-25 21:27:49 +08:00
@stille 这个跟你网站代码没有关系。

看样子你的 index.php 是在 enable-php.conf 这个文件里的 location 段,php 转发一般就是 \.php$ 结尾,你加到里面试试
stille
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
2019-11-25 22:21:46 +08:00
@stille #4 如果我没记错的话,sub_filter 是不支持正则匹配的,只能替换字符串。

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

或者用 ngx_lua 模块。
stille
2019-11-25 22:28:06 +08:00
@eason1874 再次感谢,已经很好解决问题了..我去掉伪静态,文件夹就不是绝对路径了.这样就和文件的绝对路径区分开了,就可以 sub_filter 了...

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/623053

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX