首页 > 解决方案 > 从 nginx /location 块(虚拟主机)服务 Laravel 应用程序

问题描述

我正在尝试从 Ubuntu nginx 虚拟主机配置中的 /location 块提供 laravel。我已经安装了 laravel 应用程序并且在直接访问时工作正常,但是 nginx 位置块似乎没有做它预期做的事情。

这有效:https ://www.madol.example.com/horizo​​ntal-laravel/public/index.php

这不(403):https ://www.madol.example.com/horizo​​ntal-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

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

标签: laravelnginxlaravel-5ubuntu-16.04nginx-location

解决方案


index指令正在寻找一个文件,/var/www/madol.example.com/horizontal-laravel/index.php但没有找到任何东西。

最简单的解决方案是扩展index指令以index.phppublic文件夹中查找。有关详细信息,请参阅此文档

例如:

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

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

例如:

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

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

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_fileswith是有问题的。请参阅此使用注意事项。aliasif


推荐阅读