nginx删除.php和.html文件扩展名 [英] nginx remove .php and .html file extension

查看:99
本文介绍了nginx删除.php和.html文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让nginx在没有.php或.html扩展名的情况下工作,我需要一些帮助.预先感谢您的宝贵时间!

I've been trying to get nginx to work without .php or .html extensions, and I needed a bit of help. Thanks in advance for your time!

我尝试了在如何使用NGINX从网址中同时删除.php和.html扩展名?,但没有效果

I tried the solution found at How to remove both .php and .html extensions from url using NGINX? but it has no effect

这是我当前的nginx配置.它是几个教程的组合,但是可以用.

Here's my current nginx config. It's a combination of several tutorials, but it works.

# 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;
#   }
#}

推荐答案

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

You should add the following two lines, in the exact spots where you think it'd form a loop. :-)

由于以下事实:每个请求 $request_uri 始终不变,因此不受其他重写,实际上不会形成任何无限循环.

Due to the fact that $request_uri is always constant per request, and is not affected by other rewrites, it won't, in fact, form any infinite loops.

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

#within \.php$
if ($request_uri ~ ^/([^?]*)\.php($|\?)) {  return 302 /$1?$args;  }

这篇关于nginx删除.php和.html文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆