【问题标题】:Angular + nginx redirection not happeningAngular + nginx 重定向没有发生
【发布时间】:2021-06-26 04:54:02
【问题描述】:

我是 Angular 和 nginx 的新手。我有一个在 http://localhost:4200/abc 运行的 Angular 应用程序,它与在我的本地端口 3001 - http://localhost:3001 中运行的 rails 应用程序交互。我正在尝试使用 nginx 来重定向请求。我想访问自定义 URL local-admin.com 中的角度。所以我创建了一个 nginx conf 文件

server {
    listen       80;
    server_name local-admin.com;

    #charset koi8-r;

    # access_log  ~/logs/host.access.log  main;

    location /users/sign_in{
        proxy_pass   "http://127.0.0.1:4200/abc/users/sign_in";
    }

    location /abc/api/ {
        proxy_pass  http://localhost:3001/api/;
        proxy_set_header Host $host;
    }
}

所以当我点击 http://local-admin.com/users/sign_in 时,我看到 无法访问此站点 错误,并且我的 ngingx access.log 和 error.log 为空。

我还尝试访问实际的url http://localhost:4200/abc/users/sign_in,它调用了一个API

private endpoints = {
  checkLogin: '/api/v1/is_logged_in'
}; 

isLoggedIn(): Observable<boolean> {
  return this._http.get<{is_logged_in: boolean}>(this.endpoints.checkLogin)
    .pipe(map(response => response.is_logged_in));
}

期望重定向到 http:localhost:3001/api/v1/is_logged_in 但重定向没有发生并且网络调用仍然显示 http://localhost:4200/abc/api/v1/is_logged_in。

我是否缺少任何步骤?为什么我的 nginx access.log 和 error.log 总是空的?

【问题讨论】:

    标签: angular nginx


    【解决方案1】:

    您的 local-admin.com 需要有一个 DNS 条目。如果它是您的本地计算机,请在 etc/hosts 文件中添加 dns 映射,否则只需使用 server_name 作为 127.0.0.1 并使用 http://127.0.0.1 访问

    您的 Angular 应用应作为静态内容托管(假设您的 Angular 构建位于 /opt/abc 文件夹中),可以使用以下 nginx 配置

    location /abc {
    autoindex on;
    root /opt;
    }
    

    【讨论】:

    • 我只需要在 /etc/hosts 中添加 DNS 条目并重新启动 httpd。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 2021-12-06
    • 2018-07-02
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2019-10-23
    相关资源
    最近更新 更多