NGINX 将路径作为变量发送给 PHP,除非路径是文件路径 [英] NGINX send path as variable to PHP except if path is a file path

查看:53
本文介绍了NGINX 将路径作为变量发送给 PHP,除非路径是文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有路径传递给 index.php?page=path.例如:

I want to pass all my path to index.php?page=path. For example:

domain.com/a/b/c -> index.php?page=a/b/c

因此这是我的 NGINX 配置文件:

Therefore this is my NGINX conf file:

location ~ /(?P<arg1>.*)$ {
    fastcgi_pass php:9000;
    fastcgi_param  QUERY_STRING       /index.php?page=$arg1;
}

据我所知,查询字符串应该是 .com 之后的所有内容,对吗?我将 arg1 传递给它.但是,我想忽略真正的文件路径,例如 /file.jpg/images/favicon.ico.我可以简单地在正则表达式中否定它们,但是它们根本没有路径.

As far as I know, query string should be everything after .com, right? I'm passing arg1 to it. However, I wanted to ignore truly file paths like /file.jpg or /images/favicon.ico. I could simply negate them in regex but then they'd have no path at all.

那么除了文件之外,如何将/anythingindex.php?page=anything 匹配并实际交付这些文件?

So how to match /anything to index.php?page=anything except for files and actually deliver those files?

推荐答案

下面需要使用

location ~ /(.*)$ {
  try_files $uri $uri/ /index.php?page=$1;
}

这将首先检查文件或目录是否存在,如果不存在,则将其传递给index.php 文件

This will first check if file or directory exists if not then pass it to the index.php file

这篇关于NGINX 将路径作为变量发送给 PHP,除非路径是文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆