Nginx 更改网站根目录教程 - 顺便解决403 Forbidden | File not found

本博结构

一、 介绍更改网站根目录之前的基本环境
二、 介绍更改步骤

  • 更改配置文件(更改后重新加载)
  • 403 Forbidden错误和File not found错误
  • 如何解决错误

以下为正文,以及,不愿意看基本环境的直接跳到 看步骤和问题

安装完LNMP之后,网站的根目录是/usr/share/nginx/html 这个用起来非常不方便, for instance : 直接输入cd命令,打开的是~,要打开网站根目录,则需要输入一长串 /usr/share/nginx/html不爽,时间就是生命啊,所以博主将网站根目录改为 ~,具体的绝对路径就是 /root(可能有不一样的,根据自己的实际情况来)。

接下来就介绍到底如何更改。

一、基础环境


##1. 服务器环境

  • CentOS 6.7
  • Nginx/1.0.15
    版本

##2. Nginx初始配置文件

我的nginx配置文件位置 /etc/nginx/nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {	 
	include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {    
        listen       80;	      
		server_name  localhost; #以后要绑定域名的时候改这里
		index index.php index.html index.htm;

        location / {
            root   /urs/share/nginx/hmtl; #这里是文件的根目录
            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   html; #这里是出50*错之后的展示页面位置
	    }
	    
	    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;
        }
    }
}

其实nginx配置文件的基本款挺易懂的,还是认真看看比较好,否则该错误就是一头雾水了。

可以看到当前服务器的根目录是 /usr/share/nginx/html,用起来挺不方便的:比如你输入cd命令,打开的是~而不是/usr/share/nginx/html 。 所以我们想把根目录定位到~,在我的服务器上~/root

要注意,更改完配置之后,需要重载nginx的配置文件(用这种方法不用停服)
service nginx reload

二、 更改方法


##1. 更改Nginx配置文件

将我的网站根目录改为~也就是/root

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
	
	server {
        listen       80;
        server_name  localhost;

        index index.php index.html index.htm;

        location / {
            root   /root; #这里改了
            index  index.php index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        #    root   html;
        }
        
        location ~ \.php$ {
            root           /root; #这里改了
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

记得重载配置!
service nginx reload

##2. 可能会出现的问题
改完之后可能会出现如下两个问题:

  • 403 Forbidden
  • File not found

这两个问题都源于 /root(更改后的网站根目录)的权限。

##3. 问题解答

需要给根目录加上可写权限

###3.1 未更改前的文件权限
未更改的/root权限

3.2 更改权限

chmod a+w .

3.3 更改后的权限

这里写图片描述

##4. 成功 !
这里写图片描述

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值