Nginx 配置虚拟主机、pathinfo和URL重写
在Nginx的虚拟主机配置文件中(vhost.conf)新增一个server节点,一个server节点代表一个虚拟主机。
配置参考如下:
server {
listen 80;
server_name domain.com;
location / {
root /www/wwwroot/public;
index index.html index.htm index.php;
#显示文件列表(索引)
autoindex on;
#默认为on,显示出文件的确切大小,单位是bytes
#改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_exact_size on;
#默认为off,显示的文件时间为GMT时间。
#改为on后,显示的文件时间为文件的服务器时间
autoindex_localtime on;
#URL重写配置
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$1 last;
}
}
#location ~ \.php$ {
location ~ \.php { #pathinfo配置,去掉后面的$
root /www/wwwroot/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#pathinfo配置,高版本nginx的配置方法
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
#pathinfo配置,低版本nginx的配置方法
#fastcgi_split_path_info ^(.+\.php)(.*)$;
#fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}