Nginx

Apache .htaccess 到 NGINX RewriteRules 埠

  • July 11, 2021

所以,我實際上正在嘗試將RewriteRules 從Apache移植到NGINX,但似乎我無法完全移植。

實際上,在我的伺服器上,我確實在伺服器上的https://example.com域和/var/www/html/路徑上有一個正在執行的站點。我要做的是在var/www/html/subdirectorypath 和 domain下的子目錄中安裝自定義腳本https://example.com/subdirectory

問題是重寫規則不起作用,甚至出現 404 not found 錯誤。請幫助我。

我的 Apache.htaccess文件:

RewriteRule ^page/?$ pages/page.php [L]
RewriteRule ^about/?$ pages/about.php [L]
RewriteRule ^privacy-policy/?$ pages/privacy-policy.php [L]
RewriteRule ^contact/?$ pages/contact.php [L]
RewriteRule ^terms/?$ pages/tos.php [L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) - [PT,L]

RewriteRule ^sitemap-([0-9]+).xml$ parts/sitemaps/sitemap-$1.xml [QSA,L]

RewriteRule ^(.*)/(.*)/(.*)/(.*)/?$ index.php?bank=$1&state=$2&district=$3&branch=$4 [QSA,L]
RewriteRule ^(.*)/(.*)/(.*)/?$ index.php?bank=$1&state=$2&district=$3 [QSA,L]
RewriteRule ^(.*)/(.*)/?$ index.php?bank=$1&state=$2 [QSA,L]
RewriteRule ^(.*)/?$ index.php?bank=$1 [QSA,L]

和**NGINX config我試圖移植的文件:**

server
{
 listen 80 default_server;
 listen [::]:80 default_server;

 root /var/www/html;

 # Add index.php to the list if you are using PHP
 index index.php index.html;

 server_name localhost;

 location /
 {
   try_files $uri $uri/ =404;
 }

 # pass PHP scripts to FastCGI server
 location ~ \.php$
 {
   include snippets/fastcgi-php.conf;
   #       # With php-fpm (or other unix sockets):
   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
   #       # With php-cgi (or other tcp sockets):
   #       fastcgi_pass 127.0.0.1:9000;
 }

 location /subdirectory
 {

   root /var/www/html/subdirectory;
   index index.php;
   try_files $uri $uri/ /index.php$args$query_string

   location ~ ^/(.+)
   {
   }

   location /page
   {
     rewrite ^/page/?$ /pages/page.php break;
   }

   location /about
   {
     rewrite ^/about/?$ /pages/about.php break;
   }

   location /privacy
   {
     rewrite ^/privacy-policy/?$ /pages/privacy-policy.php break;
   }

   location /contact
   {
     rewrite ^/contact/?$ /pages/contact.php break;
   }

   location /terms
   {
     rewrite ^/terms/?$ /pages/tos.php break;
   }

   location /
   {
     if (-e $request_filename)
     {
       rewrite ^/sitemap-([0-9]+).xml$ /parts/sitemaps/sitemap-$1.xml break;
     }
     rewrite ^/(.*)/(.*)/(.*)/(.*)/?$ /index.php?bank=$1&state=$2&district=$3&branch=$4 break;
     rewrite ^/(.*)/(.*)/(.*)/?$ /index.php?bank=$1&state=$2&district=$3 break;
     rewrite ^/(.*)/(.*)/?$ /index.php?bank=$1&state=$2 break;
     rewrite ^/(.*)/?$ /index.php?bank=$1 break;
   }

   location ~ /subdirectory /(.+\.php)$
   {
     include snippets/fastcgi-php.conf;
     # With php-fpm (or other unix sockets):
     fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     # With php-cgi (or other tcp sockets):
     # fastcgi_pass 127.0.0.1:9000;
   }

 }
}

這應該適用於您的情況。靈感來自可用的解決方案 @ https://serversforhackers.com/c/nginx-php-in-subdirectory

最終配置文件:

server
{
 listen 80 default_server;
 listen [::]:80 default_server;

 root /var/www/html;

 # Add index.php to the list if you are using PHP
 index index.php index.html;

 server_name localhost;

 location /
 {
   try_files $uri $uri/ =404;
 }

 # pass PHP scripts to FastCGI server
 location ~ \.php$
 {
   include snippets/fastcgi-php.conf;
   #       # With php-fpm (or other unix sockets):
   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
   #       # With php-cgi (or other tcp sockets):
   #       fastcgi_pass 127.0.0.1:9000;
 }

 # for the sub-directory
 location /subdirecory
 {
   alias /var/www/html/subdirectory; 
   try_files $uri $uri/ @subdirectory; # send all the request to @subdirectory tagged location

   location ~ \.php$
   {
     include snippets/fastcgi-php.conf;
     fastcgi_param SCRIPT_FILENAME $request_filename;
     fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
   }
 }

 location @subdirectory
 {
   rewrite /subdirectory/about$ /subdirectory/pages/about.php last;
   rewrite /subdirectory/privacy-policy$ /subdirectory/pages/privacy-policy.php last;
   rewrite /subdirectory/contact$ /subdirectory/pages/contact.php last;
   rewrite /subdirectory/page$ /subdirectory/pages/page.php last;
   rewrite /subdirectory/terms$ /subdirectory/pages/tos.php last;

   rewrite ^/subdirectory/sitemap-([0-9]+).xml$ /subdirectory/parts/sitemaps/sitemap-$1.xml last;

   rewrite ^/subdirectory/(.*)/(.*)/(.*)/(.*)/?$ /subdirectory/index.php?bank=$1&state=$2&district=$3&branch=$4 last;
   rewrite ^/subdirectory/(.*)/(.*)/(.*)/?$ /subdirectory/index.php?bank=$1&state=$2&district=$3 last;
   rewrite ^/subdirectory/(.*)/(.*)/?$ /subdirectory/index.php?bank=$1&state=$2 last;
   rewrite ^/subdirectory/(.*)/?$ /subdirectory/index.php?bank=$1 last;

 }

}

如有任何疑問,請詢問。*另外,感謝您提出這樣的問題,因為我也是新手,所以我必須學習一些新東西:P;*乾杯!!

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