【发布时间】:2019-03-29 20:18:07
【问题描述】:
我想在单个 nginx 服务器中服务多个 Laravel 应用程序,第一个在 /var/www/html/app1 中有一个根目录,第二个在 /var/www/html/app2 中有一个根目录,依此类推。每个应用程序的index.php 文件位于名为/public 的子目录中。
当用户调用http://www.mywebsite.com/app1时,nginx应该返回app1,如果用户调用http://www.mywebsite.com/app2,nginx应该返回app2。
我当前的nginxconf文件如下:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location /app1 {
root /var/www/html/app1/public;
index index.php;
}
location /app2 {
root /var/www/html/app2/public;
index index.php;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
但是,nginx 总是返回 404 页面结果。这里出了什么问题?
【问题讨论】:
-
你的配置出了什么问题,如果你浏览 /app1 它会被重定向到 /var/www/html/app1/public/app1/index.php 并且找不到这个位置并抛出一个404.此外,在每个位置块中指定索引指令是错误的并且不是必需的。 server 块中定义的 index 指令就足够了。
-
您需要使用
alias而不是root,并且您需要在每个.phpURI 中使用嵌套的location块。见this answer。
标签: linux laravel nginx server