从 nginx /location 块(虚拟主机)服务 Laravel 应用程序

Serve Laravel application from nginx /location block (virtual host)

我正在尝试从 Ubuntu nginx 虚拟主机配置中的 /location 块提供服务 laravel。我安装了 laravel 应用程序并在直接访问时工作正常,但 nginx 位置块似乎没有按预期执行。

这个有效:https://www.madol.example.com/horizontal-laravel/public/index.php

这不是 (403):https://www.madol.example.com/horizontal-laravel/

注:真实地址省略

可能有误的主要片段:

root /var/www/madol.example.com;
server_name madol.example.com www.madol.example.com;
location /horizontal-laravel {
    try_files $uri $uri/ /horizontal-laravel/public/index.php;
}

这是我的配置文件中的完整代码-

server {

     root /var/www/madol.example.com;
     index index.php index.html;

     server_name madol.example.com www.madol.example.com;

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

     location ~ \.php$ {
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
            include snippets/fastcgi-php.conf;
     }

     **# Something wrong here?**
     location /horizontal-laravel {
           try_files $uri $uri/ /horizontal-laravel/public/index.php;
     }

     location ~*  \.(jpg|jpeg|png|gif|svg|ico|css|js)$ {
            expires 7d;
}

     listen [::]:443 ssl; # managed by Certbot
     listen 443 ssl; # managed by Certbot
     ssl_certificate /etc/letsencrypt/live/madol.madcoderz.com/fullchain.pem; # managed by Certbot
     ssl_certificate_key /etc/letsencrypt/live/madol.madcoderz.com/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 {
     if ($host = www.madol.example.com) {
            return 301 https://$host$request_uri;
     } # managed by Certbot

     if ($host = madol.example.com) {
            return 301 https://$host$request_uri;
     } # managed by Certbot


     listen 80;
     listen [::]:80;
     server_name madol.example.com www.madol.example.com;
     return 404;
} # managed by Certbot

除此之外,代码中还有其他配置问题吗?

index 指令正在 /var/www/madol.example.com/horizontal-laravel/index.php 处查找文件,但未找到任何内容。

最简单的解决方案是展开 index 指令以在 public 文件夹中查找 index.php。有关详细信息,请参阅 this document

例如:

location /horizontal-laravel {
    index index.php public/index.php index.html;
    ...
}

或者,您可以通过从 try_files 指令中删除 $uri/ 文件项来停止对此 location 的索引处理。有关详细信息,请参阅 this document

例如:

location /horizontal-laravel {
    try_files $uri /horizontal-laravel/public/index.php;
    ...
}

您可以使用 rewrite...last 语句显式重定向这个 URI。有关详细信息,请参阅 this document

location = /horizontal-laravel/ { 
    rewrite ^ /horizontal-laravel/public/index.php last; 
}
location /horizontal-laravel {
    ...
}

最后,您可以重新设计 URI 方案以消除暴露的 /public/ 位。这可能最好使用 alias 指令来实现。您将需要一个嵌套位置来在新根目录下执行 PHP 脚本。

例如:

location ^~ /horizontal-laravel {
    alias /var/www/madol.example.com/horizontal-laravel/public;
    if (!-e $request_filename) { rewrite ^ /horizontal-laravel/public/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

注意 if 指令和 SCRIPT_FILENAME 使用 $request_filename 获取本地文件的路径。您将需要检查里面的内容 snippets/fastcgi-php.conf 以确保它不会损坏任何东西。

使用 try_filesalias 是有问题的,因为 this issue. See this caution 使用 if