你好,游客 登录 注册 搜索
背景:
阅读新闻

Nginx 403 forbidden的解决办法

[日期:2017-08-07] 来源:Linux社区  作者:Linux [字体: ]

以下是我的Nginx配置:

user  root; 
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        #autoindex    on; #是否允许访问目录

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            root   /home/ftpuser/taotao; #自定义访问的根目录
            index  index.html index.htm;
            #autoindex    on; #是否允许访问目录
        }

        #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;
        }
}

解决办法一(不推荐)

在nginx.conf配置文件对应的位置添加以下两句,表示使用root角色访问,并且允许访问目录。autoindex默认是off。可是这种方式不好,因为别人可以看到自己服务器的目录结构。

user root;

autoindex on; #是否允许访问目录

注:配置完成之后需要重启nginx服务,方能生效。

[root@localhost sbin]# ./nginx -s reload

解决办法二(推荐)

方法二的配置文件即为开篇所写的配置文件:

user  root; 
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        #autoindex    on; #是否允许访问目录

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            root   /home/ftpuser/taotao; #自定义访问的根目录
            index  index.html index.htm;
            #autoindex    on; #是否允许访问目录
        }

        #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;
        }
}

按照上述配置文件配置完成之后重启nginx服务

[root@localhost sbin]# ./nginx -s reload

发现已经可以访问自定义根目录文件夹(/home/ftpuser/taotao)下面的具体文件了,但是访问根路径的时候还是显示403 forbidden的错误。

这是因为在自定义访问的根目录下(/home/ftpuser/taotao)缺少nginx启动后默认访问的index.html文件,将index.html文件复制一份到根目录(/home/ftpuser/taotao)下即可正常访问了。

CentOS 7下Nginx服务器的安装配置  http://www.linuxidc.com/Linux/2017-04/142986.htm

CentOS上安装Nginx服务器实现虚拟主机和域名重定向  http://www.linuxidc.com/Linux/2017-04/142642.htm

CentOS 6.8 安装LNMP环境(Linux+Nginx+MySQL+PHP)  http://www.linuxidc.com/Linux/2017-04/142880.htm

Linux下安装PHP环境并配置Nginx支持php-fpm模块  http://www.linuxidc.com/Linux/2017-05/144333.htm

Nginx服务的SSL认证和htpasswd认证  http://www.linuxidc.com/Linux/2017-04/142478.htm

Ubuntu 16.04上启用加密安全的Nginx Web服务器  http://www.linuxidc.com/Linux/2017-07/145522.htm

Linux中安装配置Nginx及参数详解  http://www.linuxidc.com/Linux/2017-05/143853.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

CentOS 7.2下Nginx+PHP+MySQL+Memcache缓存服务器安装配置  http://www.linuxidc.com/Linux/2017-03/142168.htm

CentOS6.9编译安装Nginx1.4.7  http://www.linuxidc.com/Linux/2017-06/144473.htm

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-08/146084.htm

linux
相关资讯       nginx 403 forbidden 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款