如何為 nginx 轉換以下條件,
RewriteEngine on
RewriteRule ^([^\.] )/?$ index.php?$1 [L]
RewriteRule ^([^\.] )/([^\.] )/$ index.php?$1 [L]
RewriteRule ^([^\.] )/([^\.] )$ index.php?$1 [L]
RewriteRule ^([^\.] )/([^\.] )/([^\.] )$ index.php?$1 [L]
RewriteRule ^([^\.] )/([^\.] )/([^\.] )/$ index.php?$1 [L]
RewriteRule ^([^\.] )/([^\.] )/([^\.] )/([^\.] )$ index.php?$1 [L]
RewriteRule ^([^\.] )/([^\.] )/([^\.] )/([^\.] )/$ index.php?$1 [L]
我需要將 apache .htaccess 轉換為 Nginx 配置,如果有人知道解決方案,請幫助我,謝謝。
我已經使用一些在線工具將 .htaccess 檔案轉換為 Nginx 配置,但它不起作用。
如果我把網址放在瀏覽器中,就會自動下載 index.php 檔案。我檢查了 info.php 但 fpm 也可以正常作業。我不知道如何解決這個問題。
這里我提到了我的 Nginx conf 檔案:
server {
server_name example.com;
root /home/example/public_html;
index index.php;
access_log /var/log/nginx/example/example-access.log;
error_log /var/log/nginx/example/example-error.log;
location / {
rewrite ^/([^\.] )/?$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )/$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )/([^\.] )$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/([^\.] )$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/([^\.] )/$ /index.php?$1 break;
}
location ~ ^/(?:\.htaccess) {
deny all;
}
location ~ [^/]\.php(/|$) {
try_files $uri =404;
fastcgi_split_path_info ^(. \.php)(/. )$;
fastcgi_pass unix:/run/php/php5.6-fpm-example.com.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
uj5u.com熱心網友回復:
GetPageSpeed轉換器提供正確的結果:
server {
server_name example.com;
rewrite ^/([^\.] )/?$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )/$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )/([^\.] )$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/([^\.] )$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/([^\.] )/$ /index.php?$1 last;
}
注意last指令。這是對的。它將確保 NGINX 將再次開始位置搜索,以查看哪些指令適用于重寫的 URI /index.php。然后它最終會出現在您的location ~ [^/]\.php(/|$) { ... }和使用 PHP-FPM 處理的指令中。
使用break,事情將無法正常作業,因為重寫 URL 后沒有新的位置搜索。URI 保持不變/index.php,但location ~ [^/]\.php(/|$) { ... }不會應用來自的指令,因為 NGINX 沒有“去”應用來自它的指令。
uj5u.com熱心網友回復:
更新 2022-03-18
正如@Danila Vershinin 所指出的,這些rewrite陳述句應該以last代替結尾break。
你可以試試這樣的
location / {
rewrite ^/([^\.] )/?$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )/$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )/([^\.] )$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/([^\.] )$ /index.php?$1 break;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/([^\.] )/$ /index.php?$1 break;
}
或使用轉換器或其他轉換器。
uj5u.com熱心網友回復:
server {
server_name example.com;
root /home/test/public_html;
index index.php;
rewrite ^/([^\.] )/?$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )/$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )/([^\.] )$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/([^\.] )$ /index.php?$1 last;
rewrite ^/([^\.] )/([^\.] )/([^\.] )/([^\.] )/$ /index.php?$1 last;
access_log /var/log/nginx/example/example-access.log;
error_log /var/log/nginx/example/example-error.log;
location ~ \.php$ {
fastcgi_split_path_info ^(. \.php)(/. )$;
fastcgi_pass unix:/run/php/php5.6-fpm-example.com.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
location / {
try_files $uri $uri/ /index.php?$request_uri;
}
location ~ /\.ht {
deny all;
}
}
@Danila Vershinin 我試過這個配置,但它不起作用。
如果我給出 URL:http://example.com -----> http://example.com/admin/admin/admin/admin/admin/admin/admin/test。管理員名稱不斷添加到 URL。實際上需要重定向http://example.com -----> http://example.com/admin/test
我不知道如何解決這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/445742.html
上一篇:如何在nginx中設定子路徑代理
