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

有什么问题吗?


当前回答

以下是我如何在我的Kali机器上修复它:

定位到目录: cd /etc/nginx/sites-enabled / 编辑'default'配置文件: Sudo nano默认 在location块中添加以下行: 位置/你的目录{ 自动变址; autoindex_exact_size; } 注意,我在一个特定的目录中激活了自动索引 / yourdirectory只。否则,你电脑上的所有文件夹都将启用它,而你不想要它。 现在重新启动你的服务器,它现在应该工作了: Sudo服务nginx重启

其他回答

下面是工作的配置:

server {
    server_name www.mysite2.name;
    return 301 $scheme://mysite2.name$request_uri;
}
server {
    #This config is based on https://github.com/daylerees/laravel-website-configs/blob/6db24701073dbe34d2d58fea3a3c6b3c0cd5685b/nginx.conf
    server_name mysite2.name;

     # The location of our project's public directory.
    root /usr/share/nginx/mysite2/live/public/;

     # Point index to the Laravel front controller.
    index           index.php;

    location / {
        # URLs to attempt, including pretty ones.
        try_files   $uri $uri/ /index.php?$query_string;
    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
            rewrite     ^/(.+)/$ /$1 permanent;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #   # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

然后浏览器中唯一的输出是一个Laravel错误:“哎呀,看起来好像出了什么问题。”

不要执行chmod -R 777 app/storage(注)。让某些东西在全世界都可写是很糟糕的安全性。

chmod -R 755 app/storage工作,更安全。

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

当你想保留目录选项时,你可以像这样把index.php放在$uri前面。

try_files /index.php $uri $uri/

我也遇到过类似的错误 ——网页中的“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/*.*

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

事实上,有几件事你需要检查。 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。