laravel - 从 nginx /location 块(虚拟主机)服务 Laravel 应用程序
问题描述
我正在尝试从 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
指令以index.php
在public
文件夹中查找。有关详细信息,请参阅此文档。
例如:
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
,以确保它不会破坏任何东西。
推荐阅读
- tensorflow - 序列数据上的 LSTM,预测离散列
- django - 为普通用户复制 django admin
- matlab - Matlab中神经网络训练中的切换类
- symfony - 奏鸣曲管理员列表可编辑与编辑/显示冲突
- python - 在 Django 中根据请求自动添加新的 model_field || DRF
- android - 插入数据后,房间数据库表仍然为空
- microsoft-graph-api - 应用 $filter 选项在 Graph API 中检索具有特定名称的文件夹
- android - Android 找不到微调器的符号变量错误
- kubernetes - Kubernetes 节点详细信息
- python - 如何在跨平台的控制台中显示等宽字体(或恒定距离)?