技术解析

怎样配置干净的 PHP 接口链接? How to configure Nginx to have clean PHP API URL?
0
2021-05-20 19:34:25
idczone

不好意思,因为在公司写英文,所以以下问题是用英文写的。

Configuration 1:

server {
  index index.html index.php

  location / {
    try_files $uri $uri/;
  }

  location ~ [^/]\.php(/|$) {
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
}

Configuration like the above (Configuration 1) will return 500 Internal Server Error when visiting ...domain.../20170223, ...domain.../20170223/ but not ...domain.../20170223/index.php.

So I am think try_files is executed before index and then cause a internal redirect loop thus 500 Server Internal Error because if index runs first then .../20170223/ should works just fine while in fact is not.

But

Configuration 2:

server {
  index index.html index.php

  location / {
    try_files $uri $uri/ =404;
  }

  location ~ [^/]\.php(/|$) {
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
}

works for all ...domain.../20170223, ...domain.../20170223/ and ...domain.../20170223/index.php.

Which leads me to think when try_files is encountered with folder URI like .../20170223/ it makes an internal redirect. But contradiction is if try_files runs before index, it will be a redirect loop which is not true in this practice.

Then there is again not clue which one (try_files or index) runs first.

To exclude the position of index directive problem, the following configuration 3 & 4 are tested with same result as Configuration 1.

Configuration 3:

server {

  location / {
    index index.html index.php
    try_files $uri $uri/;
  }

  location ~ [^/]\.php(/|$) {
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
}

Configuration 4:

server {

  location / {
    try_files $uri $uri/;
    index index.html index.php
  }

  location ~ [^/]\.php(/|$) {
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
}

And there comes my problem.

The Configuration 2 doesn't receive POST data from a

submit when action is assigned with ...domain.../20170223 but works for ...domain.../20170223/ and ...domain.../20170223/index.php.

So why??? Does internal redirect lost 国外服务器POST data? Does try_files append index.php to folder uri according to index first and then make a redirect? In the different words for same meaning, does try_files make redirect but index never make redirect? Is index a dictionary for try_files whose default value is just try_files $uri =404; (this assumption still didn't explain for Configuration 1 practice)?

No idea according to the following official documents:

  • [Official] index, https://nginx.org/en/docs/http/ngx_http_index_module.html
  • [Official] try_files, http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

with the summary:

  • index processes requests ending with the slash character (‘/’), and note that using an index file causes an internal redirect, and the request can be processed in a different location (for instance, a “/” request may actually be processed in a second location as “/index.html ”.).
  • try_files has specified ordered file lists and uri or =code at last, if none of the files were found, then an internal redirect to the uri specified in the last parameter is made. (Notice: only the last part is internal redirect, the ones ahead are all served directly by Nginx as static files.)

And is there any solve to have a clean post action url without tailing slash /?


尝试 `try_files $uri $uri/ /index.php?$query_string;`?

这个还得看程序,例如我搞的框架 https://github.com/chopins/toknot , nginx 配置就十分简单, FastCGI 配置块就如下:
location / {
fastcgi_index index.php;
root $your_web_public/index.php;
fastcgi_pass php-fpm;
include fastcgi.conf;
}
然后在前面把静态路径排除就行了,这种配置无需在 nginx 中维护大量的重写规则

路由参数通过 url 传递吗

你这个本质上和我第二个没有什么区别,考虑 try_files 的执行顺序。另外我没有采用 /index.php 处理所有找不到对应文件夹和脚本的方式。

你这是是用 PHP 在根目录的 index.php 处理所有 URL 了,不是应用 Nginx 的好的实践方案哦。

这个方案是最灵活的处理方案,这样 nginx 不需要关联任何业务逻辑上的东西,并且能做很灵活的访问权限控制

问题是我在同一个域名之下用了多种语言的后台程序。这也是为什么用 Nginx 而不是 Apache 的一个重要原因。

对于多程序的后台程序,如果后端程序有很清晰的访问规则也很好弄,如果本省后端程序访问规则就不清晰,那也就不存在所谓的最佳方法了

同意 看程序的

结构是清晰的,你说的办法在跑题。我现在的要求挺简单的,就是用 Nginx 做一个干净的可以提交 POST 数据的 URL ,不涉及用 PHP 路由。

数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服