`
ngcsnow
  • 浏览: 33526 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

nginx静态资源文件无法访问,403 forbidden错误

阅读更多

      今天在搭建nginx环境时出现一个奇怪问题,配置的静态资源目录下面文件无法访问,浏览器访问出现403 forbidden,环境是centos7 + nginx 1.6。nginx.conf中http配置如下:

 

……

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

    upstream  tomcat_server {
        server   10.10.100.52:8080;
    }

    server {
        listen  80;
        charset  utf-8;
        server_name  localhost;

        location /fcm/ {
             index  index.html index.htm;
             proxy_pass       http://tomcat_server;
             proxy_set_header  X-Real-IP  $remote_addr;
             client_max_body_size  100m;
        }

        location /static/ {
           root /home/www/static;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

……

 

      从配置上看没有什么问题,而且这里配置的fcm转发到tomcat是没问题的,之前环境都是这么配的都没问题,查看nginx的log文件,发现错误日志中静态文件访问都出现 Permission denied的权限错误,但是将/home/www/static目录赋予777的最高权限还是不能解决。

 

      后来看见nginx.conf头部有一行注释的#user  nobody; 遂想可能和这有关系,取消注释,重启nginx,访问还是有问题,查了一下这行是设置nginx运行用户的,遂将nobody改成root,重启好了,找了好久的问题居然是这个原因,喜大普奔!!

 

      后来想以前我都是在linux的root用户下安装软件和操作,nginx是在root下安装的,所以没设user也没问题,这次由于centos7刚出来所以我下载了一个cenos尝鲜,并新建的一个用户,不是用root用户操作的所以就不和谐了。

 

      所以最终解决方法是在nginx.conf配置文件头部加user root:

 

user  root;
worker_processes  8;

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

……

 

  

 

 

分享到:
评论
1 楼 kylix1 2016-04-13  
问题是解决了没错,但用root用户来运行nginx,测试用是没问题的,用作生产那就。。。

相关推荐

Global site tag (gtag.js) - Google Analytics