在上篇文章中(点击打开链接)我们安装了LNMP环境,但是在安装完成之后我发现Nginx的根目录在/usr/share/nginx/html/  下面,一般都是放在/var/www/html/ 目录下面。所以我们来修改一下根目录所在地。

打开nginx配置文件,nginx.conf。里面的include的意思是,加载制定目录下面的所有以.conf结尾的配置

我把我的配置放在了 /etc/nginx/conf.d/*.conf; 下面

所以我们找到指定目录进行编辑。

#vim /etc/nginx/conf.d/default.conf
 
server{
    listen 80; #监听80端口
    server_name test.com; #访问的网址 例如:baidu.com   qq.com
    root /var/www/html; #制定根目录 例如:/var/www/html/baidu /var/www/html/qq
    sendfile on;
    #文件上传大小限制
    client_max_body_size    1000m;
 
    location / {
        index index.php index.html index.htm;
        if (-e $request_filename) {
            break;
        }
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php/$1 last;
            break;
        }
    }
    
    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/:/var/www/html";#这里是你的项目地址
        fastcgi_param HTTP_PROXY "";
        include fastcgi_params;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

//重启nginx

service nginx restart

403 Forbidden 解决办法

当访问时候,nginx 会按照 index.php  index.html  index.htm 的先后顺序在根目录中查找文件。如果这三个文件都不存在,那么nginx就会返回403 Forbidden。所以你可以吧你的/usr/share/nginx/html/目录下面的文件移动到/var/www/html/  即可,或者建立一个index.php index.html。

更多推荐