Apache .htaccess 重写规则到 NGINX 转换规则无效

Apache .htaccess 重写规则到 NGINX 转换规则无效

我在安装了 Debian 10 的 aaPanel 上运行网站“PHP 脚本”。一切正常,但图像不显示/损坏

/image/ 路径是重写规则,它解析为 image.php,从 uploads 文件夹加载图像。将 .htaccess 转换为 Nginx 并将它们添加到系统后,所有图像路径都将转到

www.website.com/image/m/112/112/335757712_1152702765_706821275.jpeg

实际路径应该是

www.website.com/uploads/media/112/112/335757712_1152702765_706821275.jpeg

以下是 .htaccess 规则

RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*)                                       $1                                              [L]
RewriteRule ^(([^/]*)+)?$                               index.php?a=$1                                  [L]

RewriteRule ^welcome/?$                                 index.php?a=welcome                             [NC]

RewriteRule ^stream/?$                                  index.php?a=stream                              [NC]
RewriteRule ^stream/logout$                             index.php?a=stream&logout                       [NC]

RewriteRule ^explore/?$                                 index.php?a=explore                             [NC]
RewriteRule ^explore/filter/([^/]+)/?$                  index.php?a=explore&filter=$1                   [NC]
RewriteRule ^explore/popular$                           index.php?a=explore&popular                     [NC]
RewriteRule ^explore/liked$                             index.php?a=explore&liked                       [NC]

RewriteRule ^history/?$                                 index.php?a=history                             [NC]

RewriteRule ^upload/?$                                  index.php?a=upload                              [NC]

RewriteRule ^pro/?$                                     index.php?a=pro                                 [NC]

RewriteRule ^stats/?$                                   index.php?a=stats                               [NC]
RewriteRule ^stats/filter/([^/]+)/?$                    index.php?a=stats&filter=$1                     [NC]

RewriteRule ^profile/([^/]+)/?$                         index.php?a=profile&u=$1                        [NC]
RewriteRule ^profile/([^/]+)/([^/]+)/?$                 index.php?a=profile&u=$1&r=$2                   [NC]
RewriteRule ^profile/([^/]+)/filter/([^/]+)/?$          index.php?a=profile&u=$1&filter=$2              [NC]

RewriteRule ^notifications/?$                           index.php?a=notifications                       [NC]
RewriteRule ^notifications/filter/([^/]+)/?$            index.php?a=notifications&filter=$1             [NC]

RewriteRule ^settings/?$                                index.php?a=settings                            [NC]
RewriteRule ^settings/([^/]+)/?$                        index.php?a=settings&b=$1                       [NC]

RewriteRule ^messages/?$                                index.php?a=messages                            [NC]
RewriteRule ^messages/([^/]+)/([^/]+)/?$                index.php?a=messages&u=$1&id=$2                 [NC]

RewriteRule ^track/([^/]+)/?$                           index.php?a=track&id=$1                         [NC]
RewriteRule ^track/([^/]+)/edit/?$                      index.php?a=track&id=$1&type=edit               [NC]
RewriteRule ^track/([^/]+)/report/?$                    index.php?a=track&id=$1&type=report             [NC]
RewriteRule ^track/([^/]+)/stats/?$                     index.php?a=track&id=$1&type=stats              [NC]
RewriteRule ^track/([^/]+)/likes/?$                     index.php?a=track&id=$1&type=likes              [NC]
RewriteRule ^track/([^/]+)/stats/filter/([^/]+)/?$      index.php?a=track&id=$1&type=stats&filter=$2    [NC]
RewriteRule ^track/([^/]+)/([^/]+)/?$                   index.php?a=track&id=$1&name=$2                 [NC]

RewriteRule ^playlist/([^/]+)/?$                        index.php?a=playlist&id=$1                      [NC]
RewriteRule ^playlist/([^/]+)/edit/?$                   index.php?a=playlist&id=$1&edit=true            [NC]
RewriteRule ^playlist/([^/]+)/([^/]+)/?$                index.php?a=playlist&id=$1&name=$2              [NC]

RewriteRule ^search/filter/([^/]+)/([^/]+)/?$           index.php?a=search&filter=$1&q=$2               [NC]

RewriteRule ^page/([^/]+)/?$                            index.php?a=page&b=$1                           [NC]

RewriteRule ^recover/?$                                 index.php?a=recover                             [NC]
RewriteRule ^recover/do/?$                              index.php?a=recover&r=1                         [NC]

RewriteRule ^image/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$   image.php?t=$1&w=$2&h=$3&src=$4

以下是 Nginx 转换后的规则

location / {
  if (-e $request_filename){
    rewrite ^/(.*) /$1 break;
  }
  rewrite ^/(([^/]*)+)?$ /index.php?a=$1 break;
}

location /welcome {
  rewrite ^/welcome/?$ /index.php?a=welcome;
}

location /stream {
  rewrite ^/stream/?$ /index.php?a=stream;
}

location = /stream/logout {
  rewrite ^(.*)$ /index.php?a=stream&logout;
}

location /explore {
  rewrite ^/explore/?$ /index.php?a=explore;
  rewrite ^/explore/filter/([^/]+)/?$ /index.php?a=explore&filter=$1;
}

location = /explore/popular {
  rewrite ^(.*)$ /index.php?a=explore&popular;
}

location = /explore/liked {
  rewrite ^(.*)$ /index.php?a=explore&liked;
}

location /history {
  rewrite ^/history/?$ /index.php?a=history;
}

location /upload {
  rewrite ^/upload/?$ /index.php?a=upload;
}

location /pro {
  rewrite ^/pro/?$ /index.php?a=pro;
}

location /stats {
  rewrite ^/stats/?$ /index.php?a=stats;
  rewrite ^/stats/filter/([^/]+)/?$ /index.php?a=stats&filter=$1;
}

location /profile {
  rewrite ^/profile/([^/]+)/?$ /index.php?a=profile&u=$1;
  rewrite ^/profile/([^/]+)/([^/]+)/?$ /index.php?a=profile&u=$1&r=$2;
  rewrite ^/profile/([^/]+)/filter/([^/]+)/?$ /index.php?a=profile&u=$1&filter=$2;
}

location /notifications {
  rewrite ^/notifications/?$ /index.php?a=notifications;
  rewrite ^/notifications/filter/([^/]+)/?$ /index.php?a=notifications&filter=$1;
}

location /settings {
  rewrite ^/settings/?$ /index.php?a=settings;
  rewrite ^/settings/([^/]+)/?$ /index.php?a=settings&b=$1;
}

location /messages {
  rewrite ^/messages/?$ /index.php?a=messages;
  rewrite ^/messages/([^/]+)/([^/]+)/?$ /index.php?a=messages&u=$1&id=$2;
}

location /track {
  rewrite ^/track/([^/]+)/?$ /index.php?a=track&id=$1;
  rewrite ^/track/([^/]+)/edit/?$ /index.php?a=track&id=$1&type=edit;
  rewrite ^/track/([^/]+)/report/?$ /index.php?a=track&id=$1&type=report;
  rewrite ^/track/([^/]+)/stats/?$ /index.php?a=track&id=$1&type=stats;
  rewrite ^/track/([^/]+)/likes/?$ /index.php?a=track&id=$1&type=likes;
  rewrite ^/track/([^/]+)/stats/filter/([^/]+)/?$ /index.php?a=track&id=$1&type=stats&filter=$2;
  rewrite ^/track/([^/]+)/([^/]+)/?$ /index.php?a=track&id=$1&name=$2;
}

location /playlist {
  rewrite ^/playlist/([^/]+)/?$ /index.php?a=playlist&id=$1;
  rewrite ^/playlist/([^/]+)/edit/?$ /index.php?a=playlist&id=$1&edit=true;
  rewrite ^/playlist/([^/]+)/([^/]+)/?$ /index.php?a=playlist&id=$1&name=$2;
}

location /search {
  rewrite ^/search/filter/([^/]+)/([^/]+)/?$ /index.php?a=search&filter=$1&q=$2;
}

location /page {
  rewrite ^/page/([^/]+)/?$ /index.php?a=page&b=$1;
}

location /recover {
  rewrite ^/recover/?$ /index.php?a=recover;
  rewrite ^/recover/do/?$ /index.php?a=recover&r=1;
}

location /image {
  rewrite ^/image/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /image.php?t=$1&w=$2&h=$3&src=$4;
}

我很困惑,想不出任何办法来解决这个问题。谢谢

答案1

重写规则转换器通常很糟糕。最好以本机 nginx 方式实现。在这种情况下,位置image如下:

location ~ ^/image/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ {
    try_files /image.php?t=$1&w=$2&h=$3&src=$4 =404;
}

顺便说一句。我希望您image.php对宽度/高度有适当的输入验证,以便攻击者无法通过请求 1M x 1M 版本的图像对您的服务器进行 DDoS 攻击...

相关内容