大网数据广告活动
服务器运维问答

Nginx PHP-FPM with single entry point and PATH_INFO

· 52CE 我爱测

I am trying to configure nginx with php-fpm so most requests is passed to a single php-script. The directory layout is as following: assets/... index.php I would like the urls to work like: / -> index.php /foo -> index.php /bar/b…

问题原文

原帖链接 · ext · CC BY-SA 3.0

I am trying to configure nginx with php-fpm so most requests is passed to a single php-script.

The directory layout is as following:

assets/...
index.php

I would like the urls to work like:

/ -> index.php
/foo -> index.php
/bar/baz?spam=ham -> index.php
/assets -> assets folder

index.php should have PATH_INFO set to '/', '/foo' and '/bar/baz' respectively.

The closest configuration I've figured out is the following:

location / {
    try_files $uri /index.php/$uri$is_args$args;
}

location /assets {
    try_files $uri =404;
}

location /index.php {
    include snippets/fastcgi-php.conf;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}

However, PATH_INFO always have an additional / (e.g. '//' in the first case). Changing to /index.php$url$is_args$args works for all cases except for / which results in 404.

I could of course modify the script to handle the extra / but it feels a bit dirty.

I've been trying to search and reading the manual but I cant figure it out, any pointers?

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

You commented that your snippets/fastcgi-php.conf contains:

fastcgi_split_path_info ^(.+\.php)(/.+)$;

Which you will notice, does not match the URI /index.php/, which explains why you get a 404 response.

You can override the value in snippets/fastcgi-php.conf by placing the corrected statement after it (assuming that you do not want to edit the system file). For example:

location / {
    try_files $uri /index.php$uri$is_args$args;
}
location /index.php {
    include snippets/fastcgi-php.conf;
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}

Simply change the + to a * so that / is matched when nothing else follows it.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。