首页 > 解决方案 > Nginx 显示欢迎页面/403 错误而不是主页

问题描述

我正在使用 CentOs 6.8

在开发模式下使用 CloudFlare DNS

Nginx 显示欢迎页面,从

/usr/share/nginx/html

但不是来自:

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

在目录中:

/etc/nginx/conf.d

2个配置文件:

default.conf
虚拟.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/$1 permanent;

            location / {

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

                        }

}

/etc/nginx/nginx.conf 输出中的 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 文件

任何人都可以帮助我这有什么问题吗?我真的很困惑,为此浪费了一整天。

标签: nginxservervpscentos6.8

解决方案


看起来这是一个权限问题。Nginx 以nginx用户身份运行,但文件归其所有root且没有全局读取权限,这意味着nginx用户无法看到它们。

首先,不要以 root 身份运行 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 服务器还有很多内容需要了解,其中涉及很多安全隐患,这里就不多说了。如果您好奇,可以在 stackoverflow.com 上找到大量信息。


推荐阅读