如何使用nginx和php在没有.php扩展的情况下进行路由

如何使用nginx和php在没有.php扩展的情况下进行路由,php,docker,nginx,url-rewriting,Php,Docker,Nginx,Url Rewriting,在我开始之前,我知道StackOverflow中有很多关于这个问题的解决方案,但它们不适用于我的案例 我用PHP设置了一个运行Nginx实例的docker容器。当我输入这样的URL,myapp.com/somefile,但结尾没有.php时,它找不到该文件。我需要浏览此URL以使其工作于myapp.com/somefile.php 这是我对Dockerfile和Nginx配置的配置 Dockerfile FROM php:7.4.8-fpm RUN apt-get update -y \

在我开始之前,我知道StackOverflow中有很多关于这个问题的解决方案,但它们不适用于我的案例

我用PHP设置了一个运行Nginx实例的docker容器。当我输入这样的URL,myapp.com/somefile,但结尾没有.php时,它找不到该文件。我需要浏览此URL以使其工作于myapp.com/somefile.php

这是我对Dockerfile和Nginx配置的配置

Dockerfile

FROM php:7.4.8-fpm

RUN apt-get update -y \
    && apt-get install -y nginx

# PHP_CPPFLAGS are used by the docker-php-ext-* scripts
ENV PHP_CPPFLAGS="$PHP_CPPFLAGS -std=c++11"

RUN docker-php-ext-install pdo_mysql \
    && docker-php-ext-install opcache \
    && apt-get install libicu-dev -y \
    && docker-php-ext-configure intl \
    && docker-php-ext-install intl \
    && apt-get remove libicu-dev icu-devtools -y
RUN { \
        echo 'opcache.memory_consumption=128'; \
        echo 'opcache.interned_strings_buffer=8'; \
        echo 'opcache.max_accelerated_files=4000'; \
        echo 'opcache.revalidate_freq=2'; \
        echo 'opcache.fast_shutdown=1'; \
        echo 'opcache.enable_cli=1'; \
    } > /usr/local/etc/php/conf.d/php-opocache-cfg.ini

COPY nginx-site.conf /etc/nginx/sites-enabled/default
COPY entrypoint.sh /etc/entrypoint.sh

COPY --chown=www-data:www-data . /var/www/html

WORKDIR /var/www/html

EXPOSE 80 443

ENTRYPOINT ["sh", "/etc/entrypoint.sh"]
nginx site.conf

server {
    root    /var/www/html;

    include /etc/nginx/default.d/*.conf;

    index index.php index.html index.htm;

    client_max_body_size 30m;

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

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        # Mitigate https://httpoxy.org/ vulnerabilities
        fastcgi_param HTTP_PROXY "";
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}
我如何解决这个问题?任何帮助都将不胜感激

注意:我试图将这行代码添加到nginx配置中,但最终下载了文件,而不是执行它们:

location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php;
}

location ~ \.php$ {
    try_files $uri =404;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}

是的,您不能强制nginx通过PHP
location
处理程序提供PHP脚本,比如
try\u files$uri$uri.PHP…
。您可以试试这个配置(是的,我知道一些
if
块是邪恶的,但别担心,这个不是):

位置/{
index.html index.htm index.php;
尝试_文件$uri$uri.html$uri/@extensionless-php;
}
位置~\.php${
#这里的默认PHP处理程序
...
}
location@extensionless php{
if(-f$document\u root$uri.php){
最后重写^$uri.php;
}
返回404;
}
如果要将所有未找到的请求重定向到
index.php
,请改用此请求:

location@extensionless-php{
if(-f$document\u root$uri.php){
最后重写^$uri.php;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME$document_root/index.php;
包括fastcgi.conf;
}

是的,您不能强制nginx通过PHP
位置
处理程序,使用类似于
try\u files$uri$uri.PHP…
的内容来强制提供PHP脚本。您可以试试这个配置(是的,我知道一些
if
块是邪恶的,但别担心,这个不是):

位置/{
index.html index.htm index.php;
尝试_文件$uri$uri.html$uri/@extensionless-php;
}
位置~\.php${
#这里的默认PHP处理程序
...
}
location@extensionless php{
if(-f$document\u root$uri.php){
最后重写^$uri.php;
}
返回404;
}
如果要将所有未找到的请求重定向到
index.php
,请改用此请求:

location@extensionless-php{
if(-f$document\u root$uri.php){
最后重写^$uri.php;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME$document_root/index.php;
包括fastcgi.conf;
}

谢谢您的回答!这是我第一次接触PHP。所以我对此一无所知。我应该在#default PHP handler here区域输入什么?@GürkanGüran您是否将所有fastcgi人员(
fastcgi_pass
和其他)放入
location~\.PHP${…}
块?根据您建议的配置,我在查询字符串方面遇到了问题。它们不会传递到PHP文件。你能帮个忙吗?@GürkanGüran我很抱歉回答得太晚,终于设法检查了这个关于查询字符串的解决方案。我的环境与您的不同,我使用默认的CentOS nginx发行版,但对我来说一切正常,查询字符串按预期传递。我的发行版使用
fastcgi\u参数
文件初始化默认的fastcgi参数,负责查询字符串的行是
fastcgi\u参数查询字符串$query\u字符串
。您的
fastcgi.conf
应该有类似的行。你能检查一下它的内容吗?谢谢你的回答!这是我第一次接触PHP。所以我对此一无所知。我应该在#default PHP handler here区域输入什么?@GürkanGüran您是否将所有fastcgi人员(
fastcgi_pass
和其他)放入
location~\.PHP${…}
块?根据您建议的配置,我在查询字符串方面遇到了问题。它们不会传递到PHP文件。你能帮个忙吗?@GürkanGüran我很抱歉回答得太晚,终于设法检查了这个关于查询字符串的解决方案。我的环境与您的不同,我使用默认的CentOS nginx发行版,但对我来说一切正常,查询字符串按预期传递。我的发行版使用
fastcgi\u参数
文件初始化默认的fastcgi参数,负责查询字符串的行是
fastcgi\u参数查询字符串$query\u字符串
。您的
fastcgi.conf
应该有类似的行。你能检查一下里面的东西吗?