【问题标题】:How do I configure nginx as proxy to jetty?如何将 nginx 配置为码头的代理?
【发布时间】:2013-12-22 15:19:21
【问题描述】:

我一直在尝试将 nginx 设置为码头的代理。我想按照this answer 中的说明做一些事情,但 Jetty 不响。

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

说,我想使用域 example.com,IP 地址为 198.51.100.0。

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

在我的情况下,你能帮我将 nginx 配置为码头的代理吗?我知道网上有很多关于如何做到这一点的参考资料,但它们都不一样,我很困惑。

我需要在 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 中,他只使用了环,看起来超级简单?使用 jetty 有什么好处?


【问题讨论】:

    标签: java nginx jetty ring


    【解决方案1】:

    如何配置 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 服务器。

    【讨论】:

    • 在 Jetty 6 中,如果您有 ${jetty.home}/contexts/myapp.xml 使用 Jetty 9.0,请将其移动到 ${jetty.home}/webapps/myapp.xml 使用 Jetty 9.1+ ,将其移至 ${jetty.base}/webapps/myapp.xml
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2012-10-14
    相关资源
    最近更新 更多