CenotOS 7 最小インストール用の Nginx 静的ファイル構成

CenotOS 7 最小インストール用の Nginx 静的ファイル構成

CenotOS 7 最小インストールを備えた新しい VM があります。

私が実現したいのは、 のディレクトリから静的ファイルを提供するように Nginx を構成することですlocalhost:80

私のディレクトリは です/home/kenny/projects/kcrypt/dist/

私の内容は次のとおりです/etc/nginx/nginx.conf:

# this is set to root in order to rule out
# any permission related issues.
user root;    

worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        root         /home/kenny/projects/kcrypt/dist/;
        index index.html;

        location / {
        }
    }
}

実行すると、curl http://localhost次の応答が返されます:

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>

考えられるあらゆるディレクトリにあらゆる種類の権限を付与してみました。

最終的に、Nginx を root として実行するように構成しました。

すでに何度も OS を再インストールしましたが、動作しません。

追伸

これは私が見つけたものです/var/log/nginx/error.log

2018/02/27 21:33:19 [error] 15689#0: *1 open() "/home/kenny/projects/kcrypt/dist/index.html" failed (13: Permission denied), client: ::1, server: , request: "GET / HTTP/1.1", host: "localhost"
2018/02/27 21:33:35 [error] 15690#0: *2 open() "/home/kenny/projects/kcrypt/dist/index.html" failed (13: Permission denied), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/02/27 21:33:38 [error] 15690#0: *3 open() "/home/kenny/projects/kcrypt/dist/index.html" failed (13: Permission denied), client: ::1, server: , request: "GET / HTTP/1.1", host: "localhost"

何を望んでいるのか分かりません... できる限りの権限を与えました。

ルート ディレクトリにあるのは次の通りです。

[root@vm3 dist]# ll
total 368K
drwxrwxrwx. 2 root root   98 Feb 26 23:16 .
drwxrwxrwx. 6 root root  234 Feb 27 21:26 ..
-rwxrwxrwx. 1 root root 1.2K Feb 26 23:16 favicon.ico
-rwxrwxrwx. 1 root root 1.7K Feb 26 23:16 index.html
-rwxrwxrwx. 1 root root 175K Feb 26 22:53 index.js
-rwxrwxrwx. 1 root root  297 Feb 26 23:16 manifest.json
-rwxrwxrwx. 1 root root 179K Feb 26 22:53 styles.css

追伸2

静的ファイルを に入れてみました/var/www/kcrypt/dist/が、結果は出ませんでした。

まだ同じエラーメッセージが表示されます:

2018/02/27 23:18:11 [error] 16157#0: *1 open() "/var/www/kcrypt/dist/index.html" failed (13: Permission denied), client: ::1, server: , request: "GET / HTTP/1.1", host: "localhost"
2018/02/27 23:20:58 [error] 16535#0: *1 open() "/var/www/kcrypt/dist/index.html" failed (13: Permission denied), client: ::1, server: , request: "GET / HTTP/1.1", host: "localhost"
2018/02/27 23:21:30 [error] 16564#0: *1 open() "/var/www/kcrypt/dist/index.html" failed (13: Permission denied), client: ::1, server: , request: "GET / HTTP/1.1", host: "localhost"

答え1

システムで SELinux が有効になっています。デフォルトでは、SELinux は Web サーバーがユーザーのホーム ディレクトリ内のファイルを読み取ることを許可しません。Web サービスに有効になっているディレクトリは、/var/www(システム パッケージがファイルを置く場所) と/srv/www(ユーザーが本番環境でファイルを置くことが予想される場所) です。

httpd_read_user_contentホームディレクトリからファイルを提供する必要がある場合は、これらのファイルの読み取りを許可するSELinux ブール値を設定できます。

setsebool -P httpd_read_user_content 1

SELinuxはウェブサーバーが書くユーザーのホームディレクトリに。これが必要な場合は、Webコンテンツを他の場所に置く必要があります。適切なディレクトリを書き込み可能にする


また、SELinuxが機能していることにも留意してくださいに加えて通常の UNIX 権限であるため、関連するファイルとディレクトリにも、特定のユースケースに応じて適切な所有権と権限が設定されている必要があります。

関連情報