72

私のapache構成には、次の単純な書き換えルールがあります。

  1. ファイルが存在しない限り、index.phpに書き換えられます
  2. 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サーバーブロックが今どのように見えるかですが、それは機能しません:(

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

8 に答える 8

132

私はこれを試し、インデックスページを取得することに成功しました。このコードをサイト構成ファイルに追加すると、次のようになります。

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

構成ファイル自体の内部では、これらが構成されたステップであると説明されています

最初にリクエストをファイルとして、次にディレクトリとして提供し、次にindex.htmlにフォールバックします。

私の場合はindex.php、phpコードでページを提供しているのでそうです。

于 2013-04-23T13:53:23.480 に答える
63

get変数も渡すには、次を使用します$args

location / {
    try_files $uri $uri/ /index.php?$args;
}
于 2014-02-12T15:15:16.437 に答える
40

1ファイルが存在しない限り、index.phpに書き換えられます

以下を追加してくださいlocation ~ \.php$

try_files = $uri @missing;

これは最初にファイルを提供しようとし、見つからない場合はその@missing部分に移動します。したがって、以下を構成に追加します(locationブロック外)。これにより、インデックスページにリダイレクトされます。

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

2ファイル拡張子(.php)が表示されないURL

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;
    }
}
于 2012-10-17T09:25:06.360 に答える
18

書き換えのないフラットでシンプルな設定は、場合によっては機能します。

location / {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /home/webuser/site/index.php;
}
于 2013-06-06T11:41:30.870 に答える
9

?の代わりにnginx$is_argsを使用する GETクエリ文字列の場合

location / { try_files $uri $uri/ /index.php$is_args$args; }
于 2016-11-23T18:11:01.087 に答える
3

これがあなたの2番目の質問の答えです:

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

それは私にとってはうまくいきます(私の経験に基づく)、つまり、すべてのblabla.phpがblablaに書き換えられます

http://yourwebsite.com/index.phpからhttp://yourwebsite.com/indexのように

于 2016-04-20T14:16:57.527 に答える
1

この質問のパート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;
    }

^([^。] * [^ /])$ $1/パーマネントを書き換えます。非ファイルアドレス(ファイル拡張子のないアドレス)を書き換えて、末尾に「/」を付けます。「アクセスが拒否されました」に遭遇したため、これを行いました。それなしでフォルダにアクセスしようとしたときのメッセージ。

try_files $ uri $ uri / /index.php = 404; SanjuDの回答から借用していますが、場所がまだ見つからない場合は、404のルート変更が追加されます。

fastcgi_index index.php; 私が見逃していたパズルの最後のピースでした。この行がないと、フォルダーはindex.phpに再ルーティングされませんでした。

于 2013-11-12T21:12:56.087 に答える
0

codeigniterのようなフレームワークにこのようなルートがある場合に備えて、index.php(他のphpファイルはfastcgiに渡されません)だけをfastcgiに渡したい場合

$route["/download.php"] = "controller/method";


location ~ index\.php$ {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi.conf;
}
于 2015-02-12T19:18:00.223 に答える