我正在尝试配置 nginx,以便可以在/之外运行主网站,然后在/myapp 之外运行新的 Laravel 站点。我已经摆脱了应用程序本身(其中的公共(public)文件夹部分)对/myapp/public 的需要,所以我需要的只是正确提供/myapp 文件夹。现在我可以正确访问主页/,但是/myapp 上的新 Laravel 应用程序会抛出 404:nginx/1.4.6 (Ubuntu)。我在下面发布了我的 nginx 配置。如果有帮助的话,它正在 AWS 上的 Ubuntu 上运行。
谢谢!
[更新!] - 我现在已经在根目录和/myapp Laravel 应用程序文件夹中一切正常,除了资源似乎不起作用。例如http://example.com/myapp/public/css/app.css 404,其他 js 和 css 文件也是如此。不确定是什么原因造成的。我已确保公用文件夹具有完全权限,至少出于测试目的。还检查了所有者/组,这与我所有其他运行良好的文件相同,ubuntu:ubuntu。
server {
listen 80;
server_name *.example.com;
set $root_path '/usr/share/nginx/html/';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ @rewrite;
location @rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index /index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
allow all;
}
rewrite ^/myapp/?(.*)$ /myapp/index.php?$1 last;
location /myapp {
allow all;
}
location ~ /\.ht {
deny all;
}
}
最佳答案
经过几天的修补,我终于解决了这个问题。现在它可以正确加载主站点、子 Laravel 站点以及所有资源。这是最终的配置,供引用。
server {
listen 80;
server_name *.example.com;
set $root_path '/usr/share/nginx/html/';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ @rewrite;
location @rewrite {
rewrite ^/myapp/?(.*)$ /index.php?$1;
}
location ~ \.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index /index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
allow all;
}
location ~* ^/myapp/?(.+)/(css|img|js|flv|swf|download)/(.+)$ {
allow all;
}
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location /myapp {
allow all;
rewrite ^/myapp/?(.*)$ /myapp/index.php?$1 last;
}
location ~ /\.ht {
deny all;
}
}
关于php - Laravel 位于 nginx 的子文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32554089/