在项目发开过程中使用的服务器基本上都是提前配置好的,而且配置好的服务器环境也不能说改就改,随意的更改环境会导致服务器其他的功能失效,可能会引发很严重的错误,最近的项目开发中,有一个项目在测试服务器开发和测试完成之后,转移到正式服务器,在转移完成之后,发现网站除了首页可以正常访问,其他的所有连接均无法打开,研究了一下发现,之前的开发环境使用的是Apache服务器,而正式的服务器使用的是Nginx服务器,看似都是服务器但是其中的小差别还是很大的,所以导致了项目无法正常访问。
如何解决这个问题呢,直接修改Nginx 的配置文件vhost.conf,直接上的服务器 配置(包含 https):
server{
listen 443; server_name demo5.thinkcmf.com; root 你的ThinkCMF5目录/public; # 该项要修改为你准备存放相关网页的路径 ssl on; ssl_certificate cert/demo5.thinkcmf.com.pem; ssl_certificate_key cert/demo5.thinkcmf.com.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { index index.php index.html index.htm; #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则 if (!-e $request_filename) { #地址作为将参数rewrite到index.php上。 rewrite ^/(.*)$ /index.php?s=$1; #若是子目录则使用下面这句,将subdir改成目录名称即可。 #rewrite ^/subdir/(.*)$ /subdir/index.php?s=$1; } } location /api/ { index index.php index.html index.htm; #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则 if (!-e $request_filename) { #地址作为将参数rewrite到index.php上。 #rewrite ^/(.*)$ /index.php?s=$1; #若是子目录则使用下面这句,将subdir改成目录名称即可。 rewrite ^/api/(.*)$ /api/index.php?s=$1; } } location ~* \/upload\/.+\.(html|php)$ { return 404; } location ~* ^\/plugins\/.+\.(html|php)$ { return 404; } location ~* \/themes\/.+\.(html|php)$ { return 404; } #proxy the php scripts to php-fpm location ~ \.php { include fastcgi_params; ##pathinfo支持start #定义变量 $path_info ,用于存放pathinfo信息 set $path_info ""; #定义变量 $real_script_name,用于存放真实地址 set $real_script_name $fastcgi_script_name; #如果地址与引号内的正则表达式匹配 if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") { #将文件地址赋值给变量 $real_script_name set $real_script_name $1; #将文件地址后的参数赋值给变量 $path_info set $path_info $2; } #配置fastcgi的一些参数 fastcgi_param SCRIPT_FILENAME $document_root$real_script_name; fastcgi_param SCRIPT_NAME $real_script_name; fastcgi_param PATH_INFO $path_info; ###pathinfo支持end fastcgi_intercept_errors on; fastcgi_pass 127.0.0.1:9000; } } server { listen 80; server_name demo5.thinkcmf.com; rewrite ^/(.*) https://$server_name/$1 permanent;}
在配置好基本环境之后,还要解决在低版本的Nginx中不支持PATHINFO环境变量,通过在Nginx.conf中配置规则实现
location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; } }
如果你的应用安装在二级目录,例如使用TP框架,Nginx的伪静态方法设置如下,其中public是所在的目录名称。
location / { if (!-e $request_filename) { rewrite ^/public/(.*)$ /youdomain/index.php?s=/$1 last; } }