首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在nginx上托管静态angular2应用程序,并将http请求代理到后端api

在nginx上托管静态angular2应用程序,并将http请求代理到后端api
EN

Stack Overflow用户
提问于 2016-10-06 16:17:02
回答 1查看 9.8K关注 0票数 3

如何使angular2路由工作,并在另一台机器上向rest提供代理http请求?

我在nginx服务器上有一个angular2 web应用程序,它为静态html文件提供服务。我有一个独立的rest托管在不同的机器上,具有不同的IP地址。我已经将nginx配置文件中的位置设置为/,以允许angular2路由正确工作。我还添加了一个location / api /,我希望它能够拦截任何api请求并将它们代理到我的后端api。

为了测试目的,我的nginx将代理设置为http://swapi.co

代码语言:javascript
复制
events {
  worker_connections  4096;  ## Default: 1024
}

http {
  server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;
    include /etc/nginx/mime.types;

    location / {
         # If you want to enable html5Mode(true) in your angularjs app for pretty URL
         # then all request for your angularJS app will be through index.html
         try_files $uri /index.html;
    }

    # /api will server your proxied API that is running on same machine different port
    # or another machine. So you can protect your API endpoint not get hit by public directly
    location /api/ {
        proxy_pass http://swapi.co;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    #Static File Caching. All static files with the following extension will be cached for 1 day
    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 1d;
    }
  } 
}

我的angular2服务

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class Service {

  private url: string = '/api/people/';
  constructor (private http: Http) {}

  getPeople () {
    return this.http.get(this.url)
                  .map(res => res.json());
  }
}

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-11 12:46:02

在我的开发环境中正确地获得了代理请求。添加到http块和指向代理传递到上游api值。

代码语言:javascript
复制
upstream api {
  server 192.168.0.15:9998;
}

完整配置:

代码语言:javascript
复制
events {
  worker_connections  4096;  ## Default: 1024
}

http {

  # Change this depending on environment
  upstream api {
    server 192.168.0.1:9998;
  }

  server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;
    include /etc/nginx/mime.types;

    location / {
      # If you want to enable html5Mode(true) in your angularjs app for pretty URL
      # then all request for your angularJS app will be through index.html
      try_files $uri /index.html;
    }

    # /api will server your proxied API that is running on same machine different port
    # or another machine. So you can protect your API endpoint not get hit by public directly
    location /api {
      proxy_pass http://api;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    #Static File Caching. All static files with the following extension will be cached for 1 day
    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
      expires 1d;
    }
  }
}
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39900998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档