按照这个答案,您可以使用此nginx配置文件将服务器内部的Dropwizard应用程序从端口8080代理到端口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应用程序,您可以从Dropwizard提供静态资产,也可以通过Nginx设置虚拟主机
作为旁注,请记住从 Dropwizard 应用程序在 mainClass 中配置 CORS:
@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");
}