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

pth*_*fan 53 nginx url-rewriting

在我的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]
Run Code Online (Sandbox Code Playgroud)

这是我的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;
}
Run Code Online (Sandbox Code Playgroud)

小智 111

完美的解决方案我已经尝试过并在我的网站配置文件中附加此代码后成功获取索引页面.

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

在配置文件本身解释说,"首先尝试将请求作为文件,然后作为目录,然后回到index.html,在我的情况下,它是index.php,因为我通过PHP代码提供页面.

  • @Ben我相信这会解决问题:try_files $ uri $ uri//index.php?$args; (11认同)
  • 哇,这太简单了. (2认同)
  • 这有效,但获取变量不再正确传递.有关如何做到这一点的任何想法? (2认同)
  • @Ben 试试:`try_files $uri $uri//index.php$is_args$args;` (2认同)

Jan*_* Wy 58

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

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

  • **警告:这只适用于不以 .php 结尾的网址,例如它不适用于 `/example.php` , example.php 不会被重定向到 index.php** (2认同)

jag*_*ler 32

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

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

try_files = $uri @missing;
Run Code Online (Sandbox Code Playgroud)

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

location @missing {
    rewrite ^ $scheme://$host/index.php permanent;
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


iut*_*nvg 15

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

location / {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /home/webuser/site/index.php;
}
Run Code Online (Sandbox Code Playgroud)

  • Nginx文档建议不要代理所有对PHP的调用:http://wiki.nginx.org/Pitfalls#Proxy_Everything (5认同)
  • NGINX建议仅在您拥有静态内容的情况下提供服务.如果不是这种情况,我相信这是最好的解决方案. (2认同)

raz*_*bee 7

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

location / { try_files $uri $uri/ /index.php$is_args$args; }
Run Code Online (Sandbox Code Playgroud)


Ari*_*fal 5

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

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

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

就像http://yourwebsite.com/index.phphttp://yourwebsite.com/index