我正在尝试从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
。有关详细信息,请参阅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
,使用alias
和this issue是有问题的。有关使用this caution的信息,请参阅if
。