Skip to content

Commit fd82141

Browse files
committed
add some explanations about more than one nginx worker processes
1 parent 9003a24 commit fd82141

13 files changed

+133
-33
lines changed

Configuration.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Check [this section](configuration.html#24-chose--coroutine-based-socket-or-asyn
9292
These tips are really useful. Most of them are from real users. Thanks [Rickr Nook](https://github.com/rickr-nook) who give us some useful tips.
9393

9494
1. The number of embed JVMs is the same with Nginx `worker_processes`, so if `worker_processes` > 1 we maybe need [nginx-clojure broadcast API][], shared memory (e.g [SharedHashMap/Chronicle-Map][]) or
95-
even external service(e.g. redis, database) to coordinate the state.
95+
even external service(e.g. redis, database) to coordinate the state or use cookie based session store to manage session information, e.g. [ring.middleware.session.cookie/cookie-store](https://github.com/mmcgrana/ring/wiki/Sessions).
9696
1. When importing Swing We Must specifiy `jvm_options "-Djava.awt.headless=true"` , otherwise the nginx will hang.
9797
1. By adding the location of your clojure source files to the classpath,then just issue "nginx -s reload" and changes to the sources get picked up!
9898
1. You can remove clojure-1.5.1.jar from class path and point at your "lein uberjar" to pick up a different version of clojure.
@@ -665,7 +665,7 @@ e.g.
665665
}
666666
```
667667

668-
2.7 Niginx Header Filter
668+
2.7 Nginx Header Filter
669669
-----------------
670670

671671
We can use Nginx Header Filter written by Java/Clojure/Groovy to do some useful things, e.g.

Configuration.md.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ <h3>
9090

9191
<ol>
9292
<li>The number of embed JVMs is the same with Nginx <code>worker_processes</code>, so if <code>worker_processes</code> &gt; 1 we maybe need <a href="https://github.com/nginx-clojure/nginx-clojure/issues/38">nginx-clojure broadcast API</a>, shared memory (e.g <a href="https://github.com/OpenHFT/Chronicle-Map">SharedHashMap/Chronicle-Map</a>) or
93-
even external service(e.g. redis, database) to coordinate the state.</li>
93+
even external service(e.g. redis, database) to coordinate the state or use cookie based session store to manage session information, e.g. <a href="https://github.com/mmcgrana/ring/wiki/Sessions">ring.middleware.session.cookie/cookie-store</a>.</li>
9494
<li>When importing Swing We Must specifiy <code>jvm_options "-Djava.awt.headless=true"</code> , otherwise the nginx will hang.</li>
9595
<li>By adding the location of your clojure source files to the classpath,then just issue "nginx -s reload" and changes to the sources get picked up!</li>
9696
<li>You can remove clojure-1.5.1.jar from class path and point at your "lein uberjar" to pick up a different version of clojure. </li>
@@ -675,7 +675,7 @@ <h2>
675675
}</pre></div>
676676

677677
<h2>
678-
<a id="user-content-27-niginx-header-filter" class="anchor" href="#27-niginx-header-filter" aria-hidden="true"><span class="octicon octicon-link"></span></a>2.7 Niginx Header Filter</h2>
678+
<a id="user-content-27-nginx-header-filter" class="anchor" href="#27-nginx-header-filter" aria-hidden="true"><span class="octicon octicon-link"></span></a>2.7 Nginx Header Filter</h2>
679679

680680
<p>We can use Nginx Header Filter written by Java/Clojure/Groovy to do some useful things, e.g. </p>
681681

More.md

+30
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,16 @@ in nginx.conf
738738
}
739739
```
740740
741+
## Session Management
742+
743+
If `worker_processes` > 1 there will be more than one jvm instances viz. more tomcat instances so to get synchronized session information we can not use the default tomcat session manger.
744+
Instead we may consider to use either of
745+
746+
1. Cookie based Session Store viz. storing all session attribute information into cookies.
747+
1. Shared HashMap among processes in the same machine ,e.g. [Chronicle Map](https://github.com/OpenHFT/Chronicle-Map)
748+
1. External Session Store, e.g. Redis / MySQL / Memcached Session Store
749+
750+
741751
### For Performance
742752
743753
#### Disable Tomcat Access Log
@@ -789,3 +799,23 @@ location /examples {
789799
[Asynchronous Channel API]: https://github.com/nginx-clojure/nginx-clojure/issues/37
790800
[2.2 Initialization Handler for nginx worker]: configuration.html#user-content-22-initialization-handler-for-nginx-worker
791801

802+
3.11 More about Nginx Worker Process
803+
-----------------
804+
The number of Nginx worker processes can be configured by nginx directive [worker_processes](http://nginx.org/en/docs/ngx_core_module.html#worker_processes).
805+
e.g. We use 8 Nginx worker processes:
806+
807+
```nginx
808+
worker_processes 8;
809+
```
810+
811+
Because a JVM instance will be embeded into per Nginx worker process so if there are more than one Nginx worker prcocesses there will be more than one JVM instances.
812+
So if we want to get synchronized session information we can not use the default tomcat session manger. Instead we may consider to use either of
813+
814+
1. Cookie based Session Store viz. storing all session attribute information into cookies.
815+
1. Shared HashMap among processes in the same machine ,e.g. [Chronicle Map](https://github.com/OpenHFT/Chronicle-Map)
816+
1. External Session Store, e.g. Redis / MySQL / Memcached Session Store
817+
818+
When there are more than one Nginx worker prcocesses sub/pub services also need to be taken care of. Subscribers may connect to JVM instances which maybe are different
819+
with the JVM instance which the publisher send message to. So we need to use [broadcast API](more.html#35-subpub--broadcast-event) from nginx-clojure
820+
and broadcast message to all Nginx worker prcocesses.
821+

More.md.html

+35
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,19 @@ <h3>
714714
<span class="pl-c">#write_page_size 2k;</span>
715715
}</pre></div>
716716

717+
<h2>
718+
<a id="user-content-session-management" class="anchor" href="#session-management" aria-hidden="true"><span class="octicon octicon-link"></span></a>Session Management</h2>
719+
720+
<p>If <code>worker_processes</code> &gt; 1 there will be more than one jvm instances viz. more tomcat instances so to get synchronized session information we can not use the default tomcat session manger.
721+
Instead we may consider to use either of</p>
722+
723+
<ol>
724+
<li>Cookie based Session Store viz. storing all session attribute information into cookies.</li>
725+
<li>Shared HashMap among processes in the same machine ,e.g. <a href="https://github.com/OpenHFT/Chronicle-Map">Chronicle Map</a>
726+
</li>
727+
<li>External Session Store, e.g. Redis / MySQL / Memcached Session Store</li>
728+
</ol>
729+
717730
<h3>
718731
<a id="user-content-for-performance" class="anchor" href="#for-performance" aria-hidden="true"><span class="octicon octicon-link"></span></a>For Performance</h3>
719732

@@ -755,3 +768,25 @@ <h4>
755768
}</pre></div>
756769

757770
<p><code>gzip</code> can also appear at http, server blocks. More details can be found <a href="http://nginx.org/en/docs/http/ngx_http_gzip_module.html">HERE</a></p>
771+
772+
<h2>
773+
<a id="user-content-311-more-about-nginx-worker-process" class="anchor" href="#311-more-about-nginx-worker-process" aria-hidden="true"><span class="octicon octicon-link"></span></a>3.11 More about Nginx Worker Process</h2>
774+
775+
<p>The number of Nginx worker processes can be configured by nginx directive <a href="http://nginx.org/en/docs/ngx_core_module.html#worker_processes">worker_processes</a>.
776+
e.g. We use 8 Nginx worker processes:</p>
777+
778+
<div class="highlight highlight-nginx"><pre><span class="pl-k">worker_processes</span> <span class="pl-s">8</span>;</pre></div>
779+
780+
<p>Because a JVM instance will be embeded into per Nginx worker process so if there are more than one Nginx worker prcocesses there will be more than one JVM instances.
781+
So if we want to get synchronized session information we can not use the default tomcat session manger. Instead we may consider to use either of </p>
782+
783+
<ol>
784+
<li>Cookie based Session Store viz. storing all session attribute information into cookies.</li>
785+
<li>Shared HashMap among processes in the same machine ,e.g. <a href="https://github.com/OpenHFT/Chronicle-Map">Chronicle Map</a>
786+
</li>
787+
<li>External Session Store, e.g. Redis / MySQL / Memcached Session Store</li>
788+
</ol>
789+
790+
<p>When there are more than one Nginx worker prcocesses sub/pub services also need to be taken care of. Subscribers may connect to JVM instances which maybe are different
791+
with the JVM instance which the publisher send message to. So we need to use <a href="more.html#35-subpub--broadcast-event">broadcast API</a> from nginx-clojure
792+
and broadcast message to all Nginx worker prcocesses.</p>

configuration.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ <h6 id="project_tagline">Nginx module for embedding Clojure / Java / Groovy prog
5858
<a href="index.html" class="btn btn-sample btn-lg"><span class="glyphicon glyphicon-home"></span> Home</a>
5959
<a href="quickstart.html" class="btn btn-sample btn-lg"><span class="glyphicon glyphicon-flash"></span> Quick Start</a>
6060
<a href="downloads.html" class="btn btn-sample btn-lg"><span class="glyphicon glyphicon-download-alt"></span> Downloads</a>
61+
<a href="installation.html" class="btn btn-sample btn-lg"><span class="glyphicon glyphicon-hdd"></span> Installation</a>
6162
<div class="btn-group">
6263
<button type="button" class="btn btn-sample btn-lg dropdown-toggle" data-toggle="dropdown" id="docDropDownBtn"><span class="glyphicon glyphicon-cog"></span> Configuration <span class="caret"></span></button>
6364
<ul class="dropdown-menu" role="menu">
@@ -67,15 +68,13 @@ <h6 id="project_tagline">Nginx module for embedding Clojure / Java / Groovy prog
6768
<li><a href="configuration.html#24-chose--coroutine-based-socket-or-asynchronous-socketchannel-or-thread-pool-for-slow-io-operations"><span class="glyphicon glyphicon-cog"></span> Coroutine/Asynchronous Client Channel/Thread Pool </a></li>
6869
<li><a href="configuration.html#25-nginx-rewrite-handler"><span class="glyphicon glyphicon-cog"></span> Nginx Rewrite Handler</a></li>
6970
<li><a href="configuration.html#26-nginx-access-handler"><span class="glyphicon glyphicon-cog"></span> Nginx Access Handler</a></li>
70-
<li><a href="configuration.html#27-niginx-header-filter"><span class="glyphicon glyphicon-cog"></span> Niginx Header Filter</a></li>
71+
<li><a href="configuration.html#27-nginx-header-filter"><span class="glyphicon glyphicon-cog"></span> Nginx Header Filter</a></li>
7172
</ul>
7273
</div>
7374
<div class="btn-group">
7475
<button type="button" class="btn btn-sample btn-lg dropdown-toggle" data-toggle="dropdown" id="docDropDownBtn"><span class="glyphicon glyphicon-book"></span> Documents <span class="caret"></span></button>
7576
<ul class="dropdown-menu" role="menu">
7677
<!-- <li><a href="quickstart.html"><span class="glyphicon glyphicon-flash"></span> Quick Start</a></li> -->
77-
<li><a href="installation.html"><span class="glyphicon glyphicon-hdd"></span> Installation</a></li>
78-
<li><a href="configuration.html"><span class="glyphicon glyphicon-cog"></span> Configuration</a></li>
7978
<li><a href="embed.html"><span class="glyphicon glyphicon-gift"></span> Embedding Nginx-Clojure into A standard App</a></li>
8079
<li><a href="more.html#34-server-channel-for-long-polling--server-sent-events-sse"><span class="glyphicon glyphicon-envelope"></span> Server Channel for Long Polling & Server Sent Events</a></li>
8180
<li><a href="more.html#35-subpub--broadcast-event"><span class="glyphicon glyphicon-signal"></span> Sub/Pub services with Broadcast Events</a></li>
@@ -84,6 +83,7 @@ <h6 id="project_tagline">Nginx module for embedding Clojure / Java / Groovy prog
8483
<li><a href="more.html#38--sever-side-websocket"><span class="glyphicon glyphicon-th-list"></span> Sever Side WebSocket</a></li>
8584
<li><a href="more.html#39--java-standard-restful-web-services-with-jersey"><span class="glyphicon glyphicon-th-list"></span> Java standard RESTful web services with Jersey</a></li>
8685
<li><a href="more.html#310-embeding-tomcat"><span class="glyphicon glyphicon-th-list"></span> Embeding Tomcat</a></li>
86+
<li><a href="more.html#311-more-about-nginx-worker-process"><span class="glyphicon glyphicon-th-list"></span> More about Nginx Worker Process</a></li>
8787
<li><a href="more.html"><span class="glyphicon glyphicon-th-list"></span> More about Nginx-Clojure</a></li>
8888
</ul>
8989
</div>
@@ -187,7 +187,7 @@ <h3>
187187

188188
<ol>
189189
<li>The number of embed JVMs is the same with Nginx <code>worker_processes</code>, so if <code>worker_processes</code> &gt; 1 we maybe need <a href="https://github.com/nginx-clojure/nginx-clojure/issues/38">nginx-clojure broadcast API</a>, shared memory (e.g <a href="https://github.com/OpenHFT/Chronicle-Map">SharedHashMap/Chronicle-Map</a>) or
190-
even external service(e.g. redis, database) to coordinate the state.</li>
190+
even external service(e.g. redis, database) to coordinate the state or use cookie based session store to manage session information, e.g. <a href="https://github.com/mmcgrana/ring/wiki/Sessions">ring.middleware.session.cookie/cookie-store</a>.</li>
191191
<li>When importing Swing We Must specifiy <code>jvm_options "-Djava.awt.headless=true"</code> , otherwise the nginx will hang.</li>
192192
<li>By adding the location of your clojure source files to the classpath,then just issue "nginx -s reload" and changes to the sources get picked up!</li>
193193
<li>You can remove clojure-1.5.1.jar from class path and point at your "lein uberjar" to pick up a different version of clojure. </li>
@@ -772,7 +772,7 @@ <h2>
772772
}</pre></div>
773773

774774
<h2>
775-
<a id="user-content-27-niginx-header-filter" class="anchor" href="#27-niginx-header-filter" aria-hidden="true"><span class="octicon octicon-link"></span></a>2.7 Niginx Header Filter</h2>
775+
<a id="user-content-27-nginx-header-filter" class="anchor" href="#27-nginx-header-filter" aria-hidden="true"><span class="octicon octicon-link"></span></a>2.7 Nginx Header Filter</h2>
776776

777777
<p>We can use Nginx Header Filter written by Java/Clojure/Groovy to do some useful things, e.g. </p>
778778

downloads.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ <h6 id="project_tagline">Nginx module for embedding Clojure / Java / Groovy prog
5858
<a href="index.html" class="btn btn-sample btn-lg"><span class="glyphicon glyphicon-home"></span> Home</a>
5959
<a href="quickstart.html" class="btn btn-sample btn-lg"><span class="glyphicon glyphicon-flash"></span> Quick Start</a>
6060
<a href="downloads.html" class="btn btn-sample btn-lg"><span class="glyphicon glyphicon-download-alt"></span> Downloads</a>
61+
<a href="installation.html" class="btn btn-sample btn-lg"><span class="glyphicon glyphicon-hdd"></span> Installation</a>
6162
<div class="btn-group">
6263
<button type="button" class="btn btn-sample btn-lg dropdown-toggle" data-toggle="dropdown" id="docDropDownBtn"><span class="glyphicon glyphicon-cog"></span> Configuration <span class="caret"></span></button>
6364
<ul class="dropdown-menu" role="menu">
@@ -67,15 +68,13 @@ <h6 id="project_tagline">Nginx module for embedding Clojure / Java / Groovy prog
6768
<li><a href="configuration.html#24-chose--coroutine-based-socket-or-asynchronous-socketchannel-or-thread-pool-for-slow-io-operations"><span class="glyphicon glyphicon-cog"></span> Coroutine/Asynchronous Client Channel/Thread Pool </a></li>
6869
<li><a href="configuration.html#25-nginx-rewrite-handler"><span class="glyphicon glyphicon-cog"></span> Nginx Rewrite Handler</a></li>
6970
<li><a href="configuration.html#26-nginx-access-handler"><span class="glyphicon glyphicon-cog"></span> Nginx Access Handler</a></li>
70-
<li><a href="configuration.html#27-niginx-header-filter"><span class="glyphicon glyphicon-cog"></span> Niginx Header Filter</a></li>
71+
<li><a href="configuration.html#27-nginx-header-filter"><span class="glyphicon glyphicon-cog"></span> Nginx Header Filter</a></li>
7172
</ul>
7273
</div>
7374
<div class="btn-group">
7475
<button type="button" class="btn btn-sample btn-lg dropdown-toggle" data-toggle="dropdown" id="docDropDownBtn"><span class="glyphicon glyphicon-book"></span> Documents <span class="caret"></span></button>
7576
<ul class="dropdown-menu" role="menu">
7677
<!-- <li><a href="quickstart.html"><span class="glyphicon glyphicon-flash"></span> Quick Start</a></li> -->
77-
<li><a href="installation.html"><span class="glyphicon glyphicon-hdd"></span> Installation</a></li>
78-
<li><a href="configuration.html"><span class="glyphicon glyphicon-cog"></span> Configuration</a></li>
7978
<li><a href="embed.html"><span class="glyphicon glyphicon-gift"></span> Embedding Nginx-Clojure into A standard App</a></li>
8079
<li><a href="more.html#34-server-channel-for-long-polling--server-sent-events-sse"><span class="glyphicon glyphicon-envelope"></span> Server Channel for Long Polling & Server Sent Events</a></li>
8180
<li><a href="more.html#35-subpub--broadcast-event"><span class="glyphicon glyphicon-signal"></span> Sub/Pub services with Broadcast Events</a></li>
@@ -84,6 +83,7 @@ <h6 id="project_tagline">Nginx module for embedding Clojure / Java / Groovy prog
8483
<li><a href="more.html#38--sever-side-websocket"><span class="glyphicon glyphicon-th-list"></span> Sever Side WebSocket</a></li>
8584
<li><a href="more.html#39--java-standard-restful-web-services-with-jersey"><span class="glyphicon glyphicon-th-list"></span> Java standard RESTful web services with Jersey</a></li>
8685
<li><a href="more.html#310-embeding-tomcat"><span class="glyphicon glyphicon-th-list"></span> Embeding Tomcat</a></li>
86+
<li><a href="more.html#311-more-about-nginx-worker-process"><span class="glyphicon glyphicon-th-list"></span> More about Nginx Worker Process</a></li>
8787
<li><a href="more.html"><span class="glyphicon glyphicon-th-list"></span> More about Nginx-Clojure</a></li>
8888
</ul>
8989
</div>

0 commit comments

Comments
 (0)