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

时间:2019-02-17 15:42:28

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

我正在尝试从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

代码中是否还有其他配置问题?

1 个答案:

答案 0 :(得分:1)

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

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

例如:

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

或者,您可以通过从location指令中删除$uri/文件术语来停止对此try_files的索引处理。有关详细信息,请参见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内的内容,以确保它不会破坏任何内容。

由于this issue,将try_filesalias一起使用会出现问题。关于if的使用,请参见this caution