【问题标题】:How to send all requests through FastCGI with Nginx如何使用 Nginx 通过 FastCGI 发送所有请求
【发布时间】:2014-07-12 03:48:58
【问题描述】:

目前,我的 Nginx 服务器设置为接受 www.example.com/filewww.example.com/file.php 以加载 file.php。但是,由于 Nginx 设置如下所示:

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    // More setup stuff
}

它通过 FastCGI 将文件作为 PHP 处理的唯一方法是 URI 以 .php 结尾,否则,它只会下载文件。如何设置它以便通过 FastCGI 处理所有 PHP 文件,而不管 URI 是什么? (反正我肯定这更安全,所以人们看不到我的 PHP 代码。)

【问题讨论】:

    标签: php nginx fastcgi


    【解决方案1】:

    虽然我认为通过单个 php 文件(即 index.php)处理不以 .php 结尾的请求路由会更好,但您可以尝试这样的事情:

    location / {
        root            /path/to/htdocs;
        index           index.php;
        if (-e $request_filename.php) {
              rewrite   ^(.*)?$ /$1.php last;
              break;
        }
        # rewrite requests without matching file index.php (request in: $_GET['req'])
        if (!-e $request_filename) {
              rewrite   ^(.*)?$ /index.php?req=$1 last;
              break;
        }
    }
    
    location ~ \.php$ {
       root             /path/to/htdocs;
       fastcgi_pass     unix:/var/www/php-fpm/nginx.sock;
       fastcgi_index    index.php;
       include          fastcgi_params;
       fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
       ..
    }
    

    【讨论】:

    • 感谢您的回答,但这并不是我真正想要实现的目标。如果请求/file,我真的希望直接加载/file.php
    • 我更新了直接实现您正在寻找的东西的答案。
    猜你喜欢
    • 2015-02-23
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2017-10-09
    相关资源
    最近更新 更多