Nginx

Apache .htaccess 將規則重寫為 NGINX 轉換的規則無效

  • October 5, 2021

我正在安裝 Debian 10 的 aaPanel 上執行網站“PHP 腳本”。一切正常,但圖像未顯示/損壞

/image/ 路徑是一個重寫規則,它解析為從上傳文件夾載入圖像的 image.php。將 .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;
}

我很困惑,我想不出任何辦法來解決這個問題。謝謝

重寫規則轉換器通常很糟糕。最好以原生 nginx 的方式實現。在這種情況下,image位置如下:

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

順便提一句。我希望您image.php對寬度/高度進行正確的輸入驗證,以便攻擊者無法通過詢問 1M x 1M 版本的圖像來對您的伺服器進行 DDoS…

引用自:https://serverfault.com/questions/1079574