我有一個全新的虛擬機,安裝了 CenotOS 7 最小安裝。
我想要發生的是將 Nginx 配置為從 .net 上的目錄提供靜態檔案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"
答案1
您的系統啟用了 SELinux。預設情況下,SELinux 不允許 Web 伺服器讀取使用者主目錄中的檔案。啟用 Web 服務的目錄是/var/www
(系統套件放置檔案的位置)和/srv/www
(使用者應在生產環境中放置檔案的位置)。
如果您需要從主目錄提供文件,您可以設定 SELinux 布林值httpd_read_user_content
,這將允許讀取這些文件。
setsebool -P httpd_read_user_content 1
請記住,SELinux 永遠不會允許 Web 伺服器寫在使用者主目錄中。如果您需要這個,您需要將您的網路內容放在其他地方,並且使相應的目錄可寫。
也要記住 SELinux 的工作原理另外常規 UNIX 權限,因此相關文件和目錄也必須具有適當的所有權和權限,無論它們適合您的特定用例。