繁体   English   中英

Catch-all应该将路径传递给nginx中的index.php

[英]Catch-all should pass path to index.php in nginx

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

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相关部分

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做同样的事情?

这是PHP-FPM的典型nginx配置。

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. .php位置块中删除了try_files语句。
  3. root声明移动到服务器块。 如果您需要有不同的根,请在您的问题中指明。
  4. 唯一的try_files语句包含api/index.php的完整路径。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM