Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Server \ Nginx + Tomcat static and dynamic load balancing and separation configuration essentials under Linux     - Linux network security backdoor technology and advanced skill practice (Linux)

- vnStatSVG: traffic monitoring software vnStat (Linux)

- Normal start Lazarus 1.0.8 under Ubuntu (Linux)

- Linux system security norms (Linux)

- Ubuntu Locale configuration problem solving Can not set LC_CTYPE (Linux)

- To learn from scratch OpenWrt perfect tutorial (Linux)

- CentOS 6.x and CentOS7 install MPlayer (Linux)

- Ubuntu 14.04 install Nmap 6.46.1 (Linux)

- To install Samba server on CentOS 6.6 (Server)

- shellinabox: one uses AJAX Web-based terminal emulator (Linux)

- Source Analysis: Java object memory allocation (Programming)

- Enterprise Hadoop cluster architecture - DNS installation (Server)

- About Nginx 7 (Server)

- Vim plugin installation YouCompleteMe (Linux)

- Android working with Volley Comments (Programming)

- MySQL5.6 based GTID master-slave replication (Database)

- Linux System Getting Started Learning: Linux common log file (Linux)

- Ubuntu buffalo wzr-hp-300nh brush DD-WRT router system (Linux)

- HBase Application Development Review and Summary of Series (Database)

- C ++ type conversion and RTTI (Programming)

 
         
  Nginx + Tomcat static and dynamic load balancing and separation configuration essentials under Linux
     
  Add Date : 2018-11-21      
         
         
         
  Linux distribution used herein: CentOS6.7
First, install Nginx

Download Source: wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

Installation Source: yum install nginx-release-centos-6-0.el6.ngx.noarch.rpm

Install Nginx: yum install nginx

Start Nginx service: service nginx start

Stop Nginx service: service nginx stop

See Nginx running status: service nginx status

Check Nginx configuration file: nginx -t

The service runs reload the configuration: nginx -s reload

Add Nginx service from the start: chkconfig nginx on

Second, modify firewall rules

Nginx modify the host where the firewall configuration: vi / etc / sysconfig / iptables, add the port nginx used to the allowed list.

For example: -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT (Permits through port 80)

Modify Tomcat where the host firewall configuration: add vi / etc / sysconfig / iptables, tomcat port used to the allowed list.

For example: -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT (Permits port 8080 through)

If you have multiple tomcat on the host, then add more this rule, modify the corresponding port number.

After saving the restart Firewall: service iptables restart

Three, Tomcat load balancing configuration

When Nginx start loading default configuration file /etc/nginx/nginx.conf, while nginx.conf in references /etc/nginx/conf.d directory of all .conf files.

Therefore, some of their own custom configuration can be written to a separate .conf files, as long as the files are placed in this directory can be /etc/nginx/conf.d easy maintenance.

Create tomcats.conf: vi /etc/nginx/conf.d/tomcats.conf, which reads as follows:

1 upstream tomcats {
2 ip_hash;
3 server 192.168.0.251:8080;
4 server 192.168.0.251:8081;
5 server 192.168.0.251:8082;
6}
Modify default.conf: vi /etc/nginx/conf.d/default.conf, modified as follows:

 Note # 1 original configuration
 2 #location / {
 3 # root / usr / share / nginx / html;
 4 # index index.html index.htm;
 # 5}
 6
 # 7 New configuration will forward the request to the default configuration for processing upstream tomcats.conf
 8 location / {
 9 proxy_set_header Host $ host;
10 proxy_set_header X-Real-IP $ remote_addr;
11 proxy_set_header REMOTE-HOST $ remote_addr;
12 proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
13 proxy_pass http: // tomcats; # configuration and upstream of the same name in the tomcats.conf
14}
After saving reload the configuration: nginx -s reload

Fourth, the separation of static resource configuration

Modify default.conf: vi /etc/nginx/conf.d/default.conf, add the following configuration:

 # 1 all js, css requests related to static resource files processed by Nginx
 2 location ~ * \.. (Js | css) $ {
 # Specify the file path; 3 root / opt / static-resources
 4 expires 12h; # expire 12 hours
 5}
 Request # 6 All photos and other multimedia-related static resource files processed by Nginx
 7 location ~ * \.. (Html ​​| jpg | jpeg | png | bmp | gif | ico | mp3 | mid | wma | mp4 | swf | flv | rar | zip | txt | doc | ppt | xls | pdf) $ {
 # Specify the file path; 8 root / opt / static-resources
 9 expires 7d; # expiration time 7 days
10}
Fifth, modify SELinux security rules

502 Bad Gateway error when accessing Nginx, it may be SELinux on Nginx master limited their use due to http access, enter the command setsebool -P httpd_can_network_connect 1 permission to open.

/etc/nginx/nginx.conf Complete configuration file as follows:

 1 user nginx;
 2 worker_processes auto;
 3
 4 error_log /var/log/nginx/error.log warn;
 5 pid /var/run/nginx.pid;
 6 worker_rlimit_nofile 100000;
 7
 8
 9 events {
10 use epoll;
11 multi_accept on;
12 worker_connections 1024;
13}
14
Fifteen
16 http {
17 include /etc/nginx/mime.types;
18 default_type application / octet-stream;
19
20 #log_format main '$ remote_addr - $ remote_user [$ time_local] "$ request"'
21 # '$ status $ body_bytes_sent "$ http_referer"'
22 # ' "$ http_user_agent" "$ http_x_forwarded_for"';
twenty three
24 #access_log /var/log/nginx/access.log main;
25
26 sendfile on;
27 server_tokens off;
28 #tcp_nopush on;
29
30 keepalive_timeout 65;
31
32 gzip on;
33 gzip_disable "msie6";
34 gzip_static on;
35 gzip_proxied any;
36 gzip_min_length 1000;
37 gzip_comp_level 4;
38 gzip_types text / plain text / css application / json application / x-javascript text / xml application / xml application / xml + rss text / javascript;
39
40 include /etc/nginx/conf.d/*.conf;
41}
/etc/nginx/conf.d/default.conf Complete configuration file as follows:

 1 server {
 2 listen 80;
 3 server_name localhost;
 4
 5 #charset koi8-r;
 6 #access_log /var/log/nginx/log/host.access.log main;
 7
 8 #location / {
 9 # root / usr / share / nginx / html;
10 # index index.html index.htm;
11 #}
12
13 location / {
14 proxy_set_header Host $ host;
15 proxy_set_header X-Real-IP $ remote_addr;
16 proxy_set_header REMOTE-HOST $ remote_addr;
17 proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
18 proxy_pass http: // web_servers;
19}
20
21 location ~ * \.. (Js | css) $ {
22 root / opt / static-resources;
23 expires 12h;
twenty four }
25
26 location ~ * \.. (Html ​​| jpg | jpeg | png | bmp | gif | ico | mp3 | mid | wma | mp4 | swf | flv | rar | zip | txt | doc | ppt | xls | pdf) $ {
27 root / opt / static-resources;
28 expires 7d;
29}
30
31 #error_page 404 /404.html;
32
33 # redirect server error pages to the static page /50x.html
34 #
35 error_page 500 502 503 504 /50x.html;
36 location = /50x.html {
37 root / usr / share / nginx / html;
38}
39
40 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
41 #
42 #location ~ \ .php $ {
43 # proxy_pass http://127.0.0.1;
44 #}
forty five
46 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
47 #
48 #location ~ \ .php $ {
49 # root html;
50 # fastcgi_pass 127.0.0.1:9000;
51 # fastcgi_index index.php;
52 # fastcgi_param SCRIPT_FILENAME / scripts $ fastcgi_script_name;
53 # include fastcgi_params;
54 #}
55
56 # deny access to .htaccess files, if Apache's document root
57 # concurs with nginx's one
58 #
59 #location ~ /\.ht {
60 # deny all;
61 #}
62}
(Tips: If you do not have root privileges command, add sudo before the command execution)
     
         
         
         
  More:      
 
- MySQL Data Types (Database)
- CentOS7 virtual machine creation failed Solution (Linux)
- To batch create users under Linux (Linux)
- Ceph single / multi-node installation summary Powered by CentOS 6.x (Server)
- Questions about Linux compiler u-boot (Programming)
- To create a file in Linux directory by setfacl (Linux)
- The Zabbix2.4.5 source compiler installation under Ubuntu 14.04 (Server)
- Singleton (Linux)
- JavaScript basic tools list (Programming)
- MySQL High Availability plan several options (Database)
- Python Basics: Search Path (Programming)
- Workspace Go language and environment variables GOPATH (Linux)
- CentOS 7 source code to compile and install Nginx process record (Server)
- Linux shell in back quotes (`) Application (Linux)
- CentOS / Linux NIC set IP address configuration (Linux)
- Linux Security Raiders SSH tools SCP uses examples to explain in detail (Linux)
- Depth Java Singleton (Programming)
- C ++ multithreading and critical resource instance (Programming)
- CentOS7 install JDK (Linux)
- crontab cause CPU exception Analysis and Processing (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.