我有3个域名,并试图使用Nginx在一台服务器上托管所有3个网站(一个数字海洋液滴)。

mysite1.name mysite2.name mysite3.name

只有1个有效。另外两个结果是403个错误(以同样的方式)。

在我的nginx错误日志中,我看到:[error] 13108#0: *1目录索引“/usr/share/nginx/mysite2.name/live/”被禁止。

我的站点启用配置是:

server {
        server_name www.mysite2.name;
        return 301 $scheme://mysite2.name$request_uri;
}
server {
        server_name     mysite2.name;

        root /usr/share/nginx/mysite2.name/live/;
        index index.html index.htm index.php;

        location / {
                try_files $uri $uri/ /index.html index.php;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

这三个站点都有几乎相同的配置文件。

每个站点的文件都在/usr/share/nginx/mysite1.name/someFolder这样的文件夹中,然后/usr/share/nginx/mysite1.name/live是指向该文件夹的符号链接。(mysite2和mysite3也是如此。)

我已经查看了Nginx 403禁止所有文件,但这没有帮助。

有什么问题吗?


当前回答

您需要对静态文件目录执行权限。此外,它们需要被你的nginx用户和组咀嚼。

其他回答

你可能会得到这个,因为Nginx政策(例如。"deny"),或者你可能得到这个是因为Nginx错误配置,或者你可能得到这个是因为文件系统限制。

你可以确定是否是后者(并可能通过使用strace看到错误配置的证据(除非OP无法访问它):

# pidof nginx
11853 11852

# strace -p 11853 -p 11852 -e trace=file -f
Process 11853 attached - interrupt to quit
Process 11852 attached - interrupt to quit
[pid 11853] stat("/var/www/html/kibanaindex.html", 0x7ffe04e93000) = -1 ENOENT (No such file or directory)
[pid 11853] stat("/var/www/html/kibana", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
^CProcess 11853 detached
Process 11852 detached

在这里,我正在检查nginx在运行测试时所做的文件系统活动(我有和你一样的错误)。

这是我当时配置的一部分

    location /kibana/3/ {
        alias /var/www/html/kibana;
        index index.html;
    }

在我的情况下,正如strace非常清楚地显示,在“别名”到“索引”的连接不是我所期望的,似乎我需要养成总是用/追加目录名的习惯,因此在我的情况下,以下工作:

    location /kibana/3/ {
        alias /var/www/html/kibana/;
        index index.html;
    }

看起来像是权限问题。

尝试像在mysite1中那样设置其他站点的所有权限。

默认情况下,文件权限应该是644和dirs 755。 还要检查运行nginx的用户是否有权限读取这些文件和dirs。

检查目录中是否缺少index.html或index.php 查看位于/var/log/nginx目录下的错误日志文件,然后打开 vim error.log

对我来说,问题是除了基本路线以外的任何路线都可以工作,添加这条线解决了我的问题:

index           index.php;

全部的事情:

server {

    server_name example.dev;
    root /var/www/example/public;
    index           index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

事实上,有几件事你需要检查。 1. 检查nginx的运行状态

ps -ef|grep nginx

ps aux|grep nginx|grep -v grep

这里我们需要检查谁在运行nginx。请记住用户和组

检查文件夹的访问状态 ls alt 与nginx的文件夹状态进行比较

(1)如果文件夹的访问状态不正确

sudo chmod 755 /your_folder_path

(2)如果文件夹的用户和组与nginx运行时的用户和组不一致

sudo chown your_user_name:your_group_name /your_folder_path

修改nginx的运行用户名和组

nginx -h

查找nginx配置文件的位置

sudo vi /your_nginx_configuration_file

//in the file change its user and group
user your_user_name your_group_name;

//restart your nginx
sudo nginx -s reload

因为nginx默认运行的用户是nobody,组也是nobody。如果我们没有注意到这个用户和组,就会引入403。