我有一个带有 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 身份运行。
我已经多次重新安装操作系统,但无法正常工作。
附言
这是我发现的/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
PS 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"
您的系统已启用 SELinux。默认情况下,SELinux 不允许 Web 服务器读取用户主目录中的文件。为 Web 服务启用的目录是
/var/www
(系统包放置文件的位置)和/srv/www
(期望用户在生产环境中放置文件的位置)。如果您需要从主目录提供文件,您可以设置 SELinux boolean
httpd_read_user_content
,这将允许读取这些文件。请记住,SELinux 永远不会允许 Web 服务器写入用户主目录。如果您需要这个,您需要将您的网页内容放在其他地方,并使相应的目录可写。
还要记住,SELinux除了常规的 UNIX 权限外还可以工作,因此相关文件和目录也必须具有适当的所有权和权限,无论它们适用于您的特定用例。