我有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禁止所有文件,但这没有帮助。

有什么问题吗?


当前回答

location ~* \.php$ {
    ...
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    
}

改变默认的

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

to

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

解决了我的问题。

其他回答

你可能会得到这个,因为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;
    }

如果你只是想列出目录内容,使用autoindex on;如:

location /somedir {
       autoindex on;
}

server {
        listen   80;
        server_name  example.com www.example.com;
        access_log  /var/...........................;
        root   /path/to/root;
        location / {
                index  index.php index.html index.htm;
        }
        location /somedir {
               autoindex on;
        }
}

我也遇到过类似的错误 ——网页中的“403禁止” ——错误日志/var/log/nginx/error.log中的"13:Permission denied

以下3个步骤对我很有效:

1:开放式终端,看到如下内容

user1@comp1:/home/www/

因此,我的用户名是“user1”(从上面)

2:修改/etc/nginx/nginx.conf中的user

# user www-data;
user user1;

3:重新加载nginx

sudo nginx -s reload  

此外,我已经应用了文件/文件夹权限(在我做以上3步之前) (755到我的目录,说/dir1/) &(644下的文件目录): (我不确定,如果真的需要这个额外的步骤,3步以上可能就足够了):

chmod 755 ./dir1/
chmod 644 ./dir1/*.*

希望这能帮助到一些人。祝你好运。

我解决了我的问题,如果我配置如下:

location = /login {
    index  login2.html;
}

它会显示403错误。

[error] 4212#2916: *2 directory index of "D:\path/to/login/" is forbidden

我试过自动索引,但不工作。 如果我改变我的配置像这样,它工作。

location = /login/ {
    index  login2.html;
}

我认为精确匹配,如果是路径,就应该是目录。

location ~* \.php$ {
    ...
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    
}

改变默认的

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

to

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

解决了我的问题。