nginx配置文件中root是放在server中还是放在location中,解决nginx File not found错误

前端之家收集整理的这篇文章主要介绍了nginx配置文件中root是放在server中还是放在location中,解决nginx File not found错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
                                            <table class="text"&gt;<tbody><tr class="li1"&gt;

<td class="ln"><pre class="de1">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

PHPstudy搭建的PHP环境,采用的是监听9000端口方式。 在mac上搭建虚拟机默认的是unix sock,所以配置文件出了问题。 用Nginx访问PHP出现 File not found错误。 如果确定有这个a.PHP文件,那么就是PHP-fpm没有找到对应的SCRIPT_FILENAME配置的执行的.PHP文件。 网上说是PHP-fpm返回给Nginx的默认错误(这个没有具体确定) 看一个例子:  geo $dollar { default $; }     server {         listen       80;         server_name  mf.m.com;           location / {             root   D:\PHPStudy\WWW\mf.m.com/dev;             index  index.PHP;             #rewrite ^/(\w+)/(\w+)/(\w+).json$ /index.PHP?site=$1&ctl=$2&act=$3&$query_string last;             rewrite ^/(\w+)/(\w+)/(\w+).json$ /index.PHP?site=$1&ctl=$2&act=$3&$query_string last;             #rewrite ^/(\w+)/(\w+)/(\w+)/(\w+).json$ /index.PHP?site=$1&ctl=$2_$3&act=$4&$query_string last;         }           location ~ \.PHP$ {                 #include fastcgi_params;                 #include fastcgi.conf;                 #fastcgi_index index.PHP;                   fastcgi_pass   127.0.0.1:9000;                 fastcgi_index  index.PHP;                 fastcgi_param  SCRIPT_FILENAME  D:\PHPStudy\WWW\mf.m.leju.com\dev$fastcgi_script_name;                 include        fastcgi_params;                       fastcgi_param DocumentRoot "D:\PHPStudy\WWW\mf.m.com/dev";                     fastcgi_param DOCUMENT_ROOT "D:\PHPStudy\WWW\mf.m.com/dev";                     以下省略。。      贴这段代码的意思是如果不需要单独定义一个loca定义不同的$document_root,那么最好在server中定义  上面那两个documentroot都可以不要了。  在server中配置root,这样各个location快可以继承父server定义的$document_root    另外还有类似的这种情况:  location ~ \.PHP$ {         fastcgi_pass   127.0.0.1:9000;         fastcgi_index  index.PHP;         fastcgi_param  SCRIPT_FILENAME  /var/www/example.com$fastcgi_script_name;         include fastcgi_params;     }  这样也不大好,因为SCRIPT_FILENAME是写死的,不能随着$document_root变化而变化。  所以我们可以修改为如下:  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;    另外,如果真的碰到了不存在的.PHP文件,可以参考以下设置:  location ~ .PHP$ {  try_files $uri =404;  fastcgi_pass 127.0.0.1:9000;  fastcgi_index index.PHP;  fastcgi_param SCRIPT_FILENAME ....  ...................................  ................................... } 上面的配置会检查.PHP文件是否存在,如果不存在,会返回404页面   参考:http://www.Nginx.cn/562.html            

猜你在找的程序笔记相关文章