Nginx虚拟目录支持PHP也是花了不少时间查找资料研究摸索,最终用下面两段解决。下面不是一段完整的Nginx配置文件,对于该配置片段简单解释如下:
1. 该文件配置一个主机www.mydomain.com在/data/Service下
2. 将一个在/data/Forum下的论坛程序挂在www.mydomain.com/Forum下,这里我用的是phpBB3
3. 将一个在/data/Mantis下的Bug跟踪管理程序MaintisBT挂在www.mydomain.com/Mantis下
phpBB3和MantisBT分别是两个独立的PHP程序。
对于Nginx虚拟目录支持分两段:
1. 第一段用alias解决虚拟目录问题,使用rewrite处理访问重定向,并传递用于fastcgi的正确的脚本位置
2. 第二段用于处理所有的非PHP文件在虚拟目录中的访问,没有第二段,访问非PHP文件就是出现404
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
server { listen 80; server_name www.mydomain.com; #root /data/Service; charset utf-8; access_log /usr/local/nginx/logs/www.mydomain.com.access.log main; <span style="color: #ff0000;"># Virtual directory with PHPsupport onnginx location ~ ^/Forum/.+\.php$ { alias /data/Forum/; rewrite /Forum/(.*\.php?) /$1 break; fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME /data/Forum$fastcgi_script_name; include fastcgi_params; } location ~^/Forum($|/.*) { alias /data/Forum/$1; index index.php index.html index.htm; } # Virtual directory with PHP support on nginx – end</span> # Virtual directory with PHP support onnginx location ~ ^/Mantis/.+\.php$ { alias /data/Mantis/; rewrite /Mantis/(.*\.php?) /$1 break; fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME /data/Mantis$fastcgi_script_name; include fastcgi_params; } location ~^/Mantis($|/.*) { alias /data/Mantis/$1; index index.php index.html index.htm; } # Virtual directory with PHP support on nginx – end location /{ root /data/Service; index index.php index.html index.htm; } #fast-cgi for php-cgi location ~\.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/Service$fastcgi_script_name; include fastcgi_params; } location ~\.(gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)${ access_log off; expires max; } error_page 404 /404.html; # redirectserver error pages to the static page /50x.html # error_page 500 502 503504 /50x.html; } |