【问题标题】:Issue in proxy pass from nginx to angular application从 nginx 到 Angular 应用程序的代理传递问题
【发布时间】:2021-07-27 12:50:40
【问题描述】:

我创建了一个 Angular 应用程序,它在 /hello 路由上显示带有“Hello World”的页面。

角度网址:http://localhost:4200/hello

我想在/greet => http://localhost/greet 上从浏览器向 Nginx 发出请求 并且应该重定向到 Angular 应用程序的/hello 路由,以便显示带有“Hello World”的页面。

但如果请求是http://localhost/hello,则应该出现错误

nginx 配置文件:

server {
    listen 80;
    server_name _;
    location /greet {
    proxy_pass http://localhost:4200/hello;
     }
    location /hello {
      return 404;
     }
}

问题是,在重定向时,路由没有被修改为/hello,并且由于角度中没有/greet 路由,我得到一个找不到。

【问题讨论】:

    标签: nginx


    【解决方案1】:

    如果请求是http://localhost/hello,则会出现错误

    location /hello {          
      error_page 404 /error404.html; #Add this snippted
    } 
    
    # Will give you the desire error 404 page.
    

    对于这个 http://localhost/greet => http://localhost/hello

    location /greet {
      proxy_pass 'http://127.0.0.1:4200/hello';
    }
    

    应该可以!

    注意:如果没有,请在您的应用服务器中添加 request_uri 并检查您缺少的内容。但是,Nginx 配置文件没有问题。

    【讨论】:

    • 没关系..但我的首要任务是将 localhost/greet 代理传递给 localhost:4200/hello 它。如果我输入 localhost/greet 它给了我页面未找到页面不应该发生它应该显示角度 hello 页面
    • 编辑我的回答。看看检查你的应用服务器(Angular App)路由。
    • const routes: Routes = [ {path: 'hello', component : HelloComponent}, { path: 'home', component : HomeComponent}, { path: '', redirectTo: '/home' , pathMatch: 'full' }, { path: '**', component: PagenotfoundComponent } ];这是我的路线配置我应该在这里改变什么
    • 我不是Angular 方面的专家,但路线存在问题。尝试删除{ path: '**', component: PagenotfoundComponent }
    • @Rashmi Pawar 解决方案成功了吗?如果有帮助,请点击答案附近的tick mark 接受问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2015-03-09
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    相关资源
    最近更新 更多