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

有什么问题吗?


当前回答

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

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;
    }
}

其他回答

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

更改try_files以指向index.php路径,在你提到的“Laravel”中,它应该是这样的

location / {
    try_files $uri $uri/ /public/index.php$request_uri;
}

在“codeigniter”项目中试试这样做

location / {
    try_files $uri $uri/ /public_web/index.php$request_uri;
}

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

在我的例子中,我没有运行这个命令

sudo apt-get install php7.4-fpm

在我的情况下,我使用hhvm监听端口9000和nginx配置中的fastcgi_pass行是不正确的。

此外,如果你正在使用mysql,从hhvm到数据库的连接不工作,检查是否安装了apparmor。