【问题标题】:Convert mod_rewrite Apache to Nginx?将 mod_rewrite Apache 转换为 Nginx?
【发布时间】:2012-07-19 07:58:18
【问题描述】:

我是互联网编程的新手。我想将 Apache 切换到 Nginx Web 服务器,但 Nginx 模式重写仍然存在问题。

我在/home/user/public_html/read/ 上的网站位置和我以前在/home/user/public_html/read/.htaccess 上的.htaccess 文件如下所示:

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^mangas/([^/]+)/([^/]+)/$ - [F,L] 
RewriteRule ^mangas/([^/]+)/$ - [F,L] 
RewriteRule ^mangas(/?)$ - [F,L]

RewriteRule ^([^/.]+)/([^/.]+)/([0-9]+)(/?)$ index.php?manga=$1&chapter=$2&page=$3 [L] 
RewriteRule ^([^/.]+)/([^/.]+)(/?)$ index.php?manga=$1&chapter=$2 [L] 
RewriteRule ^([^/.]+)(/?)$ index.php?manga=$1 [L]

如何将此 mod_rewrite 转换为 nginx? (我很抱歉,因为我的英语拼写不完美)

【问题讨论】:

    标签: apache mod-rewrite nginx


    【解决方案1】:

    在您的server{} 中尝试这些:

    location ~ ^/mangas/([^/]+)/([^/]+)/$ {
       return 403; 
    } 
    location ~ ^/mangas/([^/]+)/$ { 
       return 403; 
    } 
    location ~ ^/mangas(/?)$ { 
       return 403; 
    }
    
    location / { 
       rewrite ^/([^/.]+)/([^/.]+)/([0-9]+)(/?)$ /index.php?manga=$1&chapter=$2&page=$3 break; 
       rewrite ^/([^/.]+)/([^/.]+)(/?)$ /index.php?manga=$1&chapter=$2 break; 
       rewrite ^/([^/.]+)(/?)$ /index.php?manga=$1 break; 
    }
    

    【讨论】:

    • 仍然不行 :( 当我尝试访问我的网站时,看起来像是下载了一些“应用程序流”。有人帮我提供其他参考资料吗?
    • @FikriOnline 你为 PHP 使用什么应用服务器? php-fpm?你是如何配置 nginx 与之交互的?
    【解决方案2】:

    O certo seria:

    location ~ ^/mangas/([^/]+)/([^/]+)/$ {
       return 403;
    }
    
    location ~ ^/mangas/([^/]+)/$ {
       return 403;
    }
    location ~ ^/mangas(/?)$ {
       return 403;
    }
    location / {
       rewrite ^/([^/.]+)/([^/.]+)/([0-9]+)(/?)$ /index.php?manga=$1&chapter=$2&page=$3 last;
       rewrite ^/([^/.]+)/([^/.]+)(/?)$ /index.php?manga=$1&chapter=$2 last;
       rewrite ^/([^/.]+)(/?)$ /index.php?manga=$1 last;
    }
    

    Não sei porquê, mas, alterando de break para last funciona.

    【讨论】:

    • 您介意翻译成英文吗(如果可能)。不是讨厌或任何东西 - 但这个网站是一个英语网站,可以获得最广泛的覆盖面。通过谷歌翻译粘贴它可以作为一个初学者。
    猜你喜欢
    • 2021-11-26
    • 2013-02-02
    • 2015-05-08
    • 2014-09-24
    • 2015-05-21
    • 2011-11-25
    • 2016-12-13
    • 2011-01-27
    • 2021-12-28
    相关资源
    最近更新 更多