【问题标题】:Rewrite all requests to index.php with nginx用 nginx 重写所有对 index.php 的请求
【发布时间】:2019-04-27 11:53:33
【问题描述】:

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

  1. 除非文件存在,否则将重写为 index.php
  2. 在您从未看到文件扩展名 (.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 服务器块现在的样子,但它不起作用:(

root /home/user/www;
index index.php;

# Make site accessible from http://localhost/
server_name some-domain.dev;


###############################################################
# exclude /favicon.ico from logs
location = /favicon.ico {
    log_not_found off;
    access_log off;
}   

##############################################################
# Disable logging for robots.txt
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}   

##############################################################
# Deny all attempts to access hidden files such as 
# .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
    deny all;
    access_log off;
    log_not_found off;
}   

##############################################################
#   
location / { 
    include /etc/nginx/fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME  $document_root/index.php$args;
    fastcgi_pass    127.0.0.1:9000;
}   

###############################################################
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
    access_log off;
    expires    30d;
}   

###############################################################
# redirect server error pages to the static page /50x.html
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root html;
}   

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#   
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    # With php5-cgi alone:
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}

【问题讨论】:

    标签: url-rewriting nginx


    【解决方案1】:

    我已经尝试过了,并成功获取了我的索引页面。 当我在我的站点配置文件中添加此代码时:

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

    在配置文件本身里面解释了这些是配置的步骤

    第一次尝试将请求作为文件提供, 然后作为目录, 然后回退到 index.html

    在我的例子中是index.php,因为我通过 php 代码提供页面。

    【讨论】:

    • 哇,这太容易了。
    • 这可行,但获取变量不再正确传递。关于如何做到这一点的任何想法?
    • @Ben 我相信这会成功:try_files $uri $uri/ /index.php?$args;
    • 你是我的英雄!
    • @Ben try: try_files $uri $uri/ /index.php$is_args$args;
    【解决方案2】:

    要传递 get 变量也可以使用 $args:

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

    【讨论】:

    • 警告:这仅适用于不以 .php 结尾的 url,例如它不适用于 /example.php ,example.php 不会被重定向到 index.php
    【解决方案3】:

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

    将以下内容添加到您的location ~ \.php$

    try_files = $uri @missing;
    

    这将首先尝试提供文件,如果找不到,它将移至@missing 部分。所以还要将以下内容添加到您的配置中(location 块之外),这将重定向到您的索引页面

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

    2 在您从未看到文件扩展名 (.php) 的网址上

    要删除 php 扩展,请阅读以下内容: http://www.nullis.net/weblog/2011/05/nginx-rewrite-remove-file-extension/

    以及链接中的示例配置:

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

    【讨论】:

    • 不幸的是,它并没有真正起作用。我从 nginx 得到一个空的响应。在错误日志中,它报告以下 2012/10/17 15:23:14 [alert] 17437#0: worker process 17440 exited on signal 11 (core dumped)
    • 这两个配置的组合可能会在你像这样组合它们时搞砸,当你一次只尝试一个时它们是否有效?
    • 你能告诉我嵌套级别的配置吗?此配置适用于一级路由
    • 此方法的通用性不如此处评分较高的答案。特别是在反向代理后面使用这种方法会遇到问题,其中 url 将被重写为本地主机名而不是公共主机名。
    • =try_files = 行中做了什么? ^rewrite ^ 行中做了什么?
    【解决方案4】:

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

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

    【讨论】:

    • 好一个!正是我所寻找的,对我有用,我认为这是一个最佳解决方案,比重写更快。
    • @Lix 我同意,这是一个更好的解决方案。如果它需要任何其他东西,那么在开销上付出努力是值得的。 iutinvg,+1 表示快速静态路由。
    • Nginx 文档建议不要代理所有对 PHP 的调用:wiki.nginx.org/Pitfalls#Proxy_Everything
    • NGINX 仅在您同时提供静态内容的情况下才建议不要使用。如果这不是你的情况,我相信这是最好的解决方案。
    【解决方案5】:

    使用 nginx $is_args 代替 ?对于 GET 查询字符串

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

    【讨论】:

    • **警告:这仅适用于不以 .php 结尾的 url,例如它不适用于 /example.php ,这里的 example.php 不会被重定向到 index.php * *
    【解决方案6】:

    这是您第二个问题的答案:

       location / {
            rewrite ^/(.*)$ /$1.php last;
    }
    

    这对我有用(根据我的经验),意味着你所有的 blabla.php 都将重写为 blabla

    喜欢http://yourwebsite.com/index.phphttp://yourwebsite.com/index

    【讨论】:

      【解决方案7】:

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

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

      rewrite ^([^.]*[^/])$ $1/ Permanent; 将非文件地址(没有文件扩展名的地址)改写为末尾有一个“/”。我这样做是因为我遇到了“拒绝访问”。当我尝试在没有它的情况下访问文件夹时的消息。

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

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

      【讨论】:

        【解决方案8】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-01-03
          • 2016-07-13
          • 1970-01-01
          • 2012-12-09
          • 2016-12-18
          • 1970-01-01
          相关资源
          最近更新 更多