如何使用Nginx和dropwizard部署angularjs应用程序前端 [英] How to deploy an angularjs application frontend with Nginx and dropwizard

查看:199
本文介绍了如何使用Nginx和dropwizard部署angularjs应用程序前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用angularjs应用程序前端作为后端dropwizard的应用程序。我打算使用Nginx作为后端dropwizard服务器的网关,并作为资产服务器(图像,也许是angularjs应用程序)。

I'm developing an application using angularjs application frontend having as backend dropwizard. I'm planning to use Nginx as gateway for the backend dropwizard server and as an asset server (images and maybe the angularjs application).

我的问题是什么是最好的部署策略:

My question is what is the best strategy for deployement:


  1. 使用dropwizard后端捆绑angularjs并使用nginx作为前端?

  2. 部署nginx服务器上的angularjs应用程序?

提前致谢,

推荐答案

answer-30451568>回答您可以使用此nginx配置文件将服务器内的Dropwizard应用程序从端口8080代理到端口80:

Following this answer You can use this nginx configuration file in order to proxy the Dropwizard application inside your server from port 8080 to port 80:

server {
listen 80;

server_name api.example.com;

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header  Host             $http_host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

对于Angular应用程序,您可以提供静态资产或设置通过Nginx的虚拟主机

For your Angular application you can either serve static assets from Dropwizard or set a virtual host via Nginx

作为旁注,请记住从您的Dropwizard应用程序在mainClass中配置CORS:

As a side note, remember to configure CORS in your mainClass from your Dropwizard application:

  @Override
  public void run(Configuration configuration, Environment environment) throws Exception {
    configureCors(environment);
    environment.jersey().register(new HelloWorldResource(template));
  }

  private void configureCors(Environment environment) {
    FilterRegistration.Dynamic filter = environment.servlets().addFilter("CORS", CrossOriginFilter.class);
    filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
    filter.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,PUT,POST,DELETE,OPTIONS");
    filter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
    filter.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
    filter.setInitParameter("allowedHeaders", "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");
    filter.setInitParameter("allowCredentials", "true");
  }

这篇关于如何使用Nginx和dropwizard部署angularjs应用程序前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆