在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中做同样的事情?
发布于 2019-04-25 13:48:19
这是PHP的典型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;
}
} 请注意示例中的不同之处:
@missing位置块。try_files位置块中删除.php语句。root声明移到服务器块。如果你需要有不同的根源,请在你的问题中说明这一点。try_files语句包括api/index.php的完整路径。如果请求不存在的路径,它将由您的/app/html/api/index.php脚本处理,充当全局入口点。
https://stackoverflow.com/questions/55849313
复制相似问题