nginx 删除 .php 和 .html 文件扩展名

2023-12-15

我一直在尝试让 nginx 在没有 .php 或 .html 扩展名的情况下工作,并且我需要一些帮助。在此先感谢您的时间!

我尝试了在以下位置找到的解决方案如何使用 NGINX 从 url 中删除 .php 和 .html 扩展名?但没有效果

这是我当前的 nginx 配置。它是几个教程的组合,但它确实有效。

# You may add here your
# server {
#   ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# ht

tp://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
    root /usr/share/nginx/www;
    index index.php index.html;

    server_name localhost;

    location / {
        try_files $uri $uri.php $uri.html $uri/ =404;
    }

    error_page 404 /404.html;

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        # fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # fastcgi_pass unix:/var/run/php5-fpm.sock;
        # fastcgi_index index.php;
        # include fastcgi_params;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

# HTTPS server
#
#server {
#   listen 443;
#   server_name localhost;
#
#   root html;
#   index index.html index.htm;
#
#   ssl on;
#   ssl_certificate cert.pem;
#   ssl_certificate_key cert.key;
#
#   ssl_session_timeout 5m;
#
#   ssl_protocols SSLv3 TLSv1;
#   ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
#   ssl_prefer_server_ciphers on;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

您应该在您认为会形成循环的确切位置添加以下两行。 :-)

由于这个事实$request_uri每个请求始终是恒定的,并且不受其他重写的影响,事实上,它不会形成任何无限循环。

#at the top of location /
if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }

#within \.php$
if ($request_uri ~ ^/([^?]*)\.php($|\?)) {  return 302 /$1?$args;  }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

nginx 删除 .php 和 .html 文件扩展名 的相关文章

随机推荐

  • 当我调整 Jframe 大小时如何停止 auto-repaint()

    我还在学习Java 如果有人能帮助我我会很高兴 抱歉英语不好 我是西班牙人 我正在制作一款瓷砖游戏 该游戏使用经典的 游戏循环 将引擎限制在 60fps 循环休眠然后调用repaint 这很好用 但 问题是 当 JFrame 调整大小或最大
  • 模拟连接在 Couchbase 中如何工作?

    我有一份相互依赖的文件 第一的 doctype closed auctions seller person person11304 buyer person person0 itemref item item1 price 50 03 da
  • 如何一次又一次地调用一个方法,直到它返回包含“None”的“Future”值

    给定一个返回 a 的方法Future像这样 def remove id String Future Option User Future removes and returns the user identified by id 我如何一次
  • 使用 ajax 和 JavaScript 发布到 GitHub v3 API 失败并出现 HTTP 404

    我正在尝试按照文档将 blob 发布到 JavaScript jQuery 中的 GitHub APIhttps developer github com v3 git blobs create a blob 但是我不断收到 404 请求
  • 如何通过 PHP 将 .xls 转换为 .pdf?

    我在谷歌上搜索了很多小时 但还没有找到解决方案 我需要将 xls 文件转换为 pdf 文件 怎么做 我找到了这个http www aspose com 但似乎它没有PHP API 只有 NET和JAVA 感谢您的每一个建议 PHPExcel
  • 上传apk到市场时,apk必须使用与之前版本相同的证书进行签名

    我已经创建了一个应用程序并发布在android市场上 现在我想发布它的第二个版本 我想澄清几件事 我保留了相同的包名称 我没有以前的密钥库 因为我更改了 Windows 因此删除了密钥库 并且我没有该密钥库的备份 现在我应该有什么方法可以从
  • 在 Xcode 6 中调整测量中心

    在新的 Xcode 6 中 缺少更改按钮测量中心的功能 在 Xcode 5 中 https i stack imgur com 3fCfY jpg Xcode 6 https i stack imgur com 46YTZ jpg 当我将一
  • 使用带有 LAME 的 NDK 的内置函数警告的不兼容隐式声明

    我正在尝试按照位于以下位置的教程进行操作 http developer samsung com android technical docs Porting and using LAME MP3 on Android with JNI 其要
  • 选择 2 下拉列表但允许用户输入新值?

    我想要一个包含一组值的下拉菜单 但也允许用户 选择 此处未列出的新值 我看到select2如果您使用它 则支持此功能tags模式 但是有没有办法在不使用标签的情况下做到这一点 The 很好的答案由 提供 fmpwizard适用于 Selec
  • 检测 div 中的滚动结束

    我有一个包含许多博客文章的动态网站 我想首先加载四个帖子 然后在滚动到末尾时加载另外四个帖子 我知道如何在后端处理它 但我在前端遇到问题 我已将 html 和 body 的高度设置为 100 因此窗口上的滚动事件不起作用 作为解决方法 我决
  • 使用 std::bind 时从 std::function 获取函数指针

    我正在尝试使用std function和这个结合std bind 但我遇到了一些问题 这有效 include
  • 如何在脚本中处理MySQL时区

    我正在开发一个移动应用程序 从应用程序调用 Web 服务 该服务根据模式 mode xx 运行不同的查询 在其中一些查询中 我使用日期函数 例如 DATE NOW MySQL数据库中存储的数据存储在GMT 7 加拿大山地时间 我尚未为此网络
  • 在python 2.6中创建字典的字典

    我在 python2 7 中有一行代码生成空字典的字典 values 0 1 2 4 5 8 value dicts x for x in values 在 python2 6 上运行时会引发语法错误 我可以使用 for 循环做同样的事情
  • Rundeck 未设置环境变量以使用不同的 ssh 端口进行远程执行

    Rundeck 将传递给作业的所有选项设置为环境变量 例如 RD OPTION 但当作业在具有不同 ssh 端口的远程节点中执行时 它不会设置这些变量 脚本成功登录远程节点 但环境变量不存在 请帮我提供解决方案 工作定义示例
  • 在 C# 中传递二维数组的一维

    我已经从 C 迁移到 C 我有一个接受数组的函数 我想将二维数组的一维传递给此函数 C 代码将是 void array processing int param void main int Client ID 3 50 Some Proce
  • 64 位 iOS 设备中的 UIViewControllerHierarchy 不一致

    我正在开发一个 iPad 应用程序 它有一个视图控制器 称为 ContentViewController 其中有 3 个不同的视图 滑块视图 从底部打开 其中包含图标列表 基于选择图标 我必须在内容视图中加载视图控制器 控制视图 屏幕左侧有
  • PHP imap_search UID SEARCH 返回 false

    我正在使用 PHP 的 imap 函数 并且想要检索 UID gt n 的电子邮件 这适用于搜索日期但不适用于 UID imap imap open host username password emails imap search ima
  • 谷歌地图 API 标记与标签

    I have var marker new MarkerWithLabel position uav Position icon mapStyles uavSymbolBlack labelContent uav Callsign div
  • 旋转整个 ggplot() 而不旋转任何文本 R

    我希望旋转整个图 轴和所有内容 但保持轴标签和标题的原样 以便可以水平阅读它们 library ggplot2 data mtcars ggplot geom point data mtcars aes x mpg y cyl labs t
  • nginx 删除 .php 和 .html 文件扩展名

    我一直在尝试让 nginx 在没有 php 或 html 扩展名的情况下工作 并且我需要一些帮助 在此先感谢您的时间 我尝试了在以下位置找到的解决方案如何使用 NGINX 从 url 中删除 php 和 html 扩展名 但没有效果 这是我