Nginx 配置虚拟主机、pathinfo和URL重写

Nginx 配置虚拟主机、pathinfo和URL重写

经验文章nimo972025-06-23 20:29:275A+A-

在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;
     }
 }
点击这里复制本文地址 以上内容由nimo97整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

尼墨宝库 © All Rights Reserved.  蜀ICP备2024111239号-7