如何使用 nginx 和 php 进行没有 .php 扩展名的路由

问题描述 投票:0回答:1

在开始之前,我知道 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-opcache-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;
}
php docker nginx url-rewriting
1个回答
6
投票

是的,您不能强制 nginx 通过 PHP

location
处理程序(例如
try_files $uri $uri.php ...
)强制提供 PHP 脚本。你可以尝试这个配置(是的,我知道有些
if
块是邪恶的,但别担心,这个不是):

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

location ~ \.php$ {
    # default PHP handler here
    ...
}

location @extensionless-php {
    if ( -f $document_root$uri.php ) {
        rewrite ^ $uri.php last;
    }
    return 404;
}

如果您想将所有未找到的请求重定向到

index.php
,请改用这个:

location @extensionless-php {
    if ( -f $document_root$uri.php ) {
        rewrite ^ $uri.php last;
    }
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    include fastcgi.conf;
}
© www.soinside.com 2019 - 2024. All rights reserved.