前提
有个文件目录源码 Directory Lister
我装到 LNMP 环境下.按照要求需要配伪静态
location / {
rewrite /(.*)/$ /index.php?dir=$1 last;
}
挂载了 COS
尝试挂载了腾讯云 COS 到这个网站程序主目录,打开网站能正常列出 COS 里的目录.并且可以下载.
需求
现在想用sub_filter
语法把页面显示的下载路径替换为 COS 的域名.于是打算用
location / {
sub_filter '
但是无法生效.
Nginx 肯定是支持的,另外一个目录程序可美国服务器以用sub_filter
替换成功,但是那个程序不需要伪静态,所以有可能是这方面冲突..主要另外一个程序无法索引挂载的 COS 里的文件,所以才尝试使用 Directory Lister
因为不是很懂这一块,试了很多办法,都不行...特来求大神们帮帮忙
你重写到哪个路径就在哪个路径下 sub_filter 就行了,像你重写到 index.php ,那你肯定还有一个 location 是匹配 php 把它转发给 php 后端的,你就在那里写就可以了。
老哥,我把 Nginx 配置和网站目录结构发出来了...麻烦帮看下 sub_filter 改写在哪..
图中下面那个 index.php 是根目录的,上面那个应该是 rewrite 过去真正的 index.php.
结构图
https://img.ioiox.com/2019/11/25/15746879941916.jpg
Nginx 配置
```
server
{
listen 80;
[::]: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;
[::]: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;
404 /404.html;
Deny access to PHP files in specific directory
~ /(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;
}
```
这个跟你网站代码没有关系。
看样子你的 index.php 是在 enable-php.conf 这个文件里的 location 段,php 转发一般就是 \.php$ 结尾,你加到里面试试
谢谢老哥,现在 sub_filter 可以了..不过还有个问题出现了..不知道能否解决
就是在当前目录有文件夹,也有文件时....文件替换成 cdn 地址了..文件夹地址也替换了..那肯定就进不去...这个不知道有办法解决么?
下载地址成功变为 https://cdn.xxx.com/projects/1.zip
但是 321 目录也变为 https://cdn.xxx.com/321/ 了这样是无法访问的..
https://img.ioiox.com/2019/11/25/15746906328803.jpg
如果我没记错的话,sub_filter 是不支持正则匹配的,只能替换字符串。
如果你这个网页只是自己用用,告诉你一个比较简单的方法,通过 sub_filter 替换