如果目录存在,nginx给403

时间:2015-11-08 15:24:09

标签: angularjs nginx server

我有以下配置可以正常工作,但如果目录存在于url路径中,我仍然会得到403.

例如:domainname.com/users

如果用户目录存在,我得到403而不是加载index.html。

server {
    listen  80;

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

    server_name www.domainname.com;
    return 301 https://www.domainname.com$request_uri;
    root /usr/share/nginx/dist/;
    index  index.html;

    location / {
      autoindex off;
      try_files $uri /index.html =404;
    }


    # these settings are from https://github.com/h5bp/server-configs-nginx/blob/master/h5bp/location/expires.conf
    # feel free to change as much as you like
    # cache.appcache, your document html and data
    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
      expires -1;
      access_log logs/static.log;
    }

    # Feed
    location ~* \.(?:rss|atom)$ {
      expires 1h;
      add_header Cache-Control "public";
    }

    # Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
      expires 1M;
      access_log off;
      add_header Cache-Control "public";
    }

    # CSS and Javascript
    location ~* \.(?:css|js)$ {
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }
}

2 个答案:

答案 0 :(得分:0)

我认为您需要在$url/中指定try_files,以便在缺少尾随/时测试目录是否存在:

try_files $uri $url/ /index.html =404;

当匹配目录时,这应该遵守index指令。

答案 1 :(得分:0)

问题是我有$ uri / in' / etc / nginx / sites-available / default',只需删除它。