Skip to content

Commit 2f9572e

Browse files
committed
remove example about access control by rewrite handler, we prefer access
handler to do access control
1 parent b30dbfc commit 2f9572e

File tree

3 files changed

+104
-226
lines changed

3 files changed

+104
-226
lines changed

Configuration.md

+36-74
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ http {
8585

8686
###Advanced JVM Options for I/O
8787

88-
Check [this section](configuration.html#24-chose--coroutine-based-socket-or-asynchronous-socketchannel-or-thread-pool-for-slow-io-operations) for more deitals about choice and configuration about `thread pool` , `coroutined` based socket or `asynchronous socket/channel`.
88+
Check [this section](configuration.html#24-chose--coroutine-based-socket-or-asynchronous-socketchannel-or-thread-pool-for-slow-io-operations) for more deitals about choice and configuration about `thread pool` , `coroutine` based socket or `asynchronous socket/channel`.
8989

9090
###Some Useful Tips
9191

@@ -430,7 +430,7 @@ jvm_workers 40;
430430
Now Nginx-Clojure will create a thread pool with fixed 40 threads per JVM instance/Nginx worker to handle requests. If you get more memory, you can set
431431
a bigger number.
432432

433-
2.5 Nginx rewrite handler
433+
2.5 Nginx Rewrite Handler
434434
-----------------
435435

436436
A nginx rewrite handler can be used to set var or return errors before proxy pass or content ring handler.
@@ -439,6 +439,39 @@ content ring handler.
439439
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
440440
content ring handler.
441441

442+
> **Note:**
443+
All rewrite directives, such as `rewrite`, `set`, will be executed after the invocation nginx clojure rewrite handler even if
444+
they are declared before nginx rewrtite handler.
445+
So the below example maybe is wrong. For more details about Nginx Variable please check this [nginx tutorial](http://openresty.org/download/agentzh-nginx-tutorials-en.html)
446+
which explains perfectly the variable scope.
447+
448+
```nginx
449+
450+
location /myproxy {
451+
## It maybe is WRONG!!!
452+
## Because execution of directive `set` is after the execution of Nginx-Clojure rewrite handler
453+
set $myhost "";
454+
rewrite_handler_type 'clojure';
455+
rewrite_handler_code ' ....
456+
';
457+
proxy_pass $myhost
458+
}
459+
460+
```
461+
462+
This example is right and there we declare variable $myhost at the outside of `location {` block.
463+
464+
```nginx
465+
set $myhost "";
466+
location /myproxy {
467+
rewrite_handler_type 'clojure';
468+
rewrite_handler_code ' ....
469+
';
470+
proxy_pass $myhost
471+
}
472+
473+
```
474+
442475
### 2.5.1 Simple Example about Nginx rewrite handler
443476

444477
Here's a simple clojure example for Nginx rewrite handler :
@@ -493,23 +526,6 @@ We can also use this feature to complete a simple dynamic balancer , e.g.
493526
494527
```
495528

496-
> **Note:**
497-
> 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 `set`.
498-
> For more details about Nginx Variable please check this [nginx tutorial](http://openresty.org/download/agentzh-nginx-tutorials-en.html) which explains perfectly the variable scope.
499-
500-
```nginx
501-
502-
location /myproxy {
503-
##WRONG!!! Because execution of directive `set` maybe is after the execution of Nginx-Clojure rewrite handler
504-
set $myhost "";
505-
rewrite_handler_type 'clojure';
506-
rewrite_handler_code ' ....
507-
';
508-
proxy_pass $myhost
509-
}
510-
511-
```
512-
513529
The equivalent java code is here
514530

515531
```java
@@ -545,63 +561,9 @@ Then we set the java rewrtite handler in nginx.conf
545561
}
546562
547563
```
548-
549-
### 2.5.3 Simple Access Controller By Nginx rewrite handler
550-
551-
For clojure
552-
553-
```nginx
554-
handler_type 'clojure';
555-
rewrite_handler_code '
556-
(do (use \'[nginx.clojure.core])
557-
(import \'[com\.test AuthenticationHandler])
558-
(fn[req]
559-
(if ((AuthenticationHandler.) req)
560-
;AuthenticationHandler returns true so we go to proxy_pass
561-
phase-done
562-
;else return 403
563-
{:status 403}
564-
)))
565-
';
566-
proxy_pass http://localhost:8084;
567-
```
568-
569-
For Java
570-
* nginx.conf
571-
572-
```nginx
573-
rewrite_handler_type 'java';
574-
rewrite_handler_name 'com.test.MyHandler';
575-
proxy_pass http://localhost:8084;
576-
```
577-
578-
* MyHandler.java
579-
580-
```java
581-
package com.test;
582-
import static nginx.clojure.java.Constants.*;
583-
public class MyHandler implements NginxJavaRingHandler {
584564

585-
public Object[] invoke(Map<String,Object> req) {
586-
587-
/*do some computing here*/
588-
589-
if (goto-proxy-pass) {
590-
return PHASE_DONE;
591-
}else { //return 403
592-
return Object[] resps = new Object[] {
593-
Constants.STATUS, 403,
594-
//add some headers -- optional for no-20X response
595-
//Constants.HEADERS, nginx.clojure.java.ArrayMap.create(new Object[]{CONTENT_TYPE,"text/plain"}),
596-
//body text -- optional for no-20X response
597-
// Constants.BODY, "xxxxxxxxxxxxxx!"
598-
};
599-
}
600-
}
601-
602-
```
603565

604-
### 2.5.4 Access request BODY in Nginx rewrite handler
566+
### 2.5.3 Access request BODY in Nginx Rewrite Handler
605567

606568
Try `always_read_body on;` where about the location you want to access the request body in a JAVA rewrite handler.
607569
We also added an example(for unit testing) about this, in nginx.conf

Configuration.md.html

+34-76
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ <h3>
8181
<h3>
8282
<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>
8383

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>
8585

8686
<h3>
8787
<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>
450450
a bigger number.</p>
451451

452452
<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>
454454

455455
<p>A nginx rewrite handler can be used to set var or return errors before proxy pass or content ring handler.
456456
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
457457
content ring handler.
458458
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
459459
content ring handler.</p>
460460

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+
461492
<h3>
462493
<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>
463494

@@ -510,23 +541,6 @@ <h3>
510541
}
511542
</pre></div>
512543

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-
530544
<p>The equivalent java code is here</p>
531545

532546
<div class="highlight highlight-java"><pre>
@@ -561,63 +575,7 @@ <h3>
561575
</pre></div>
562576

563577
<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&lt;<span class="pl-smi">String</span>,<span class="pl-smi">Object</span>&gt;</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>
621579

622580
<p>Try <code>always_read_body on;</code> where about the location you want to access the request body in a JAVA rewrite handler.
623581
We also added an example(for unit testing) about this, in nginx.conf</p>

0 commit comments

Comments
 (0)