AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 899187
Accepted
Hristo Kolev
Hristo Kolev
Asked: 2018-02-28 11:46:03 +0800 CST2018-02-28 11:46:03 +0800 CST 2018-02-28 11:46:03 +0800 CST

CenotOS 7 最小安装的 Nginx 静态文件配置

  • 772

我有一个带有 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"
nginx
  • 1 1 个回答
  • 2036 Views

1 个回答

  • Voted
  1. Best Answer
    Michael Hampton
    2018-02-28T12:55:56+08:002018-02-28T12:55:56+08:00

    您的系统已启用 SELinux。默认情况下,SELinux 不允许 Web 服务器读取用户主目录中的文件。为 Web 服务启用的目录是/var/www(系统包放置文件的位置)和/srv/www(期望用户在生产环境中放置文件的位置)。

    如果您需要从主目录提供文件,您可以设置 SELinux boolean httpd_read_user_content,这将允许读取这些文件。

    setsebool -P httpd_read_user_content 1
    

    请记住,SELinux 永远不会允许 Web 服务器写入用户主目录。如果您需要这个,您需要将您的网页内容放在其他地方,并使相应的目录可写。


    还要记住,SELinux除了常规的 UNIX 权限外还可以工作,因此相关文件和目录也必须具有适当的所有权和权限,无论它们适用于您的特定用例。

    • 4

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve