如何让 sbt 的 Jetty 使用本地域名?

发布于 2024-10-19 11:24:58 字数 1614 浏览 6 评论 0原文

我想要 sbt> 推出的 Jetty 版本~jetty 监听 my.name.local,我已在 /etc/hosts 中将其设置为 127.0.0.1似乎可以更改 Jetty 的设置从 sbt 内部。

以下是我的项目:

import sbt._

class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
  // ...

  val jetty = "org.eclipse.jetty" % "jetty-webapp" % "7.3.0.v20110203" % "test"

  override lazy val jettyInstance = new JettyRunner(customJettyConfiguration) 

  def customJettyConfiguration = { 
    val myLog = log 
    val myJettyClasspath = jettyClasspath 
    val myScanDirectories = scanDirectories
    val myScanInterval = scanInterval

    new CustomJettyConfiguration { 
      def classpath = jettyRunClasspath
      def jettyClasspath = myJettyClasspath
      def war = jettyWebappPath
      def contextPath = jettyContextPath
      def classpathName = "test"
      def parentLoader = buildScalaInstance.loader
      def scanDirectories = Path.getFiles(myScanDirectories).toSeq
      def scanInterval = myScanInterval
      def port = jettyPort
      def log = myLog      
      override def jettyConfigurationXML = 
        <Configure class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="virtualHosts">
            <Array type="java.lang.String">
              <Item>my.name.local</Item>
            </Array>
          </Set>
        </Configure>
    }
  }
}

虽然它似乎启动时没有任何抱怨,但据我所知,访问 my.name.local 并没有到达 Jetty。

I'd like the version of Jetty launched by sbt> ~jetty to listen on my.name.local, which I've set to 127.0.0.1 in /etc/hosts. It seems to be possible to change Jetty's settings from within sbt.

Here's what I have for my project:

import sbt._

class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
  // ...

  val jetty = "org.eclipse.jetty" % "jetty-webapp" % "7.3.0.v20110203" % "test"

  override lazy val jettyInstance = new JettyRunner(customJettyConfiguration) 

  def customJettyConfiguration = { 
    val myLog = log 
    val myJettyClasspath = jettyClasspath 
    val myScanDirectories = scanDirectories
    val myScanInterval = scanInterval

    new CustomJettyConfiguration { 
      def classpath = jettyRunClasspath
      def jettyClasspath = myJettyClasspath
      def war = jettyWebappPath
      def contextPath = jettyContextPath
      def classpathName = "test"
      def parentLoader = buildScalaInstance.loader
      def scanDirectories = Path.getFiles(myScanDirectories).toSeq
      def scanInterval = myScanInterval
      def port = jettyPort
      def log = myLog      
      override def jettyConfigurationXML = 
        <Configure class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="virtualHosts">
            <Array type="java.lang.String">
              <Item>my.name.local</Item>
            </Array>
          </Set>
        </Configure>
    }
  }
}

While it seems to launch without complaints, visiting my.name.local doesn't hit Jetty as far as I can tell.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筑梦 2024-10-26 11:24:58

我个人更喜欢在 Linux 上使用 iptables 将端口 80 重新路由到 8080,而不是以 root 身份运行 sbt(这是危险的):

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

这仅在下次重新启动之前有效。为了使该设置在 Ubuntu 10.04 上保持不变,我使用:(

sudo bash -c "iptables-save > /etc/iptables.rules"
echo "#!/bin/sh
iptables-restore < /etc/iptables.rules
exit 0
" > /etc/network/if-pre-up.d/iptablesload
echo "#!/bin/sh
iptables-save -c > /etc/iptables.rules
if [ -f /etc/iptables.downrules ]; then
   iptables-restore < /etc/iptables.downrules
fi
exit 0
" > /etc/network/if-post-down.d/iptablessave
chmod +x /etc/network/if-post-down.d/iptablessave
chmod +x /etc/network/if-pre-up.d/iptablesload

请参阅此 Ubuntu iptables wiki

Rather than running sbt as root (dangerous), I personally prefer rerouting port 80 to 8080 using iptables on Linux :

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

Which works only until the next reboot. To make the setting persistent on Ubuntu 10.04, I use :

sudo bash -c "iptables-save > /etc/iptables.rules"
echo "#!/bin/sh
iptables-restore < /etc/iptables.rules
exit 0
" > /etc/network/if-pre-up.d/iptablesload
echo "#!/bin/sh
iptables-save -c > /etc/iptables.rules
if [ -f /etc/iptables.downrules ]; then
   iptables-restore < /etc/iptables.downrules
fi
exit 0
" > /etc/network/if-post-down.d/iptablessave
chmod +x /etc/network/if-post-down.d/iptablessave
chmod +x /etc/network/if-pre-up.d/iptablesload

(see this Ubuntu iptables wiki)

不再让梦枯萎 2024-10-26 11:24:58

我发帖太早了。我需要做的就是覆盖 jettyPort

override def jettyPort = 80

并通过 sudo 运行 sbt

I posted too soon. All I need to do is override jettyPort:

override def jettyPort = 80

And run sbt via sudo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文