从nginx / location block(虚拟主机)提供Laravel应用程序

问题描述 投票:2回答:1

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

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

laravel nginx laravel-5 ubuntu-16.04 nginx-location
1个回答
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内部的内容,以确保它不会破坏任何内容。

由于try_files,使用aliasthis issue是有问题的。有关使用this caution的信息,请参阅if

© www.soinside.com 2019 - 2024. All rights reserved.