如何使用nginx和php在没有.php扩展名的情况下进行路由 [英] How to do routing without .php extension with nginx and php
问题描述
在我开始之前,我知道StackOverflow中有很多关于此问题的解决方案,但是它们不适用于我的情况.
Before I start, I know there are many solutions in StackOverflow regarding this issue but they are not working for my case.
我用PHP设置了一个运行Nginx实例的docker容器.当我输入这样的URL,即结尾没有.php的myapp.com/somefile时,它找不到该文件.我需要浏览该URL才能使其工作myapp.com/somefile.php.
I set up a docker container running Nginx instance with PHP. When I enter such URL, myapp.com/somefile without .php at the end, it cannot find the file. I need to browse this URL to make it work myapp.com/somefile.php.
这是我使用Dockerfile和Nginx配置的配置.
Here is my configuration with Dockerfile and Nginx config.
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;
}
}
如何解决此问题?任何帮助将不胜感激!
How can I solve this issue? Any help would be appreciated!
注意:我试图将这行代码添加到nginx配置中,但最终下载文件而不是执行它们:
Note: I tried to add this lines of code to the nginx config but i ended up downloading files instead of executing them :
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
处理程序强制使用类似 try_files $ uri $ uri的东西来强制提供PHP脚本..php ...
.您可以改用此配置(是的,我知道某些 if
块是邪恶的,但是不用担心,这不是):
Yes, you cannot force nginx to force serving PHP script via PHP location
handler with something like try_files $uri $uri.php ...
. You can try this config instead (yes, I know that some if
blocks are evil, but don't worry, this one isn't):
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
,请改用此请求:
If you want to redirect all the not found requests to index.php
, use this one instead:
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;
}
这篇关于如何使用nginx和php在没有.php扩展名的情况下进行路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!