配置nginx下別名alias支持PHP fastcgi解析


1)參看如下連篇文章:
Nginx設置alias實現虛擬目錄 alias與root的用法區別
http://down.chinaz.com/server/201111/1382_1.htm
Nginx下alias支持PHP的問題
http://i.laoer.com/nginx-alias-php.html

2)我的環境是:web根目錄在 /var/www/html/中,但是我要加上一個類似於apache的別名目錄 /bbs ,此目錄不在 web根目錄中。
我的配置文件如下:

server {
    listen       80;
    server_name    localhost;
    default_type text/plain;

    location / {
        root    /var/www/html;
        index    index.php index.htm index.html;

    }

    location = /extension/sub {
        set $push_channel_id $arg_exten;
        push_subscriber long-poll;
        push_subscriber_concurrency broadcast;
        push_channel_group broadcast;
        default_type text/plain;
        #default_type text/html;
    }
    location = /extension/pub {
        set $push_channel_id $arg_exten;
        push_publisher;
        push_min_message_buffer_length 0;
        push_max_message_buffer_length 0;
        push_message_timeout 1h;
        push_channel_group broadcast;
        default_type text/plain;
        #default_type text/html;
    }

    location /bbs {
        alias /opt/bbs/;
        index index.html index.htm index.php;
    }
    location ~ ^/bbs/.+\.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME /opt$fastcgi_script_name;
        include        fastcgi_params;
        #include fastcgi.conf;
    }

  location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        include        fastcgi_params;
        #include fastcgi.conf;
    }

}

說明: 上面這個就是成功的例子。但是又如下幾點需要注意:
1)location ~ \.php$ {}  段,必須放在 location ~ ^/bbs/.+\.php$ {} 段后面,否則/bbs/的url打不開
2) location ~ ^/bbs/.+\.php$ {} 里面也可以寫成如下:
location ~ ^/bbs/.+\.php$ {
        root /opt;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
也就是用 變量名 $document_root 代替 /opt; 其實每個 location {}中的 $document_root 都是局部變量,都是在本段配置 root指令指定的路徑。

 

以上配置完全可以通過nginx的第3放插件 echo 模塊來得到驗證,方法如下:
location /echo {
        root /etc/asterisk;
        #echo "$document_root";
        echo "$fastcgi_script_name";
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM