我已经建立了一个独立的Jetty服务器(版本9.4.19)并向其部署了Angular 8应用程序。
基本href看起来像这样:<base href="/myapp/">
。
我可以通过<server-ip>:8080/myapp/
访问该应用程序。
我知道,我必须为码头实施重写规则,以将<server-ip>:8080/myapp/*
形式的任何请求引回到index.html,以便angular处理请求。
我激活了重写模块(java -jar start.jar add-to-start=rewrite
)。然后,我编辑了jetty-rewrite.xml
文件并尝试添加规则。该文件如下所示:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- configure rewrite handler -->
<!-- =========================================================== -->
<Call name="insertHandler">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<Set name="rewriteRequestURI"><Property name="jetty.rewrite.rewriteRequestURI" deprecated="rewrite.rewriteRequestURI" default="true"/></Set>
<Set name="rewritePathInfo"><Property name="jetty.rewrite.rewritePathInfo" deprecated="rewrite.rewritePathInfo" default="false"/></Set>
<Set name="originalPathAttribute"><Property name="jetty.rewrite.originalPathAttribute" deprecated="rewrite.originalPathAttribute" default="requestedPath"/></Set>
<!-- Set DispatcherTypes -->
<Set name="dispatcherTypes">
<Array type="javax.servlet.DispatcherType">
<Item><Call class="javax.servlet.DispatcherType" name="valueOf"><Arg>REQUEST</Arg></Call></Item>
<Item><Call class="javax.servlet.DispatcherType" name="valueOf"><Arg>ASYNC</Arg></Call></Item>
</Array>
</Set>
<!--<Get id="Rewrite" name="ruleContainer"/>-->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
<Set name="pattern">/myapp/*</Set>
<Set name="replacement">/myapp/</Set>
</New>
</Arg>
</Call>
<!-- see rewrite-compactpath.xml for example how to add a rule -->
</New>
</Arg>
</Call>
</Configure>
当我在应用程序中刷新页面时,收到404错误消息。我想这意味着重写不起作用。
我试图将重写目标更改为<Set name="replacement">/</Set>
和<Set name="replacement">/myapp/index.html</Set>
,但这也不起作用。
有人可以指出我正确的方向吗?
编辑:
我发现:当我将RewritePatternRule的模式更改为应用程序的特定部分(例如<Set name="pattern">/myapp/dashboard</Set>
)并从该页面刷新时,重写工作正常,但不适用于应用程序的其他部分。
答案 0 :(得分:0)
RewritePatternRule
将重写模式部分,而将pathInfo
部分保留下来。
如果您将org.eclipse.jetty.rewrite
的日志记录级别设置为DEBUG,则会看到...
[DEBUG]:oejrh.RuleContainer: applied org.eclipse.jetty.rewrite.handler.RewritePatternRule[ht][/myapp/*][/myapp/]
[DEBUG]:oejrh.RuleContainer: rewrote /myapp/alt.html to /myapp/alt.html
如果您切换到RewriteRegexRule
,则可以更好地控制内容。
示例设置...
$ mkdir ~/rewrite-test-base
$ cd ~/rewrite-test-base/
[rewrite-test-base]$ java -jar ~/jetty-home-9.4.20.v20190813/start.jar --add-to-start=http,deploy,rewrite,resources
INFO : webapp transitively enabled, ini template available with --add-to-start=webapp
INFO : server transitively enabled, ini template available with --add-to-start=server
INFO : security transitively enabled
INFO : servlet transitively enabled
INFO : http initialized in ${jetty.base}/start.ini
INFO : threadpool transitively enabled, ini template available with --add-to-start=threadpool
INFO : bytebufferpool transitively enabled, ini template available with --add-to-start=bytebufferpool
INFO : rewrite initialized in ${jetty.base}/start.ini
INFO : deploy initialized in ${jetty.base}/start.ini
MKDIR : ${jetty.base}/webapps
INFO : Base directory was modified
# Setup the rewrite rules
$ cd ~/rewrite-test-base
$ mkdir etc
$ cp ~/tmp/my-rewrite.xml etc/
$ echo "etc/my-rewrite.xml" >> start.ini
# Create some content to work with
$ cd ~/rewrite-test-base
$ mkdir webapps/myapp
$ echo "This is the index.html" > webapps/myapp/index.html
$ echo "This you shouldn't be able to access" > webapps/myapp/alt.html
# Now start the server.
$ cd ~/rewrite-test-base
$ java -jar ~/jetty-home-9.4.20.v20190813/start.jar
2019-09-19 13:40:46.582:INFO::main: Logging initialized @331ms to org.eclipse.jetty.util.log.StdErrLog
2019-09-19 13:40:46.619:INFO:oeju.TypeUtil:main: JVM Runtime does not support Modules
2019-09-19 13:40:46.668:WARN:oejx.XmlConfiguration:main: Deprecated method public void org.eclipse.jetty.server.HttpConfiguration.setBlockingTimeout(long) in file:///home/joakim/jetty-home-9.4.20.v20190813/etc/jetty.xml
2019-09-19 13:40:46.744:INFO:oejs.Server:main: jetty-9.4.20.v20190813; built: 2019-08-13T21:28:18.144Z; git: 84700530e645e812b336747464d6fbbf370c9a20; jvm 1.8.0_222-b10
2019-09-19 13:40:46.757:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///home/joakim/rewrite-test-base/webapps/] at interval 1
2019-09-19 13:40:46.816:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /myapp, did not find org.eclipse.jetty.jsp.JettyJspServlet
2019-09-19 13:40:46.823:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
2019-09-19 13:40:46.823:INFO:oejs.session:main: No SessionScavenger set, using defaults
2019-09-19 13:40:46.824:INFO:oejs.session:main: node0 Scavenging every 600000ms
2019-09-19 13:40:46.837:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@2758fe70{myapp,/myapp,file:///home/joakim/rewrite-test-base/webapps/myapp/,AVAILABLE}{/home/joakim/rewrite-test-base/webapps/myapp}
my-rewrite.xml
<?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RuleContainer">
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewriteRegexRule">
<Set name="regex">/myapp/.*$</Set>
<Set name="replacement">/myapp/</Set>
</New>
</Arg>
</Call>
</Configure>
对其进行测试...
$ curl -vvv http://localhost:8080/myapp/
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /myapp/ HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Last-Modified: Thu, 19 Sep 2019 18:35:54 GMT
< Content-Type: text/html
< Accept-Ranges: bytes
< Content-Length: 23
< Server: Jetty(9.4.20.v20190813)
<
This is the index.html
* Connection #0 to host localhost left intact
$ curl -vvv http://localhost:8080/myapp/alt.html
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /myapp/alt.html HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Last-Modified: Thu, 19 Sep 2019 18:35:54 GMT
< Content-Type: text/html
< Accept-Ranges: bytes
< Content-Length: 23
< Server: Jetty(9.4.20.v20190813)
<
This is the index.html
* Connection #0 to host localhost left intact
$ curl -vvv http://localhost:8080/myapp/afdjklafjdskal
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /myapp/afdjklafjdskal HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Last-Modified: Thu, 19 Sep 2019 18:35:54 GMT
< Content-Type: text/html
< Accept-Ranges: bytes
< Content-Length: 23
< Server: Jetty(9.4.20.v20190813)
<
This is the index.html
* Connection #0 to host localhost left intact