This article is reproduced from: http://os.51cto.com/art/201111/304608.htm
This article introduces the final realization of load balancing through the cluster configuration of nginx and tomcat in Windows xp and ubuntu environments .
AD At the beginning, I had a small question: why not use open source apache and Nginx software loads? F5 equipment often costs hundreds of thousands and is expensive? I had a relatively naive question, and I later understood: F5 operates on the transport layer of the IOS network model, and Nginx and apache are based on the http reverse proxy method and are located in the seventh application layer of the ISO model. To put it bluntly, the difference between TCP UDP and http protocols is that Nginx cannot provide load balancing for applications based on TCP protocols. Understood the differences and application scenarios between the two, and developed a strong interest in Nginx. I read Zhang Yan’s "Practical Nginx" (this young and promising young man in 1985 was envious + jealous), and understood the general principles and Configuration, Ubuntu10.10, tried to configure Nginx+tomcat load balancing under window, forwarded all requests to tomcat, and did not configure static, dynamic separation, image hotlink prevention and other configurations.Nginx Introduction
Nginx (pronounced the same as engine x) is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server, and is released under a BSD-like protocol. Its characteristics are that it occupies less memory and has strong concurrency capabilities. In fact, nginx’s concurrency capabilities do perform better among web servers of the same type. Currently, the users of nginx websites in mainland China include: Sina, NetEase, Tencent, and other well-known micronets Plurk also uses nginx.The above is an introduction to Nginx, which is basically nonsense. Now let’s get to the point. A combination of pictures and text will show the basic configuration, first the window environment, and secondly the Ubuntu environment (Vbox virtual).
Window xp environment: Nginx+Tomcat6
1. Download address
2. Directory structure
Log directory “ nginx.exe main program
It is extremely simple to install Nginx under the window. Just unzip it to an English directory without spaces (personal habit, worry about problems with Chinese). Double-click nginx to start. Here I installed it to: D:server directory, which is covered below. The tomcat is also installed in this directory.
If you want to stop nginx, run the command in dos environment: nginx -s stop
3. nginx.conf configuration
The Nginx configuration file is in the conf directory by default. The main configuration file is nginx.conf. We install it in D:servernginx-0.8.20, the default main configuration file is D:servernginx-0.8.20nginx.conf. The following is the configuration of nginx as a front-end reverse proxy server.
Nginx.conf code
- #The user and group used by Nginx are not specified under window
- #user niumd niumd;
- #The number of working sub-processes (usually equal to the number of CPUs or 2 times the CPU)
- worker_processes 2;
- #Error log storage path
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- error_log logs/error.log info;
- #Specify pid to store files
- pid Logs/nginx.pid;
- events {
- #When using the network IO model, Linux recommends epoll, FreeBSD recommends kqueue, and it is not specified under window. P #USE EPOLL;
- #Allow the maximum number of connections
- worker_connections 2048
- ;
- #Define log format
- #log_format main
- '$remote_addr - $remote_user [$time_local] $request ' " #
- '" $ Status "$ BODY_BYTES_SENT" $ http_referr "' " #
- '" $ http_user_agent "" $ http_x_Forwardeded_For "' ;
- #access_log off; access_log logs/access.log;
- client_header_timeout 3m;
- client_body_timeout 3m; send_timeout 3m;
- client_header_buffer_size 1k;
- large_client_header_buffers
- 4 4k;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- #keepalive_timeout 75
- 20 ;
- include gzip.conf;
- upstream localhost {
- #Allocate requests to each backend tomcat based on ip calculation. Many people mistakenly believe that it can solve the session problem, but it does not.
- #In the case of multiple networks, the IP may be different when routing is switched
- #ip_hash; }
- server {
- Listen proxy_connect_timeout 3 ;
- proxy_send_timeout
- 30 ;
- proxy_read_timeout 3 0
- ; proxy_pass http://localhost; }
- proxy_set_header Host $host;
- proxy_set_header ;
- proxy_send_timeout
- 300; proxy_read_timeout
- 300; proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
- gzip compression related configuration is as follows:
-
Gzip.conf code:
gzip_min_length 1000
;- gzip_types text /plain text/css application/x-javascript;
- 4. Tomcat configuration
- Everyone is familiar with tomcat. You only need to modify the server.xml configuration file. Here we use apache-tomcat-6.0.14 For example, decompress and name them in the server directory: apache-tomcat-6.0.14_1, apache-tomcat-6.0.14_2.
- The first port modification:
- Xml code
- Serverport
- = "18006"
- shutdown= "SHUTDOWN"
- >
The second port modification:
Xml code
- Connector
- port
"18081"protocol
="HTTP/1.1"
connectionTimeout ="20000"
CRedirectport- =
- "8443" /& gt;
- Third port modification: java code
"8009" protocol = "ajp/1.3 1.3 "
redirectPort=- "8443"
- />
- Engine element adds jvmRoute attribute: Xml code
Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat1">
Don’t duplicate the two tomcat ports. To ensure that it can be started, the other tomcat configuration is omitted and Seagate is omitted, and the listening port is 18080. We will upload all the configuration information in the attachment.
5. Verify configuration and test load balancing
First test whether the nginx configuration is correct, test command: nginx -t (default verification: confnginx.conf), you can also specify the configuration file path.
Secondly, verify tomcat and start two tomcats. If there is no port conflict, it is considered successful (I won’t talk nonsense about the java that tomcat relies on and so on);
Finally, verify the configuration of load balancing settings, http:// localhost/ or http://localhost/index.jsp. I modified the index.jsp page and added log output information for easier observation. Note: On the head of the kitten in the upper left corner: access tomcat2, access tomcat1. It means that different tomcats are accessed.
This completes the nginx+tomcat load balancing configuration under window. Regarding tomcat Session, memcached is usually used, or nginx_upstream_jvm_route is used. It is an Nginx extension module used to implement the Cookie-based Session Sticky function. If there are too many tomcats, session synchronization is not recommended. Synchronizing sessions between servers consumes resources, and high concurrency environments can easily cause session storms. Please adopt the session solution reasonably according to your own application situation.
Ubuntu10.10 environment: Nginx+Tomcat6
Let’s briefly talk about how to install and configure under ubuntu10.10
1. Download Nginx
Address: http://nginx.org/ en/download.html, linux version: nginx-0.8.20.tar.. Decompression command:
tar -zxvf nginx-0.8.20.tar.gz
2. Compile and install NginxNginx depends on some other PCRE, openssl (depends on libssl-dev), and my notebook Ubuntu environment has been installed For PCRE, you only need to install the dependent openssl. Let’s briefly talk about how to install PCRE and openssl, etc.
PCRE download address: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
Shell code
- tar zxvf pcre-8.01.tar.gz
- cd pcre-8.01
- sudo ./configure
- sodu make
- sodu make install
openssl via apt-get install Install
- sudo apt-get install openssl
- sudo apt-get install libssl-dev
- //If other packages are missing, please use this method to install. Ubuntu has a dependency prompt
The dependent software packages are installed, below To compile Nginx:
Shell code
- #Copy the window shared directory software to the current working directory
- cp /mnt/fileshare/nginx-0.8.20.tar.gz ./
- #Unzip the software package
- tar zxvf nginx-0.8.20.tar.gz
- cd nginx-0.8.20
- //Compile source code, use nobody by default, specify existing users and groups on this machine, enable nginx-status function, and monitor nginx status. Start debug
- sudo ./configure --user=niumd --group=niumd --with-debug --with-http_stub_status_module
- sudo make
- sudo make install
The compilation and installation ends correctly. Check the default configuration according to the above method under the window, then start nginx under the default configuration, visit http://127.0.0.1
Nginx configuration After success, we will make few modifications to nginx.conf under window, as follows:
Ubuntu nginx.conf code
- #User and group used by Nginx
- user niumd niumd;
- #The number of worker sub-processes (usually equal to the number of CPUs or 2 times the CPU)
- worker_processes 2;
- #Error日志存放路径
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- error_log logs/error.log info;
- #指定pid存放文件
- pid logs/nginx. pid;
- events {
- #Use network IO model Linux recommends epoll, FreeBSD recommends kqueue
- Use epoll;
- #Maximum number of connections allowed
- worker_ connections 2048
- ; }
- html user [$time_local] $request ' " #
- '" $ Status "$ BODY_BYTES_SENT" $ http_referr "' " #
- '" $ http_user_agent "" $ http_x_Forwardeded_For "' ;
- #access_log off;
- access_log logs/access.log;
- client_header_timeout 3m;
- client_body_timeout 3m; send_timeout 3m;
- client_header_buffer_size 1k;
- large_client_header_buffers
- 4 4k;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- #keepalive_timeout
- 75
- 20;
- include gzip.conf;
- upstream localhost {
- #ip_hash
- #ip_hash;
- server localhost:
- 18081; using using using using using out out out out out out of ’ s to ’ ’s ’ ‐ ‐ ‐ ‐ ‐ server { location proxy_connect_timeout 3 ;
- proxy_send_timeout
- 30 ; proxy_read_timeout
- 30 ;
- proxy_pass http://localhost;
- users and groups;
- 3. Configure tomcat
- Please refer to the configuration under window, which is exactly the same.
- 4. Start and stop nginx
- Starting nginx under ubuntu is slightly different from that in window. The general starting and stopping method is as follows.
- Java code
- #nginx目录执行
- sbin/nginx
- 或通过-c 指定配置文件
- sbin/nginx -c usr/local/nginx8.20/conf/nginx/conf
- niumd@niumd-laptop:/usr/local/nginx$ pwd
- /usr/local/nginx
- niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -t
- the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- configuration file /usr/local/nginx/conf/nginx.conf test is successful
- niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -v
- nginx version: nginx/0.8.20
- niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -V
- nginx version: nginx/0.8.20
- built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
- configure arguments: --user=niumd --group=niumd --with-debug --with-http_sub_module
- niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx
- niumd@niumd-laptop:/usr/local/nginx$ ps -ef|grep nginx
- root 5158 1 022:32 ? 00:00:00 nginx: master process sbin/nginx
- niumd 5159 5158 022:32 ? 00:00:00 nginx: worker process
- niumd 5161 1577 022:32 pts/0 00:00:00 grep --color=auto nginx
- niumd@niumd-laptop:/usr/local/nginx$
Shell代码
我们通过ps -ef|grep nginx,看到如下结果:
注意:在启动时linux提示一句警告【warn】……,是因为我们设置的 #允许最大连接数 worker_connections 2048,超过linux默认1024的限制。
停止:kill -信号类型 pid
nginx/logs目录下有个nginx。pid的文件,此文件记录了每次运行的pid,也可以通过ps命令查询。
信号类型如下:
信号类型 描述 RERM.INT 快速关闭 HUP 平滑重启,加载配置 USR1 重新加载日志 USER2 平滑升级执行程序 WINCH 从容关闭工作进程 QUIT 从容关闭 以上就介绍了Nginx+tomcat配置集群,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version
