在我的 apache 配置中,我有以下简单的重写规则
如何在 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;
}
我已经尝试过并成功获取我的索引页。 当我在站点配置文件中添加此代码时:
location / {
try_files $uri $uri/ /index.php;
}
配置文件本身内部解释了这些是配置步骤
第一次尝试将请求作为文件提供服务, 然后作为目录, 然后回到index.html
就我而言,它是
index.php
,因为我通过php代码提供页面。
要传递 get 变量,请使用
$args
:
location / {
try_files $uri $uri/ /index.php?$args;
}
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;
}
}
扁平且简单的配置,无需重写,在某些情况下可以工作:
location / {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/webuser/site/index.php;
}
使用 nginx $is_args 代替 ?对于 GET 查询字符串
location / { try_files $uri $uri/ /index.php$is_args$args; }
这是你的第二个问题的答案:
location / {
rewrite ^/(.*)$ /$1.php last;
}
这对我有用(根据我的经验),意味着你所有的 blabla.php 将重写为 blabla
就像 http://yourwebsite.com/index.php 到 http://yourwebsite.com/index
以下是我解决此问题第 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。
如果您只想将index.php(不会将其他php文件传递给fastcgi)传递给fastcgi,以防您在像codeigniter这样的框架中有这样的路由
$route["/download.php"] = "controller/method";
location ~ index\.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
index index.php;
location / { try_files $uri $uri/ =404; }
error_page 404 /?URL=$uri;