【发布时间】:2015-02-01 14:04:47
【问题描述】:
尝试使用带有 Laravel 后端的 Angular JS 从 Apache 迁移到 Nginx。
前后端完全独立,如var/www/angular和var/www/laravel。它们来自同一个域 - mysite.com(角度)和 laravel API 路由通过 mysite.com/api
我已经完成了所有工作,包括 html5mode 和前端路由的浏览器刷新,例如 mysite.com/foo
问题是,刷新或手动输入 mysite.com/foo/bar 不起作用。相反,html 是在没有 css 的情况下提供的,这意味着它没有被路由到 angular。
非常感谢任何建议。
这是我当前的 Nginx 虚拟主机配置:
server {
listen 80 default_server;
server_name default;
root /home/forge/default/laravel/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
location / {
root /home/forge/default/angular/dist;
try_files $uri $uri/ /index.html;
}
location /lvl/ {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \/lvl\/index\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
include fastcgi.conf;
#This specifically sets so the /api/ portion
#of the URL is not included
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_param ENV production;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/default-error.log error;
error_page 404 /index.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
编辑 1: 根据 raam86 的评论,我尝试了他向我推荐的重写版本,结果完全相同,但问题仍然存在。以下是我所做的修改:
location / {
root /home/forge/default/angular/dist;
try_files = $uri @missing;
}
location @missing {
rewrite ^/$ /index.html last;
}
【问题讨论】:
-
我已经尝试了一个非常相似的答案,并引用了 @rewrite 规则,但没有运气。不确定重写规则是否正确...