如何配置nginx

时间:2017-08-01 02:29:05

标签: php nginx

我在centos7上构建lnmp。然后我通过chrome访问它。结果是:

  

找不到档案。

我的nginx配置文件如下:

server {
listen       80;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;

location / {
    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;
}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}
# 添加下面内容
location ~ \.php$ {
   fastcgi_pass        127.0.0.1:9000;
   fastcgi_index       index.php;
   fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
   include fastcgi_params;
   #include fastcgi.conf;
  }

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}

我的目录:

[michael@localhost html]$ pwd
/usr/share/nginx/html
[michael@localhost html]$ ll
total 35272
-rw-r--r--. 1 michael www        15086 Jun 26  2016 favicon.ico
-rw-rw-r--. 1 michael michael        7 Jul 31 21:49 index.html
-rw-rw-r--. 1 michael michael       20 Jul 24 04:14 index.php
-rw-r--r--. 1 michael www          432 Aug  5  2016 index.php.bak
-rw-r--r--. 1 michael www     36085974 Feb 20 03:50 iwwenbo.zip
drwxr-xr-x. 3 michael www           52 Aug 17  2016 sites
[michael@localhost html]$ cat index.php
<?php
 phpinfo();
?>

谁能告诉我?有什么不对?我已多次尝试过。它似乎无法找到一些文件,但index.php已经存在,我不知道找不到哪个文件。

1 个答案:

答案 0 :(得分:0)

最后,我解决了。我修改了配置文件。

location ~ \.php$ {
root   /usr/share/nginx/html; 
fastcgi_pass        127.0.0.1:9000;
fastcgi_index       index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
#include fastcgi.conf;

}

是的,我添加了一行root /usr/share/nginx/html;