清理 nginx 上的 URL 和 php 扩展

Ale*_*s G 3 php rewrite nginx

我在网上查看了许多其他问题和参考资料 - 根据我的所有计算,我的设置应该有效,但事实并非如此。

我使用 php-fpm 安装了 nginx。如果我尝试访问 .php 文件,它会正确运行并得到正确的结果。我在我的配置文件中得到了这个:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}
Run Code Online (Sandbox Code Playgroud)

现在,我想设置我的网络应用程序,以便在仍显示在浏览器地址栏中的同时/somedir/file自动执行。所以我修改了我的配置以包含以下内容:/somdir/file.php/somdir/file

location / {
    try_files $uri $uri/ $uri.php?query_string
}
Run Code Online (Sandbox Code Playgroud)

类型的作品,那就是服务器做访问的PHP文件。然而,它没有使用location ~ \.php$上面现有的块来执行它,而是简单地将 php 源代码作为下载到浏览器中。如果我将 .php 手动附加到请求的 URL,则执行 php。

感觉好像一旦服务器匹配try_files$uri.php,它就不会在位置上再执行一次以查看它需要对 php 文件做什么。我尝试将 php 块放在 的上方和下方location /,但没有任何区别。

我怎样才能让php被执行?

mar*_*ama 5

您是否尝试使用该rewrite指令?例如:

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

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}
Run Code Online (Sandbox Code Playgroud)

来源:http : //www.tweaktalk.net/60/nginx-remove-php-file-extension-from-url