java - 如何将 nginx 配置为 jetty 的代理?

标签 java nginx jetty ring

我一直在尝试将 nginx 设置为 jetty 的代理。我想按照 this answer 中的解释做一些事情但 Jetty 不响。

我创建了一个 .war 并将它放在 ~/jetty/jetty-dist/webapps/web_test-0.1.0-SNAPSHOT-standalone.war

例如,我想使用 IP 地址为 198.51.100.0 的域 example.com。

我还将 /etc/nginx/sites-available/default 复制到文件 example.com 中,并将其放在同一目录中。

在我的案例中,你能帮我配置 nginx 作为 jetty 的代理吗?我知道网上有很多关于如何执行此操作的引用资料,但它们各不相同,我感到很困惑。

我需要在 nginx 中做哪些具体的更改?我需要在 jetty.xml 中做哪些更改?我是否需要进行任何其他更改?我的应用程序会在 example.com/index.html 上提供吗?

nginx 的当前状态复制如下:

upstream jetty {
  server 127.0.0.1:8080 fail_timeout=0
}

server {
        listen 80 default_server;
        #listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        server_name localhost;

        location / {
                proxy_pass http://jetty

                try_files $uri $uri/ =404;
        }

编辑

我想知道我是否需要使用 Jetty。在 this setup他只是用戒指,这看起来 super 简单?使用 jetty 有什么好处?


最佳答案

如何配置 nginx 以与 java 服务器一起工作。在示例中使用 Jetty。

编辑/etc/nginx/sites-available/hostname:

server {
  listen       80;
  server_name  hostname.com;

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

考虑禁用对端口 8080 的外部访问:

/sbin/iptables -A INPUT -p tcp -i eth0 --dport 8080 -j REJECT --reject-with tcp-reset

Jetty 配置示例 (jetty.xml) 可能类似于:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!--
 | http://eclipse.org/jetty/documentation/current/configuring-connectors.html
 +-->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
    <Set name="secureScheme">https</Set>
    <Set name="securePort"><Property name="jetty.tls.port" default="8443" /></Set>
    <Set name="outputBufferSize">65536</Set>
    <Set name="requestHeaderSize">8192</Set>
    <Set name="responseHeaderSize">8192</Set>
  </New>
  <Call name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        <Arg name="server"><Ref refid="Server" /></Arg>
        <Arg name="acceptors" type="int"><Property name="http.acceptors" default="-1"/></Arg>
        <Arg name="selectors" type="int"><Property name="http.selectors" default="-1"/></Arg>
        <Arg name="factories">
          <Array type="org.eclipse.jetty.server.ConnectionFactory">
            <Item>
              <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                <Arg name="config"><Ref refid="httpConfig" /></Arg>
              </New>
            </Item>
          </Array>
        </Arg>
        <Set name="host"><Property name="jetty.host" default="localhost" /></Set>
        <Set name="port"><Property name="jetty.port" default="8080" /></Set>
      </New>
    </Arg>
  </Call>
</Configure>

这将导致 Jetty 监听 localhost:8080 并且 nginx 将请求从 domain.com:80 重定向到 Jetty 服务器。

关于java - 如何将 nginx 配置为 jetty 的代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20730858/

相关文章:

nginx - 如何在 CDN 后面创建反向代理(另一个反向代理)

ruby-on-rails - 使用 Dokku + nginx + unicorn 的未初始化常量 ActiveRecord (NameError)

java - 如何将子域路由到集群内的一个或多个合适的节点?

java - websocket.send 期间收到 "InvalidStateError: DOM Exception 11"

java - 跨越数据库中多个模式的事务

java - 二维数组对象遇到问题

http - 使用 header 过滤代理响应 header

maven - 为什么 maven 在错误的 repo 中寻找 Artifact ?

java - 为什么 Eclipse 不会@Override?

java - 访问对象的对象变量?