在/etc/nginx/sites-enabled中编辑我的站点配置文件

从default文件复制一份重命名为www.example.com内容如下:

###YoushouldlookatthefollowingURL'sinordertograspasolidunderstanding#ofNginxconfigurationfilesinordertofullyunleashthepowerofNginx.#https://www.nginx.com/resources/wiki/start/#https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#https://wiki.debian.org/Nginx/DirectoryStructure##Inmostcases,administratorswillremovethisfilefromsites-enabled/and#leaveitasreferenceinsideofsites-availablewhereitwillcontinuetobe#updatedbythenginxpackagingteam.##Thisfilewillautomaticallyloadconfigurationfilesprovidedbyother#applications,suchasDrupalorWordpress.Theseapplicationswillbemade#availableunderneathapathwiththatpackagename,suchas/drupal8.##Pleasesee/usr/share/doc/nginx-doc/examples/formoredetailedexamples.###Defaultserverconfiguration#server{listen80;listen[::]:80;#SSLconfiguration##listen443ssldefault_server;#listen[::]:443ssldefault_server;##Note:YoushoulddisablegzipforSSLtraffic.#See:https://bugs.debian.org/773332##Readuponssl_cipherstoensureasecureconfiguration.#See:https://bugs.debian.org/765782##Selfsignedcertsgeneratedbythessl-certpackage#Don'tusetheminaproductionserver!##includesnippets/snakeoil.conf;root/var/www/html/hdgf/public;#hdgf是我的项目文件夹名称#Addindex.phptothelistifyouareusingPHPindexindex.phpindex.htmlindex.htmindex.nginx-debian.html;server_namewww.example.comexample.com;location/{#Firstattempttoserverequestasfile,then#asdirectory,thenfallbacktodisplayinga404.#try_files$uri$uri/=404;if(!-e$request_filename){rewrite^/index.php(.*)$/index.php?s=$1last;rewrite^(.*)$/index.php?s=$1last;break;}}#passPHPscriptstoFastCGIserver##location~\.php${#includesnippets/fastcgi-php.conf;###Withphp-fpm(orotherunixsockets):#fastcgi_passunix:/var/run/php/php7.0-fpm.sock;##Withphp-cgi(orothertcpsockets):#fastcgi_pass127.0.0.1:9000;#}#denyaccessto.htaccessfiles,ifApache'sdocumentroot#concurswithnginx'sone##location~/\.ht{#denyall;#}location~.*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)${expires30d;access_logoff;}location~.*\.(js|css)?${expires7d;access_logoff;}location~.php${try_files$uri=404;#增加fastcgi_split_path_info^(.+.php)(/.+)$;#反注释##NOTE:Youshouldhave"cgi.fix_pathinfo=0;"inphp.ini###Withphp5-cgialone:#fastcgi_pass127.0.0.1:9000;##Withphp5-fpm:fastcgi_passunix:/var/run/php/php7.0-fpm.sock;#反注释fastcgi_indexindex.php;#反注释includefastcgi_params;#反注释}}#VirtualHostconfigurationforexample.com##Youcanmovethattoadifferentfileundersites-available/andsymlinkthat#tosites-enabled/toenableit.##server{#listen80;#listen[::]:80;##server_nameexample.com;##root/var/www/example.com;#indexindex.html;##location/{#try_files$uri$uri/=404;#}#}

因为thinkphp5中的伪静态,nginx找不到对应的页面,此时就会返回404,修改对404的处理,如下

#try_files $uri $uri/ =404;注释此句,新增下面这段:

if (!-e $request_filename) {
rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}

然后重启nginx

sudo /etc/init.d/nginx restart

这样就可以访问其他页面了,但404没处理好,以后再说,暂时这样处理。