首页 > 解决方案 > 为什么使用 nginx 部署 laravel 项目时会出现 403 禁止?

问题描述

我尝试了来自 SO 和其他在线网站的许多答案,但我似乎无法让它发挥作用。

使用 nginx 配置部署我的 laravel 项目时出现 403 禁止错误:

server {
        listen 80;
        server_name xxxxx.co www.xxxxx.co;
        return 301 https://xxxxx.co$request_uri;
}

server {
        listen 80;
        listen [::]:80;

        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/www.xxxxx.co/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/www.xxxxx.co/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

        root /var/www/www.xxxxx.co/public;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name  xxxxx.co www.xxxxx.co;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php?$query_string;
        }

}

/var/www/www.xxxxx.co/public如果重要,此内容在哪里:

admin favicon.ico home index.php web.config

在本地机器上,它工作正常,我看到主页,但使用 nginx 我无法让它工作

编辑 1:这是/var/www/www.xxxxx.co 的权限

drwxr-xr-x 13 root root 4096 Oct 10 07:48 .

以下是来自/var/log/nginx/domains/main.xxxxx.co.error.log的日志



2020/10/10 08:19:39 [error] 19736#19736: *9 directory index of "/var/www/www.xxxxx.co/public/home/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET /home/ HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:20:31 [error] 19736#19736: *9 open() "/var/www/www.xxxxx.co/public/login" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET /login HTTP/1.1", host: "xxxxx.co"                    2020/10/10 08:20:35 [error] 19736#19736: *9 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:22:03 [error] 19776#19776: *1 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"                                           2020/10/10 08:22:04 [error] 19776#19776: *1 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:24:03 [error] 19825#19825: *1 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"                                           2020/10/10 08:25:31 [error] 19857#19857: *1 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:25:36 [error] 19857#19857: *3 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"                                           2020/10/10 08:26:24 [error] 19873#19873: *1 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:29:14 [error] 19908#19908: *1 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"                                           2020/10/10 08:31:44 [error] 19937#19937: *1 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:31:48 [error] 19937#19937: *1 directory index of "/var/www/www.xxxxx.co/public/home/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET /home/ HTTP/1.1", host: "xxxxx.co"                                 2020/10/10 08:34:21 [error] 19968#19968: *1 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:36:24 [error] 19968#19968: *3 directory index of "/var/www/www.xxxxx.co/public/" is forbidden, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"                                           2020/10/10 08:38:46 [error] 20024#20024: *2 open() "/var/www/www.xxxxx.co/publicindex.php" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:38:50 [error] 20024#20024: *2 open() "/var/www/www.xxxxx.co/publicindex.php" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET /home/ HTTP/1.1", host: "xxxxx.co"                 2020/10/10 08:39:43 [error] 20024#20024: *2 open() "/var/www/www.xxxxx.co/publicindex.php" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:40:23 [error] 20024#20024: *2 open() "/var/www/www.xxxxx.co/publicindex.php" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET /index HTTP/1.1", host: "xxxxx.co"                 2020/10/10 08:40:26 [error] 20024#20024: *2 open() "/var/www/www.xxxxx.co/publicindex.php" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET /home/ HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:53:32 [error] 20220#20220: *36 open() "/var/www/www.xxxxx.co/publicindex.php" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "xxxxx.co"                     2020/10/10 08:53:46 [error] 20220#20220: *36 open() "/var/www/www.xxxxx.co/publicindex.php" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET /index HTTP/1.1", host: "xxxxx.co"
2020/10/10 08:53:50 [error] 20220#20220: *41 open() "/var/www/www.xxxxx.co/publicindex.php" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET /index HTTP/1.1", host: "www.xxxxx.co"            2020/10/10 08:54:19 [error] 20220#20220: *42 open() "/var/www/www.xxxxx.co/publicindex.php" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET /index HTTP/1.1", host: "www.xxxxx.co"
2020/10/10 08:54:22 [error] 20244#20244: *1 open() "/var/www/www.xxxxx.co/publicindex.php" failed (2: No such file or directory), client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", host: "www.xxxxx.co"                  2020/10/10 09:03:03 [crit] 20322#20322: *2 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", upstrea>2020/10/10 09:05:47 [crit] 20394#20394: *2 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", upstrea>2020/10/10 09:05:48 [crit] 20394#20394: *2 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", upstrea>2020/10/10 13:30:47 [crit] 20394#20394: *224 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.66.201, server: www.xxxxx.co, request: "GET /ads.txt HTTP/1.1">2020/10/10 13:54:08 [crit] 20394#20394: *257 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 195.154.61.206, server: www.xxxxx.co, request: "GET / HTTP/1.1", upst>2020/10/10 13:57:53 [crit] 20394#20394: *265 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 195.154.61.206, server: www.xxxxx.co, request: "GET / HTTP/1.1", upst>2020/10/10 15:15:46 [crit] 20394#20394: *350 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.66.203, server: www.xxxxx.co, request: "GET /ads.txt HTTP/1.1">2020/10/10 15:38:08 [crit] 20394#20394: *385 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 62.210.10.77, server: www.xxxxx.co, request: "GET / HTTP/1.1", upstre>2020/10/10 15:38:28 [crit] 20394#20394: *387 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 195.154.63.222, server: www.xxxxx.co, request: "GET / HTTP/1.1", upst>2020/10/10 17:52:32 [crit] 20394#20394: *629 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.66.94, server: www.xxxxx.co, request: "GET / HTTP/1.1", upstre>2020/10/10 20:00:52 [crit] 20394#20394: *848 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.66.201, server: www.xxxxx.co, request: "GET / HTTP/1.1", upstr>2020/10/10 21:00:50 [crit] 20394#20394: *900 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.66.95, server: www.xxxxx.co, request: "GET / HTTP/1.1", upstre>2020/10/10 22:00:32 [crit] 20394#20394: *988 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.66.205, server: www.xxxxx.co, request: "GET / HTTP/1.1", upstr>2020/10/10 23:22:15 [crit] 20394#20394: *1098 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 182.66.43.161, server: www.xxxxx.co, request: "GET / HTTP/1.1", upst>2020/10/11 00:01:52 [crit] 20394#20394: *1165 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.66.203, server: www.xxxxx.co, request: "GET / HTTP/1.1", upst>2020/10/11 04:40:28 [crit] 20394#20394: *1609 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 77.30.211.212, server: www.xxxxx.co, request: "GET / HTTP/1.1", upst>2020/10/11 05:32:53 [crit] 20394#20394: *1685 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.66.205, server: www.xxxxx.co, request: "GET / HTTP/1.1", upst>2020/10/11 08:56:03 [crit] 20394#20394: *1948 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.66.205, server: www.xxxxx.co, request: "GET /en/desert-one HT>2020/10/11 09:30:17 [crit] 20394#20394: *1994 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 167.172.56.36, server: www.xxxxx.co, request: "GET /wp-login.php HTT>2020/10/11 11:43:53 [crit] 20394#20394: *2149 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 18.162.245.185, server: www.xxxxx.co, request: "GET /wp-login.php HT>2020/10/11 13:16:09 [crit] 20394#20394: *2324 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.66.201, server: www.xxxxx.co, request: "GET /ads.txt HTTP/1.1>2020/10/11 13:41:01 [crit] 20394#20394: *2343 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 94.49.72.36, server: www.xxxxx.co, request: "GET / HTTP/1.1", upstre>2020/10/11 13:50:54 [crit] 28196#28196: *1 connect() to unix:/var/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 94.49.72.36, server: www.xxxxx.co, request: "GET / HTTP/1.1", upstream:>                                                                                                                                                                                                                  

标签: phplinuxlaravelnginx

解决方案


终于修好了。@Ivan Shatsky 的评论让我搜索 PHP-FPM。我发现它很重要,并且大多数在线示例都没有将它包含在配置中。我通过以下方式安装它:

sudo apt-get update
sudo apt-get install php7.4-cli php7.4-curl php7.4-mysql php7.4-fpm php7.4-gd php7.4-xml php7.4-mbstring php7.4-zip php7.4-soap php7.4-dev -y

我遇到了其他问题,但最终让它与这个配置一起工作:

server {
        listen 80;
        server_name www.xxxxx.co xxxxx.co;
        return 301 https://www.xxxxx.co$request_uri;
}

server {
    listen 80;
    listen [::]:80;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.xxxxx.co/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.xxxxx.co/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    server_name  www.xxxxx.co xxxxx.co;
    root /var/www/www.xxxxx.co/public;
    index index.php index.html index.htm;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    # pass the PHP scripts to FastCGI server listening on /var/run/php7.4-fpm.sock
    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

推荐阅读