Nginx 同样碰到 403 问题了 directory index of "/*/public/" is forbidden

patxiao · 2014年05月24日 · 最后由 udonmai 回复于 2015年03月30日 · 42497 次阅读

同样碰到 403 问题了,请帮看看 直接 rails s 启动可以正常 ip 访问 感觉 server_name pattest.domain1.com;并未生效,通过http://pattest.domain1.com:3000/http://pattest.domain2.com:3000/403或者访问都是

配置: cat /etc/nginx/sites-enabled/example.com.conf server { #listen 80 default; listen 3000; server_name pattest.domain1.com; # 这里填写你真实域名 root /var/www/pattest.domain1.com/pat/public; passenger_ruby /home/lpxiao/.rvm/rubies/ruby-2.1.0/bin/ruby; passenger_enabled on; }

nginx log: 2014/05/23 23:04:36 [error] 35484#0: *4 directory index of "/var/www/pattest.domain1.com/pat/public/" is forbidden, client: 123.114.36.75, server: pattest.domain1.com, request: "GET / HTTP/1.1", host: "pattest.domain1.com:3000"

ror public: drwxr-xr-x 3 lpxiao lpxiao 4096 May 23 22:33 public

补充:在 public 下放/index.html 能正常访问到

论坛,stackoverflow 的 case 都看过了,都没解决。。

public 目录和应用根目录拥有者是什么?

#3 楼 @Rei 终于等来啦,public 和应用根目录/var/www/pattest.domain1.com/pat 的拥有者都是 lpxiao

#4 楼 @patxiao 不要等,我不知道问题在哪。

#3 楼 @Rei drwxr-xr-x 15 lpxiao lpxiao 4096 May 23 22:33 pat drwxr-xr-x 2 lpxiao lpxiao 4096 May 24 16:29 public 基本上是按照“Ubuntu 12.04 上使用 Nginx Passenger 部署 Ruby on Rails”操作的,不同之处是 pat 这个应用之前已经可以通过 rails s 启动,没有 bundle

#5 楼 @Rei 能经验的给一些调试思路吗?现在是加 autoindex on;能正常访问目录;增加 Index.html 文件也能访问到,同时如果用的是另一个非 nginx conf 中配置的域名也能访问到

给个思路

  1. 检查 nginx 的运行用户 ps -ef| grep nginx 或者看 /etc/nginx/nginx.conf 的 user 配置
  2. 通过 sudo -u <nginx_user> ls /var/www/pattest.domain1.com/pat/public/ 来检查是否真的 permission deny
  3. 基本上就是权限不对,所以请 sudo chown <nginx_user>: -R /var/www/pattest.domain1.com/pat/public/

这样还搞不定的话,赶紧去学一下 linux 吧..

另外...为啥老用 passenger 啊...这货我就没搞明白他好在哪....部署超麻烦诶..

#8 楼 @ruohanc 再看下?

  1. 运营用户是 user www-data;;
  2. 可以正常 ls
  3. 没有用 public 下面如果有 index.html 是可以正常访问的,看 nginx 日志就是 public 无权限,public +777 和改 owner 都没有用

#10 楼 @patxiao 该贴的日志贴出来吧,包括前面运行的命令和命令输出的结果....经过你自己的理解过滤一遍说不准已经有偏差了...

#11 楼 @ruohanc

  1. shell ps -ef| grep nginx root 45240 1 0 21:02 ? 00:00:00 nginx: master process /usr/sbin/nginx www-data 45241 45240 0 21:02 ? 00:00:00 nginx: worker process www-data 45242 45240 0 21:02 ? 00:00:00 nginx: worker process www-data 45243 45240 0 21:02 ? 00:00:00 nginx: worker process www-data 45244 45240 0 21:02 ? 00:00:00 nginx: worker process lpxiao 45454 43362 0 21:23 pts/1 00:00:00 grep --color=auto nginx
cat /etc/nginx/nginx.conf | grep user
user www-data;

2.

sudo -u www-data ls /var/www/pattest.domain1.com/pat/public/
[sudo] password for lpxiao: 
404.html  422.html  500.html  assets  favicon.png  robots.txt

3.

sudo chown www-data: -R public/
[sudo] password for lpxiao: 

ll | grep public
drwxr-xr-x  3 www-data www-data  4096 May 24 21:11 public/

log,偶发报 favicon.ico,不知啥原因

2014/05/24 21:33:10 [error] 45241#0: *12 open() "/var/www/pattest.domain1.com/pat/public/favicon.ico" failed (2: No such file or directory), client: 123.114.36.75, server: pattest.domain1.com, request: "GET /favicon.ico HTTP/1.1", host: "pattest.domain2.cn"
2014/05/24 21:33:44 [error] 45241#0: *16 directory index of "/var/www/pattest.domain1.com/pat/public/" is forbidden, client: 123.114.36.75, server: pattest.domain1.com, request: "GET / HTTP/1.1", host: "pattest.domain1.com"
2014/05/24 21:34:38 [error] 45241#0: *16 directory index of "/var/www/pattest.domain1.com/pat/public/" is forbidden, client: 123.114.36.75, server: pattest.domain1.com, request: "GET / HTTP/1.1", host: "pattest.domain1.com"
2014/05/24 21:35:49 [error] 45241#0: *18 directory index of "/var/www/pattest.domain1.com/pat/public/" is forbidden, client: 123.114.36.75, server: pattest.domain1.com, request: "GET / HTTP/1.1", host: "pattest.domain1.com"

#11 楼 @ruohanc nginx 配置没动过

cat /etc/nginx/nginx.conf 
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        # include /etc/nginx/naxsi_core.rules;

        ##
        # Phusion Passenger config
        ##
        # Uncomment it if you installed passenger or passenger-enterprise
        ##

        # passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
        # passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


# mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
# 
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
# }

按文档加的

cat /etc/nginx/sites-enabled/example.com.conf 
server {
    listen 80 default;
    #listen 3000;
    server_name pattest.domain1.com;
    root /var/www/pattest.domain1.com/pat/public;
    passenger_ruby /home/lpxiao/.rvm/rubies/ruby-2.1.0/bin/ruby;
    passenger_enabled on;
}

还有个怪事,我有两个域名 a.com,b.com,pat.a.com,pat.b.com 都解析到这个 ip,但是 nginx 里只配 pat.a.com,两个访问都是 403,也都导向 root 目录了,nginx 没过滤我的域名?

关于这事,我每次都反复教别人,如果你弄不清 Linux 环境用户权限的事情,那就简单点儿!项目代码、数据库、Log、上传文件…通通放在用户的 Home 目录,Nginx 用那个用户跑

##
# Phusion Passenger config
##
# Uncomment it if you installed passenger or passenger-enterprise
##

# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;

#16 楼 @Rei 多谢 rei,连上了 @huacnlee 找时间把环境用户权限好好学学

#17 楼 @patxiao 你这问题好像不是一般的用户权限问题........passenger 不知道在搞什么鬼..

#18 楼 @ruohanc passenger 没打开,我写的教程也漏了。

#18 楼 @ruohanc passenger 权限比自己管理还简单,检查应用根目录所属用户,然后用这个用户身份运行。但是为了安全,不能用 root 用户运行。

楼主自己文档没看仔细吧。

nginx 示例代码里面肯定是有提示需要配置 passenger_root 的。

#17 楼 @patxiao 果断抛弃 passenger 吧。投入 unicorn 和 puma 的怀抱吧。

#20 楼 @Rei .....好牛逼的 feature.....

24 楼 已删除

搜了好久找到这个帖子,加了这个

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;

之后第一次觉得能看到 RoR 报错也是一种幸福。。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号