簡體   English   中英

Angular + nginx 重定向未發生

[英]Angular + nginx redirection not happening

我是 Angular 和 nginx 的新手。 我有一個 Angular 應用程序在 http:/localhost:4200/abc 上運行,它與在我的本地端口 3001 中運行的 rails 應用程序交互 - Z80791B3AE7002CB88C246876D9FAA8F 我正在嘗試使用 nginx 來重定向請求。 我想在自定義 URL local-admin.com 中訪問 angular。 所以我創建了一個 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 總是空的?

您的 local-admin.com 需要有一個 DNS 條目。 如果它是您的本地計算機,請在 etc/hosts 文件中添加 dns 映射,否則只需使用 server_name 作為 127.0.0.1 並使用http://127.0.0.1訪問

Your angular app should be hosted as static content (Assuming your angular build is in /opt/abc folder ), Following nginx config can be used

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM