当前位置: 首页 > 后端技术 > PHP

laravel学习的nginx配置网站

时间:2023-03-29 13:58:09 PHP

前言在设置laravel项目的域名站点时,需要对nginx做一些相应的rewrite重写配置,才能做相关的路由,否则会报404。nginx.conf配置服务器{listen80;服务器名xxx.com;#域名root/data/www/myProject/blog/public;#站点目录,请求到laravel项目公共目录indexindexindex.htmlindex.htmindex.php;#默认请求文件位置~\.php${fastcgi_pass127.0.0.1:9000;fastcgi_indexindex.php;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;包括fastcgi_params;}location/{try_files$uri$uri//index.php?$query_string;#这句话对于laravel部署是必须的,隐藏index.php}if(!-d$request_filename){rewrite^/(.+)/$/$1permanent;}#删除索引操作if($request_uri~*index/?$){rewrite^/(.*)/index/?$/$1permanent;}#根据laravel规则重写URLif(!-e$request_filename){rewrite^/(.*)$/index.php?/$1last;休息;}位置=/50x.html{根HTML;}}操作及示例将站点重写为nginx.conf后,重启nginx:sudonginx-sreload以laravel5.2版本为例,模拟输出helloworld,可以在laravel项目中使用app/Http/routes。在php中定义一条hello路由:Route::get('/hello',function(){return'helloworld';});在浏览器中输入xxx.com/hello,在浏览器中打印helloworld