前言:

很多用fastcgi的企业,都喜欢把nginx 如此配置

server {
    listen 80;
    server_name test.com;
    index index.html index.php;
    root /home/www;
    include other.conf;
    location ~ .*\.(php|php5)?$
                        {
                                fastcgi_pass  127.0.0.1:9000;
                                fastcgi_index index.php;
                                include fcgi.conf;
                        }

 

但这样的配置只支持那种 所有动态文件,均在 /home/www 目录下的结构

-----------------------------------------------------------------

如果现在的域名目录

/home/www/html 静态根目录

/home/www/my   用户动态页面

/home/www/trade  交易动态页面

可以考虑如此配置

 

server{
   listen 80;
   server_name test.com;
   root /home/www/html;
   index index.php index.html;
 
   location /my {
       alias /home/www/my;
       index index.html;
   }
   location ~ /my/(?<after_ali>(.*)\.(php|php5)?$) {
           root /home/www/my;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fcgi.conf;
            fastcgi_param SCRIPT_FILENAME $document_root/${after_ali};
   } 

location /trade {
       alias /home/www/trade;
       index index.html;
   }
   location ~ /trade/(?<after_ali>(.*)\.(php|php5)?$) {
           root /home/www/trade;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fcgi.conf;
            fastcgi_param SCRIPT_FILENAME $document_root/${after_ali};
   }

  location ~ .*\.(php|php5)?$
                        {
                                fastcgi_pass  unix:/tmp/php-cgi.sock;
                                fastcgi_index index.php;
                                include fcgi.conf;
                        }
}

----------------

其中粗体字部分表示 正则匹配的内容,赋值给 after_ali 变量(仅nginx 1.1以上版本支持)

然后改写script_filename 系统变量,不然默认的话会在

/home/www/trade/trade 下面找动态文件