此篇文章谨记录怎么解决在配置Nginx服务器伪静态的坎坎坷坷。
今天周六,又到了我自己的些许时间。我就试着研究一下将自己博客的服务器Apache换成Nginx,各种优缺点我自己比对过,不论当前是否适合,我觉得我要去尝试一下,万一哪天用到的时候,可以沉着应对。
我用的phpstudy集成环境,博客是wordpress,当前Apache配置了伪静态,但是当我切换到Nginx的时候,我根本无从下手,没有接触过Nginx的我开始一步一步的进行测试。首先切换到Nginx后,第一件事情肯定是配置伪静态,否则我当前的博客就会打不开,经过我的多次研究查看资料,我可以把自己的博客设置伪静态了。
方法一:
打开Nginx目录中的conf文件夹,里面是一些相关的配置文件,打开文件中的vhosts.conf,若网站不止一个,则找到要设置伪静态网站的那一段server{}代码,比如我这个:
- server {
- listen 80;
- server_name 自己网站域名 ;
- root “D:\phpStudy\PHPTutorial\WWW\自己网站文件夹”;
- location / {
- index index.html index.htm index.php;
- #autoindex on;
- }
- location ~ \.php(.*)$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param PATH_INFO $fastcgi_path_info;
- fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
- include fastcgi_params;
- }
在#autoindex on下面回车,将如下代码粘贴进去
- if (-f $request_filename/index.html){
- rewrite (.*) $1/index.html break;
- }
- if (-f $request_filename/index.php){
- rewrite (.*) $1/index.php;
- }
- if (!-f $request_filename){
- rewrite (.*) /index.php;
- }
完成以上的调整之后,保存,并重启Nginx服务器,这个时间,博客就可以打开了,如果打开wordpres后台的时候,发现页面为404错误,这个时候,我们需要在location的外面加一行代码,如下:
- rewrite /wp-admin$ $scheme://$host$uri/ permanent;
这样就可以正常登陆我们的wordpress后台。加完后整体代码如下:
- location / {
- index index.html index.htm index.php;
- #autoindex on;
- # 伪静态配置
- if (-f $request_filename/index.html){
- rewrite (.*) $1/index.html break;
- }
- if (-f $request_filename/index.php){
- rewrite (.*) $1/index.php;
- }
- if (!-f $request_filename){
- rewrite (.*) /index.php;
- }
- }
- #伪静态后,防止wordpres后台打不开
- rewrite /wp-admin$ $scheme://$host$uri/ permanent;
这样,我们就设置完了Nginx的伪静态。
方法二:
前面的几个步骤一样,就是同样找到自己网站的那一段server{}代码,在#autoindex on下面回车,将如下代码粘贴进去
- try_files $uri $uri/ /index.php?$args;
然后,再在这个location的下面添加方法一中的一行代码即可,即
- rewrite /wp-admin$ $scheme://$host$uri/ permanent;
全部添加的样子为(第7行和第10行是我们刚添加的):
- server {
- listen 80;
- server_name www.inqingdao.cn ;
- root “D:\phpStudy\PHPTutorial\WWW\我的伪静态网站名”;
- location / {
- index index.html index.htm index.php;
- try_files $uri $uri/ /index.php?$args;
- #autoindex on;
- }
- rewrite /wp-admin$ $scheme://$host$uri/ permanent;
- location ~ \.php(.*)$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param PATH_INFO $fastcgi_path_info;
- fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
- include fastcgi_params;
- }
- }
这样,伪静态设置也是完成了。
完美!谢谢