Home Backend Development PHP Tutorial Methods to support PATH_INFO under nginx

Methods to support PATH_INFO under nginx

Jun 08, 2018 am 09:43 AM
nginx

这篇文章主要介绍了nginx下支持PATH_INFO的方法,结合实例形式详细分析了nginx下使用PATH_INFO模式的具体方法与相关使用技巧,需要的朋友可以参考下

本文实例分析了nginx下支持PATH_INFO的方法。分享给大家供大家参考,具体如下:

要想让nginx支持PATH_INFO,首先需要知道什么是pathinfo,为什么要用pathinfo?

pathinfo不是nginx的功能,pathinfo是php的功能。

php中有两个pathinfo,一个是环境变量$_SERVER['PATH_INFO'];另一个是pathinfo函数,pathinfo() 函数以数组的形式返回文件路径的信息;。

nginx能做的只是对$_SERVER['PATH_INFO]值的设置。

下面我们举例说明比较直观。先说php中两种pathinfo的作用,再说如何让nginx支持pathinfo。

php中的两个pathinfo

php中的pathinfo()

pathinfo()函数可以对输入的路径进行判断,以数组的形式返回文件路径的信息,数组包含以下元素。

[dirname]  路径的目录
[basename] 带后缀 文件名
[extension]  文件后缀
[filename]  不带后缀文件名(需php5.2以上版本)

例如

<?php
print_r(pathinfo("/nginx/test.txt"));
?>
Copy after login

输出

Array
(
  [dirname] => /nginx
  [basename] => test.txt
  [extension] => txt
  [filename] => test
)
Copy after login

php中的$_SERVER['PATH_INFO']

PHP中的全局变量$_SERVER['PATH_INFO'],PATH_INFO是一个CGI 1.1的标准,经常用来做为传参载体。

被很多系统用来优化url路径格式,最著名的如THINKPHP框架。

对于下面这个网址:

http://www.test.cn/index.php/test/my.html?c=index&m=search

我们可以得到 $_SERVER['PATH_INFO'] = '/test/my.html',而此时 $_SERVER['QUERY_STRING'] = 'c=index&m=search';

如果不借助高级方法,php中http://www.test.com/index.php?type=search 这样的URL很常见,大多数人可能会觉得不太美观而且对于搜索引擎也是非常不友好的(实际上有没有影响未知),因为现在的搜索引擎已经很智能了,可以收入带参数的后缀网页,不过大家出于整洁的考虑还是想希望能够重写URL,

下面是一段解析利用PATH_INFO的进行重写的非常简单的代码:

<?php
if(!isset($_SERVER[&#39;PATH_INFO&#39;])) {
  $pathinfo = &#39;default&#39;;
} else {
  $pathinfo = explode(&#39;/&#39;, $_SERVER[&#39;PATH_INFO&#39;]);
}
if(is_array($pathinfo) && !empty($pathinfo)) {
  $page = $pathinfo[1];
} else {
  $page = &#39;default.php&#39;;
}
?>
Copy after login

有了以上认识我们就可以介入nginx对$_SERVER['PATH_INFO']支持的问题了。在这之前还要介绍一个php.ini中的配置参数cgi.fix_pathinfo,它是用来对设置cgi模式下为php是否提供绝对路径信息或PATH_INFO信息。没有这个参数之前PHP设置绝对路径PATH_TRANSLATED的值为SCRIPT_FILENAME,没有PATH_INFO值。设置这个参数为cgi.fix_pathinfo=1后,cgi设置完整的路径信息PATH_TRANSLATED的值为SCRIPT_FILENAME,并且设置PATH_INFO信息;如果设为cgi.fix_pathinfo=0则只设置绝对路径PATH_TRANSLATED的值为SCRIPT_FILENAME。cgi.fix_pathinfo的默认值是1。

nginx默认是不会设置PATH_INFO环境变量的的值,需要php使用cgi.fix_pathinfo=1来完成路径信息的获取,但同时会带来安全隐患,需要把cgi.fix_pathinfo=0设置为0,这样php就获取不到PATH_INFO信息,那些依赖PATH_INFO进行URL美化的程序就失效了。

1.可以通过rewrite方式代替php中的PATH_INFO

实例:thinkphp的pathinfo解决方案

设置URL_MODEL=2

location / {
  if (!-e $request_filename){
    rewrite ^/(.*)$ /index.php?s=/$1 last;
  }
}
Copy after login

2.nginx配置文件中设置PATH_INFO值

请求的网址是/abc/index.php/abc

PATH_INFO的值是/abc
SCRIPT_FILENAME的值是$doucment_root/abc/index.php
SCRIPT_NAME /abc/index.php

旧版本的nginx使用如下方式配置

location ~ .php($|/) {
  set $script $uri;
  set $path_info "";
  if ($uri ~ "^(.+.php)(/.+)") {
    set $script $1;
    set $path_info $2;
  }
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$script;
  fastcgi_param SCRIPT_NAME $script;
  fastcgi_param PATH_INFO $path_info;
}
Copy after login

新版本的nginx也可以使用fastcgi_split_path_info指令来设置PATH_INFO,旧的方式不再推荐使用,在location段添加如下配置。

location ~ ^.+\.php {
 (...)
 fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
 fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;
 fastcgi_param PATH_INFO $fastcgi_path_info;
 fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
 (...)
}
Copy after login

最后可能有人要问为什么apache不会出现这个问题?

apache一般是以模块的方式运行php,apache可以对$_SERVER['PATH_INFO']的值进行设置,不需要另外配置。

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

The above is the detailed content of Methods to support PATH_INFO under nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to solve the problem of nginx cross-domain How to solve the problem of nginx cross-domain Apr 14, 2025 am 10:15 AM

There are two ways to solve the Nginx cross-domain problem: modify the cross-domain response header: add directives to allow cross-domain requests, specify allowed methods and headers, and set cache time. Use CORS modules: Enable modules and configure CORS rules that allow cross-domain requests, methods, headers, and cache times.

How to check the running status of nginx How to check the running status of nginx Apr 14, 2025 am 11:48 AM

The methods to view the running status of Nginx are: use the ps command to view the process status; view the Nginx configuration file /etc/nginx/nginx.conf; use the Nginx status module to enable the status endpoint; use monitoring tools such as Prometheus, Zabbix, or Nagios.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to solve nginx403 error How to solve nginx403 error Apr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

See all articles