Configuration points of Nginx + Tomcat load balancing and dynamic/static separation in Linux
This article uses the Linux release: CentOS6.7: https://wiki.centos.org/Download
1. 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
View Nginx running status: service nginx status
Check Nginx configuration file: nginx-t
Reload configuration during service running: nginx-s reload
Add Nginx Service self-start: chkconfig nginx on
Ii. Modify firewall rules
Modify the firewall configuration of the host where Nginx is located: vi/etc/sysconfig/iptables, and add the ports used by nginx to the allowed list.
For example,-a input-m state -- state NEW-m tcp-p tcp -- dport 80-j ACCEPT (indicating that port 80 is allowed)
Modify the firewall configuration of the host where Tomcat is located: vi/etc/sysconfig/iptables, and add the port used by tomcat to the allow list.
For example,-a input-m state -- state NEW-m tcp-p tcp -- dport 8080-j ACCEPT (indicating that port 8080 is allowed)
If there are multiple tomcat servers on the host, add multiple tomcat servers according to this rule and modify the corresponding port number.
Restart firewall after saving: service iptables restart
Iii. Tomcat Load Balancing Configuration
When Nginx is started, the configuration file/etc/nginx. conf is loaded by default, and all. conf files in the/etc/nginx/conf. d directory are referenced in nginx. conf.
Therefore, you can write custom configurations to a separate. conf file, as long as the file is placed in the/etc/nginx/conf. d directory for easy maintenance.
Create tomcats. conf: vi/etc/nginx/conf. d/tomcats. conf. The content is 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 as follows:
1 # comment out the original configuration 2 # location/{3 # root/usr/share/nginx/html; 4 # index index.html index.htm; 5 #}6 7 # Add a configuration to forward requests to tomcats by default. the upstream configured in conf is processed 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: // using ATS; # With using ATS. the upstream configured in conf has the same name 14}
Save and reload the configuration: nginx-s reload
Iv. Static resource separation configuration
Modify default. conf: vi/etc/nginx/conf. d/default. conf and add the following Configuration:
1 # All js and css-related static resource file requests are processed by Nginx 2 location ~. *\. (Js | css) ${3 root/opt/static-resources; # specify the file path 4 expires 12 h; # The expiration time is 12 hours 5} 6 # requests for all images and other multimedia-related static resource files are 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) ${8 root/opt/static-resources; # specify the file path 9 expires 7d; # The expiration time is 7 days 10}
5. Modify SELinux security rules
If the 502 Bad Gateway error occurs during Nginx access, it may be because SELinux on the Nginx host restricts its http access permission. Enter the setsebool-P httpd_can_network_connect 1 command to enable the permission.
Complete configuration of the file/etc/nginx. conf is 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 15 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"';23 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 }
Complete configuration of the file/etc/nginx/conf. d/default. conf is 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;24 }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.html34 #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:8041 #42 #location ~ \.php$ {43 # proxy_pass http://127.0.0.1;44 #}45 46 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:900047 #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 root57 # concurs with nginx's one58 #59 #location ~ /\.ht {60 # deny all;61 #}62 }
(Tip: if you do not have the root permission when executing a command, add sudo to the command)
For more Nginx tutorials, see the following:
Deployment of Nginx + MySQL + PHP in CentOS 6.2
Build a WEB server using Nginx
Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5
Performance Tuning for Nginx in CentOS 6.3
Configure Nginx to load the ngx_pagespeed module in CentOS 6.3
Install and configure Nginx + Pcre + php-fpm in CentOS 6.4
Nginx installation and configuration instructions
Nginx log filtering using ngx_log_if does not record specific logs
Nginx details: click here
Nginx: click here
This article permanently updates the link address: