【问题标题】:Angular - Nginx rewrites domain/foo to angular root but not domain/foo/barAngular - Nginx 将 domain/foo 重写为 angular root 但不是 domain/foo/bar
【发布时间】:2015-02-01 14:04:47
【问题描述】:

尝试使用带有 Laravel 后端的 Angular JS 从 Apache 迁移到 Nginx。

前后端完全独立,如var/www/angularvar/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;
}

【问题讨论】:

标签: angularjs nginx


【解决方案1】:

原来问题的根源在于 grunt 没有在样式声明之前应用基础 /,即使我有

<base href="/">

设置在我的 index.html 的头部

解决方案很简单...只需将基本标签移动到头部的顶部而不是底部!

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多