Nginx 显示欢迎 Page/403 错误而不是主页

Nginx Showing Welcome Page/403 Error instead of homepage

我使用的是 CentOs 6.8

在开发模式下使用 CloudFlare DNS

Nginx 显示欢迎页面,取自

/usr/share/nginx/html

但不是来自:

/home/nginx/domains/XYZDomain.com/public/

在目录中:

/etc/nginx/conf.d

2 个配置文件:

default.conf
virtual.conf

default.conf 文件输出

# Main Local

server {
        listen 80;
        server_name localhost;
        root   /usr/share/nginx/html;
        index index.php index.html index.htm;


        location / {
                try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        #
        location ~ \.php$ {
                include fastcgi_params;
                #fastcgi_pass 127.0.0.1:9000;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        }

        location ~ /\. {
                deny all;
                access_log off;
                log_not_found off;
        }
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

}

virtual.conf 文件输出:

server {

            listen   80;
            #server_name  www.XYZDomain.com;
            # Now Changed to below withouth wwww.
            server_name  XYZDomain.com;
            rewrite ^/(.*) http://XYZDomain.com/ permanent;

            location / {

                        root   /home/nginx/domains/XYZDomain.com/public/;
                        index  index.html;

                        }

}

nginx.conf 文件在 /etc/nginx/nginx.conf 输出

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    #default_type  application/octet-stream;
    #changed to text/html
    default_type text/html;
    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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

所有具有权限的目录。

ls -la /home/nginx/domains/XYZDomain.com

drwxr-s--- 6 root root 4096 Jul  1 12:54 .
drwxr-xr-x 3 root root 4096 Jul  2 14:44 ..
drwxr-s--- 2 root root 4096 Jun 30 15:58 backup
drwxr-s--- 2 root root 4096 Jun 30 15:58 log
drwxr-s--- 2 root root 4096 Jun 30 15:58 private
drwxr-sr-x 2 root root 4096 Jul  2 15:01 public

我已经尝试修改 default.php 和 virtual.conf 文件

谁能帮我看看这是怎么回事?我真的很困惑,为此浪费了一整天。

Nginx 403 error: directory index of [folder] is forbidden

更改用户 /etc/nginx/nginx.confnginxroot

帮助...

有害吗?或安全风险?现在这是一个问题...

感谢 Whosebug & 感谢社区

看起来这是一个权限问题。 Nginx 运行宁作为 nginx 用户,但文件归 root 所有并且没有全局读取权限,这意味着 nginx 用户看不到它们。

首先,不要运行 ngninx 作为root! 这非常糟糕。如果有人破坏了您的站点,他们可能拥有对服务器的根访问权限。

有几种方法可以解决这个问题。最简单的方法是将文件的所有者更改为 nginx:

chown -R nginx:nginx /home/nginx/domains/XYZDomain.com

当然,您必须记住对您创建的任何新文件执行此操作

您也可以只将文件的组更改为 nginx,并使它们组可读:

chgrp -R nginx /home/nginx/domains/XYZDomain.com
chmod -R g+r /home/nginx/domains/XYZDomain.com

第二种方式安全一些,因为nginx没有写入权限,只能读取文件。但是,如果您有需要动态创建或编辑文件的脚本,这可能会导致其他问题。

关于权限和 Web 服务器还有很多内容需要深入探讨,其中涉及很多安全隐患,这里无法深入探讨。如果您好奇,可以在 whosebug.com 上找到大量信息。

一种方法是将用户更改为 root 但它是有害的。

其次是 将所有域文件传输到

/var/www/html/XYZDomain.com/public 而不是 /home/nginx/domains/XYZDomain.com/public/

并更改了 virtual.conf(在 conf.d 目录中)文件 来自 root /home/nginx/domains/XYZDomain.com/public; root /var/www/html/XYZDomain.com/public

需要注意的事情是:

  1. 检查多余的无用空间

  2. 从网上复制的代码可能会导致问题

  3. 始终运行nginx -T检查错误

  4. 总是在测试之前重启 Nginx service nginx restart

  5. 运行 tail -f /var/log/nginx/error.log 检查最新的错误日志

  6. 确保您的网站目录具有访问权限

  7. 切换 SELinux,它可能会导致问题。