Url rewriting 使用nginx重写对index.php的所有请求

Url rewriting 使用nginx重写对index.php的所有请求,url-rewriting,nginx,Url Rewriting,Nginx,在我的apache配置中,我有以下简单的重写规则 除非文件存在,否则将重写为index.php 在URL上,您永远看不到文件扩展名(.php) 我如何在nginx中重写这个 # # Redirect all to index.php # RewriteEngine On # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILE

在我的apache配置中,我有以下简单的重写规则

  • 除非文件存在,否则将重写为index.php
  • 在URL上,您永远看不到文件扩展名(.php)
  • 我如何在nginx中重写这个

    #
    # Redirect all to index.php
    #
    RewriteEngine On
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
    RewriteRule .* index.php [L]
    
    以下是我的nginx服务器块现在的样子,但它不起作用:(


    1除非文件存在,否则将重写为index.php

    将以下内容添加到
    位置~\.php$

    try_files = $uri @missing;
    
    这将首先尝试为文件提供服务,如果找不到它,它将移动到
    @缺少的部分。因此,还要将以下内容添加到您的配置中(在
    位置
    块之外),这将重定向到您的索引页

    location @missing {
        rewrite ^ $scheme://$host/index.php permanent;
    }
    

    2在URL上,您永远看不到文件扩展名(.php)

    要删除php扩展,请阅读以下内容:

    以及链接中的示例配置:

    location / {
        set $page_to_view "/index.php";
        try_files $uri $uri/ @rewrites;
        root   /var/www/site;
        index  index.php index.html index.htm;
    }
    
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/site$page_to_view;
    }
    
    # rewrites
    location @rewrites {
        if ($uri ~* ^/([a-z]+)$) {
            set $page_to_view "/$1.php";
            rewrite ^/([a-z]+)$ /$1.php last;
        }
    }
    

    完美的解决方案我已经尝试过了,当我在我的站点配置文件中添加此代码时,我成功地获得了我的索引页

    location / {
                try_files $uri $uri/ /index.php;
    }
    

    在配置文件中,文件本身解释了“首先尝试将请求作为文件,然后作为目录,然后返回到index.html,在我的例子中,它是index.php,因为我通过php代码提供页面。

    无需重写的简单配置在某些情况下可以工作:

    location / {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /home/webuser/site/index.php;
    }
    

    以下是我解决问题第一部分的方法:

        location / {
                rewrite ^([^.]*[^/])$ $1/ permanent;
                try_files $uri $uri/ /index.php =404;
                include fastcgi_params;
                fastcgi_pass php5-fpm-sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
        }
    
    重写^([^.]*[^/])$$1/permanent;重写非文件地址(没有文件扩展名的地址)以在结尾处有“/”。我这样做是因为我在尝试访问文件夹时遇到了“拒绝访问”消息

    try_files$uri$uri//index.php=404;借用了SanjuD的答案,但如果仍然找不到位置,则需要额外的404重路由


    fastcgi_index index index.php;是我丢失的最后一块拼图。没有这一行,文件夹没有重新路由到index.php。

    要传递get变量,请使用
    $args

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    

    如果您只想将index.php(没有其他php文件会传递给fastcgi)传递给fastcgi,以防在codeigniter这样的框架中有这样的路由

    $route["/download.php"] = "controller/method";
    
    
    location ~ index\.php$ {
            fastcgi_pass 127.0.0.1:9000;
            include fastcgi.conf;
    }
    

    以下是您第二个问题的答案:

       location / {
            rewrite ^/(.*)$ /$1.php last;
    }
    
    这是我的工作(基于我的经验),意味着所有blabla.php都将重写为blabla


    喜欢使用nginx$is_args而不是?作为GET查询字符串

    location / { try_files $uri $uri/ /index.php$is_args$args; }
    

    不幸的是,它没有真正起作用。我从nginx得到一个空响应。在错误日志中,它报告了以下2012/10/17 15:23:14[警报]17437#0:工作进程17440在信号11(堆芯转储)时退出这两个配置的组合可能会把事情搞砸,当你这样组合它们时,它们在你一次只尝试一个的情况下有效吗?你能告诉我嵌套级别的配置吗?这种配置在一级路由上也很有效。这种方法不如这里的高级别答案通用。特别是你会遇到问题在反向代理后面使用此方法,url将被重写为本地主机名,而不是公共主机名。在
    try\u files=
    行中
    =
    做什么?在
    rewrite^
    行中
    ^
    做什么?哇,这太容易了。这可以工作,但获取变量的方式不再正确传递。有没有关于如何使用的想法要做到这一点?@Ben我相信这会奏效:try_files$uri$uri//index.php?$args;你是我的英雄!@Ben try:
    try_files$uri$uri//index.php$is_args$args;
    很好!正是我所寻找的,对我来说很有效,我认为这是一个最佳的解决方案,比重写更快。@Lix我同意,这是一个更好的解决方案。如果它需要任何东西的话至少,这是值得在开销上付出努力的。iutinvg,+1表示快速静态路由。Nginx文档建议不要代理所有对PHP的调用:Nginx建议只在您也有静态内容的情况下才反对。如果不是这样,我相信这是最好的解决方案。警告:这只适用于没有t以.php结尾,例如,它不适用于
    /example.php
    ,example.php将不会重定向到index.php**警告:这仅适用于不以.php结尾的URL,例如,它不适用于
    /example.php
    ,example.php此处将不会重定向到index.php**
    location / { try_files $uri $uri/ /index.php$is_args$args; }