|
81 | 81 | <h3>
|
82 | 82 | <a id="user-content-advanced-jvm-options-for-io" class="anchor" href="#advanced-jvm-options-for-io" aria-hidden="true"><span class="octicon octicon-link"></span></a>Advanced JVM Options for I/O</h3>
|
83 | 83 |
|
84 |
| -<p>Check <a href="configuration.html#24-chose--coroutine-based-socket-or-asynchronous-socketchannel-or-thread-pool-for-slow-io-operations">this section</a> for more deitals about choice and configuration about <code>thread pool</code> , <code>coroutined</code> based socket or <code>asynchronous socket/channel</code>.</p> |
| 84 | +<p>Check <a href="configuration.html#24-chose--coroutine-based-socket-or-asynchronous-socketchannel-or-thread-pool-for-slow-io-operations">this section</a> for more deitals about choice and configuration about <code>thread pool</code> , <code>coroutine</code> based socket or <code>asynchronous socket/channel</code>.</p> |
85 | 85 |
|
86 | 86 | <h3>
|
87 | 87 | <a id="user-content-some-useful-tips" class="anchor" href="#some-useful-tips" aria-hidden="true"><span class="octicon octicon-link"></span></a>Some Useful Tips</h3>
|
@@ -450,14 +450,45 @@ <h3>
|
450 | 450 | a bigger number.</p>
|
451 | 451 |
|
452 | 452 | <h2>
|
453 |
| -<a id="user-content-25-nginx-rewrite-handler" class="anchor" href="#25-nginx-rewrite-handler" aria-hidden="true"><span class="octicon octicon-link"></span></a>2.5 Nginx rewrite handler</h2> |
| 453 | +<a id="user-content-25-nginx-rewrite-handler" class="anchor" href="#25-nginx-rewrite-handler" aria-hidden="true"><span class="octicon octicon-link"></span></a>2.5 Nginx Rewrite Handler</h2> |
454 | 454 |
|
455 | 455 | <p>A nginx rewrite handler can be used to set var or return errors before proxy pass or content ring handler.
|
456 | 456 | If the rewrite handler returns <code>phase-done</code> (Clojure) or <code>PHASE_DONE</code> (Groovy/Java), nginx will continue to invoke proxy_pass or
|
457 | 457 | content ring handler.
|
458 | 458 | If the rewrite handler returns a general response, nginx will send this response to the client and stop to continue to invoke proxy_pass or
|
459 | 459 | content ring handler.</p>
|
460 | 460 |
|
| 461 | +<blockquote> |
| 462 | +<p><strong>Note:</strong> |
| 463 | +All rewrite directives, such as <code>rewrite</code>, <code>set</code>, will be executed after the invocation nginx clojure rewrite handler even if |
| 464 | +they are declared before nginx rewrtite handler. |
| 465 | +So the below example maybe is wrong. For more details about Nginx Variable please check this <a href="http://openresty.org/download/agentzh-nginx-tutorials-en.html">nginx tutorial</a><br> |
| 466 | +which explains perfectly the variable scope.</p> |
| 467 | +</blockquote> |
| 468 | + |
| 469 | +<div class="highlight highlight-nginx"><pre> |
| 470 | + <span class="pl-k">location</span> <span class="pl-en">/myproxy </span>{ |
| 471 | + <span class="pl-c">## It maybe is WRONG!!!</span> |
| 472 | + <span class="pl-c">## Because execution of directive `set` is after the execution of Nginx-Clojure rewrite handler</span> |
| 473 | + <span class="pl-k">set</span> <span class="pl-smi">$myhost</span> <span class="pl-s">""</span>; |
| 474 | + <span class="pl-k">rewrite_handler_type</span> <span class="pl-s">'clojure'</span>; |
| 475 | + <span class="pl-k">rewrite_handler_code</span> <span class="pl-s">' ....</span> |
| 476 | +<span class="pl-s"> '</span>; |
| 477 | + <span class="pl-k">proxy_pass</span> <span class="pl-smi">$myhost</span> |
| 478 | + } |
| 479 | +</pre></div> |
| 480 | + |
| 481 | +<p>This example is right and there we declare variable $myhost at the outside of <code>location {</code> block.</p> |
| 482 | + |
| 483 | +<div class="highlight highlight-nginx"><pre> <span class="pl-k">set</span> <span class="pl-smi">$myhost</span> <span class="pl-s">""</span>; |
| 484 | + <span class="pl-k">location</span> <span class="pl-en">/myproxy </span>{ |
| 485 | + <span class="pl-k">rewrite_handler_type</span> <span class="pl-s">'clojure'</span>; |
| 486 | + <span class="pl-k">rewrite_handler_code</span> <span class="pl-s">' ....</span> |
| 487 | +<span class="pl-s"> '</span>; |
| 488 | + <span class="pl-k">proxy_pass</span> <span class="pl-smi">$myhost</span> |
| 489 | + } |
| 490 | +</pre></div> |
| 491 | + |
461 | 492 | <h3>
|
462 | 493 | <a id="user-content-251-simple-example-about-nginx-rewrite-handler" class="anchor" href="#251-simple-example-about-nginx-rewrite-handler" aria-hidden="true"><span class="octicon octicon-link"></span></a>2.5.1 Simple Example about Nginx rewrite handler</h3>
|
463 | 494 |
|
@@ -510,23 +541,6 @@ <h3>
|
510 | 541 | }
|
511 | 542 | </pre></div>
|
512 | 543 |
|
513 |
| -<blockquote> |
514 |
| -<p><strong>Note:</strong> |
515 |
| - The below example is wrong becuase so far Nginx-Clojure has do nothing about making sure Nginx-Clojure rewrite handler execution is before/after the execution of directive <code>set</code>. |
516 |
| - For more details about Nginx Variable please check this <a href="http://openresty.org/download/agentzh-nginx-tutorials-en.html">nginx tutorial</a> which explains perfectly the variable scope.</p> |
517 |
| -</blockquote> |
518 |
| - |
519 |
| -<div class="highlight highlight-nginx"><pre> |
520 |
| - <span class="pl-k">location</span> <span class="pl-en">/myproxy </span>{ |
521 |
| - <span class="pl-c">##WRONG!!! Because execution of directive `set` maybe is after the execution of Nginx-Clojure rewrite handler</span> |
522 |
| - <span class="pl-k">set</span> <span class="pl-smi">$myhost</span> <span class="pl-s">""</span>; |
523 |
| - <span class="pl-k">rewrite_handler_type</span> <span class="pl-s">'clojure'</span>; |
524 |
| - <span class="pl-k">rewrite_handler_code</span> <span class="pl-s">' ....</span> |
525 |
| -<span class="pl-s"> '</span>; |
526 |
| - <span class="pl-k">proxy_pass</span> <span class="pl-smi">$myhost</span> |
527 |
| - } |
528 |
| -</pre></div> |
529 |
| - |
530 | 544 | <p>The equivalent java code is here</p>
|
531 | 545 |
|
532 | 546 | <div class="highlight highlight-java"><pre>
|
@@ -561,63 +575,7 @@ <h3>
|
561 | 575 | </pre></div>
|
562 | 576 |
|
563 | 577 | <h3>
|
564 |
| -<a id="user-content-253-simple-access-controller-by-nginx-rewrite-handler" class="anchor" href="#253-simple-access-controller-by-nginx-rewrite-handler" aria-hidden="true"><span class="octicon octicon-link"></span></a>2.5.3 Simple Access Controller By Nginx rewrite handler</h3> |
565 |
| - |
566 |
| -<p>For clojure</p> |
567 |
| - |
568 |
| -<div class="highlight highlight-nginx"><pre> <span class="pl-k">handler_type</span> <span class="pl-s">'clojure'</span>; |
569 |
| - <span class="pl-k">rewrite_handler_code</span> <span class="pl-s">'</span> |
570 |
| -<span class="pl-s"> (do (use <span class="pl-cce">\'</span>[nginx.clojure.core]) </span> |
571 |
| -<span class="pl-s"> (import <span class="pl-cce">\'</span>[com\.test AuthenticationHandler]) </span> |
572 |
| -<span class="pl-s"> (fn[req]</span> |
573 |
| -<span class="pl-s"> (if ((AuthenticationHandler.) req)</span> |
574 |
| -<span class="pl-s"> ;AuthenticationHandler returns true so we go to proxy_pass</span> |
575 |
| -<span class="pl-s"> phase-done </span> |
576 |
| -<span class="pl-s"> ;else return 403</span> |
577 |
| -<span class="pl-s"> {:status 403}</span> |
578 |
| -<span class="pl-s"> )))</span> |
579 |
| -<span class="pl-s"> '</span>; |
580 |
| -<span class="pl-k">proxy_pass</span> http://localhost:8084;</pre></div> |
581 |
| - |
582 |
| -<p>For Java</p> |
583 |
| - |
584 |
| -<ul> |
585 |
| -<li> |
586 |
| -<p>nginx.conf</p> |
587 |
| - |
588 |
| -<div class="highlight highlight-nginx"><pre> <span class="pl-k">rewrite_handler_type</span> <span class="pl-s">'java'</span>; |
589 |
| - <span class="pl-k">rewrite_handler_name</span> <span class="pl-s">'com.test.MyHandler'</span>; |
590 |
| - <span class="pl-k">proxy_pass</span> http://localhost:8084;</pre></div> |
591 |
| -</li> |
592 |
| -<li> |
593 |
| -<p>MyHandler.java</p> |
594 |
| - |
595 |
| -<div class="highlight highlight-java"><pre><span class="pl-k">package</span> <span class="pl-smi">com.test</span>; |
596 |
| -<span class="pl-k">import static</span> <span class="pl-smi">nginx.clojure.java.Constants.*</span>; |
597 |
| -<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">MyHandler</span> <span class="pl-k">implements</span> <span class="pl-e">NginxJavaRingHandler</span> { |
598 |
| - |
599 |
| - <span class="pl-k">public</span> <span class="pl-k">Object</span>[] <span class="pl-en">invoke</span>(<span class="pl-k">Map<<span class="pl-smi">String</span>,<span class="pl-smi">Object</span>></span> <span class="pl-v">req</span>) { |
600 |
| - |
601 |
| - <span class="pl-c">/*do some computing here*/</span> |
602 |
| - |
603 |
| - <span class="pl-k">if</span> (goto<span class="pl-k">-</span>proxy<span class="pl-k">-</span>pass) { |
604 |
| - <span class="pl-k">return</span> <span class="pl-c1">PHASE_DONE</span>; |
605 |
| - }<span class="pl-k">else</span> { <span class="pl-c">//return 403</span> |
606 |
| - <span class="pl-k">return</span> <span class="pl-k">Object</span>[] resps <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">Object</span>[] { |
607 |
| - <span class="pl-smi">Constants</span><span class="pl-c1"><span class="pl-k">.</span>STATUS</span>, <span class="pl-c1">403</span>, |
608 |
| - <span class="pl-c">//add some headers -- optional for no-20X response</span> |
609 |
| - <span class="pl-c">//Constants.HEADERS, nginx.clojure.java.ArrayMap.create(new Object[]{CONTENT_TYPE,"text/plain"}),</span> |
610 |
| - <span class="pl-c">//body text -- optional for no-20X response</span> |
611 |
| - <span class="pl-c">// Constants.BODY, "xxxxxxxxxxxxxx!"</span> |
612 |
| - }; |
613 |
| - } |
614 |
| - } |
615 |
| -</pre></div> |
616 |
| - |
617 |
| -<h3> |
618 |
| -<a id="user-content-254-access-request-body-in-nginx-rewrite-handler" class="anchor" href="#254-access-request-body-in-nginx-rewrite-handler" aria-hidden="true"><span class="octicon octicon-link"></span></a>2.5.4 Access request BODY in Nginx rewrite handler</h3> |
619 |
| -</li> |
620 |
| -</ul> |
| 578 | +<a id="user-content-253-access-request-body-in-nginx-rewrite-handler" class="anchor" href="#253-access-request-body-in-nginx-rewrite-handler" aria-hidden="true"><span class="octicon octicon-link"></span></a>2.5.3 Access request BODY in Nginx Rewrite Handler</h3> |
621 | 579 |
|
622 | 580 | <p>Try <code>always_read_body on;</code> where about the location you want to access the request body in a JAVA rewrite handler.
|
623 | 581 | We also added an example(for unit testing) about this, in nginx.conf</p>
|
|
0 commit comments