首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Catch-all应该在nginx中传递到index.php的路径。

Catch-all应该在nginx中传递到index.php的路径。
EN

Stack Overflow用户
提问于 2019-04-25 12:28:09
回答 1查看 1.1K关注 0票数 1

apache中,我有一个.htaccess,如果找不到文件或文件夹,它将从http://server/api/any/path/i/want重写到http://server/api/index.php

代码语言:javascript
复制
Options -MultiViews

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
    RewriteRule ^.*$ %2index.php [L,NC,QSA]
</IfModule>

我将转到docker,并将使用nginx代替,我想重写rewrite

需要注意的是,使用apache.htaccess $_SERVER['REQUEST_URI']的是/api/any/path/i/want,而不是重写的url (index.php....)。

我对nginx不是很熟悉,但从帖子上看,我发现了一些事情。

site.conf相关部分

代码语言:javascript
复制
location / {
    root /app/html;
    try_files $uri $uri/ index.html /index.php?$args;
}

location ~ \.php$ {
    try_files $uri @missing;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}

location @missing {
    rewrite ^ $scheme://$host/api/index.php permanent;
}

不幸的是,上面的配置只会重定向到index.php,这是我所能得到的。

我如何在nginx中做同样的事情?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-25 13:48:19

这是PHP的典型nginx配置。

代码语言:javascript
复制
server {

 root /app/html;

 location / {
        try_files $uri $uri/ /api/index.php$is_args$args;
  }

  location ~ \.php$ {

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
   }

} 

请注意示例中的不同之处:

  1. 删除不必要的@missing位置块。
  2. try_files位置块中删除.php语句。
  3. root声明移到服务器块。如果你需要有不同的根源,请在你的问题中说明这一点。
  4. 唯一的try_files语句包括api/index.php的完整路径。

如果请求不存在的路径,它将由您的/app/html/api/index.php脚本处理,充当全局入口点。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55849313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档