CentOs 7 Nginx(13:权限被拒绝)从 css 加载字体文件时

CentOs 7 Nginx(13:权限被拒绝)从 css 加载字体文件时

我有一个带有 bootstrap js 框架的 nginx web 服务器。我正在尝试让 css 能够加载字体文件。尽管当浏览器尝试访问字体文件时出现错误 403。

我已经在 serverfault 中查找了那些有类似问题的人,尽管我没有成功应用他们的解决方案。例如:“如何让 nginx 支持 @font-face 格式并允许 access-control-allow-origin?

以下是我的错误和我的服务器配置:

错误:

2016/01/07 10:30:10 [error] 20770#0: *26 open() "/usr/share/nginx/html/fonts/glyphicons-halflings-regular.ttf" failed (13: Permission denied), client: 0.0.0.0, server: localhost, request: "GET /fonts/glyphicons-halflings-regular.ttf HTTP/1.1", host: "localhost", referrer: "http://localhost/css/bootstrap.min.css"

/etc/nginx/conf.d/default.conf:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;

        if ($request_filename ~* ^.*?/([^/]*?)$)
            {
                set $filename $1;
            }

        if ($filename ~* ^.*?\.(eot)|(ttf)|(woff)$){
                add_header Access-Control-Allow-Origin *;
           }
    }

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    location ~ \.php$ {
        root           /usr/share/nginx/html;
        try_files $uri =404;
        fastcgi_pass   unix:/var/run/php-fpm/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }


    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
     #
    location ~ /\.ht {
        deny  all;
    }
}

/etc/nginx/mime.类型:

application/vnd.ms-fontobject         eot;
    application/font-sfnt                 otf ttf;
    application/font-woff                 woff;
    application/font-woff2                woff2;

systemctl 重启 nginx.service

答案1

这是因为 nginx 无法打开文件(而不是 nginx 本身拒绝访问),请确保 nginx 的用户有权读取 glyphicons-halflings.ttf

相关内容