Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Nginx系列(七.nginx支持pathinfo)

Nginx系列(七.nginx支持pathinfo)

WBOY
WBOYasal
2016-08-08 09:24:421045semak imbas

一、通过rewrite方式代替php中的PATH_INFO (仅支持Thinkphp)

<code>location / {
    <span>if</span> (!<span>-e</span><span>$request_filename</span>){
        rewrite ^/(.*)$ /index.php?s=/<span>$1</span> last;
    }
}</code>

二、nginx配置文件中设置PATH_INFO值

<code>location ~ \.php {
    root /web/www/thinkphp;
    fastcgi_index index.php;
    fastcgi_pass <span>127.0</span>.<span>0.1</span>:<span>9000</span>;
    include      fastcgi_params;

    <span>set</span><span>$path_info</span><span>""</span>;
    <span>set</span><span>$real_script_name</span><span>$fastcgi_script_name</span>;
    <span>if</span> (<span>$fastcgi_script_name</span> ~ <span>"^(.+?\.php)(/.+)$"</span>) {
        <span>set</span><span>$real_script_name</span><span>$1</span>;
        <span>set</span><span>$path_info</span><span>$2</span>;
    }

    fastcgi_param SCRIPT_FILENAME <span>$document_root</span><span>$real_script_name</span>;
    fastcgi_param SCRIPT_NAME <span>$real_script_name</span>;
    fastcgi_param PATH_INFO <span>$path_info</span>;
}</code>

建议关闭 cgi.fix_pathinfo 选项

三、nginx自带pathinfo支持 (新版本)

<code><span>location</span><span>~ [^/]\.php(/|$)</span> {
        <span>root</span> /web/www/thinkphp;
        <span>fastcgi_index</span> index.php;
        <span>fastcgi_pass</span><span>127.0.0.1:9000</span>;
        <span>include</span>      fastcgi_params;

        <span>fastcgi_split_path_info</span><span> ^(.+?\.php)(/.*)$</span>;
        <span>fastcgi_param</span> SCRIPT_FILENAME <span>$document_root</span><span>$fastcgi_script_name</span>;
        <span>fastcgi_param</span> SCRIPT_NAME <span>$fastcgi_script_name</span>;
        <span>fastcgi_param</span> PATH_INFO <span>$fastcgi_path_info</span>;
        <span>fastcgi_param</span> PATH_TRANSLATED <span>$document_root</span><span>$fastcgi_path_info</span>;
}</code>

需要开启 cgi.fix_pathinfo 选项

四. 隐藏index.php文件

<code>location / {
    <span>if</span> (!<span>-e</span><span>$request_filename</span>){
        rewrite ^/(.*)$ /index.php/<span>$1</span> last;
    }
}</code>

五.nginx 官方指导

http://wiki.nginx.org/PHPFcgiExample

以上就介绍了Nginx系列(七.nginx支持pathinfo),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn