1. Introduction to Nginx
Nginx is a lightweight web server/reverse proxy server that occupies less memory and has strong concurrent capabilities. According to the official test, nginx can support 50,000 concurrent connections, and its resource consumption such as CPU and memory is very low, and its operation is very stable.
2. Usage scenarios
2.1. High concurrency scenarios
- load balance
-
Distribute the request/data [evenly] to multiple operation units for execution. The key to load balancing is [evenly]
2.2. Forward proxy
- The agent clarifies who the target of the buyer is, and the agent helps us to contact the target
2.3. Reverse proxy
- The client only needs to send the request to the proxy server, and the proxy server will forward the request to the internal server for processing, and return the result to the client after processing
- But in the whole process, the client and the actual processing server will not directly establish a connection
3. Nginx source code installation
4. Nginx load balancing
4.1. Cluster construction
4.2. Load strategy
-
4.2.1. Request Polling
- In turn forwarded to the configured server
-
4.2.2. Add weight
- Using server weights can further affect the nginx load balancing algorithm. The greater the weight, the more requests will be distributed.
- Say you can do it, you can do it or not
-
4.2.3. Minimum number of connections
- In the case of the least connection load, nginx will try to avoid distributing too many requests to busy application servers, but distribute new requests to less busy servers to avoid server overload.
-
4.2.4. IpHash
- Ensures that requests from the same client will always be directed to the same server, unless this server is unavailable
5. Static resources
5.1. Image bed
- Unified image management by external server
5.2. Configure static resources
6. Single Linux to build multiple Tomcats
6.1. Copy multiple
6.2. Environment variables
Set and execute source /etc/profile
#tomcat8080
export CATALINA_HOME=/opt/bdp/apache-tomcat-8080
export CATALINA_BASE=/opt/bdp/apache-tomcat-8080
export TOMCAT_HOME=/opt/bdp/apache-tomcat-8080
#tomcat18080
export CATALINA_HOME18080=/opt/bdp/apache-tomcat-18080
export CATALINA_BASE18080=/opt/bdp/apache-tomcat-18080
export TOMCAT_HOME18080=/opt/bdp/apache-tomcat-18080
6.3. server.xml file
vim apache-tomcat-18080/conf/server.xml
Only need to modify tomcat18080, 8080 remains unchanged
#22--默认为8005--》修改为18005
<Server port="18005" shutdown="SHUTDOWN">
#69--默认为8080--》修改为18080
<Connector port="18080" protocol="HTTP/1.1" connectionTimeout="20000"
redirectPort="8443" />
#116-默认为8009--》修改为18009
<Connector port="18009" protocol="AJP/1.3" redirectPort="8443" />
6.4. catalina.sh script
vim apache-tomcat-18080/bin/catalina.sh
Only need to modify Tomcat18080, modify catalina.sh
####################113行开始添加
export CATALINA_BASE=$CATALINA_BASE18080
export CATALINA_HOME=$CATALINA_HOME18080
export TOMCAT_HOME=$TOMCAT_HOME18080
6.5. Start Tomcat
./apache-tomcat-8080/bin/startup.sh
./apache-tomcat-18080/bin/startup.sh
7. Virtual Hosting
The virtual host uses special software and hardware technology. It divides a server host running on the Internet into multiple "virtual" hosts. Each virtual host can be an independent website with an independent domain name. With complete Internet server functions
(WWW, FTP, Email, etc.), the virtual hosts on the same host are completely independent. From the perspective of website visitors, each virtual host is exactly the same as a stand-alone host
Classification
- 1. Domain name-based virtual hosts, which distinguish virtual hosts through domain names
- 2. Port-based virtual hosts, distinguishing virtual hosts through ports
8. Session consistency
-
node distribution
-
Memory Database
-
install database
yum install memcached -y
-
Start the database service
-
systemctl status memcached systemctl start memcached systemctl enable memcached vim /etc/sysconfig/memcached
-
Tomcat
-
Copy the jar package to the lib directory of the current server
-
Pay attention to the version of memcached
-
If a machine has multiple tomcats, all sessions need to be consistent
- Configure tomcat/conf/server.xml
- If the same project wants to share the session, the jvmRoute needs to be named the same
- If you only have one tomcat, ignore this step
-
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm18080"> </Engine>
-
Configure tomcat/conf/context.xml
-
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.88.100:11211" sticky="true" lockingMode="auto" sessionBackupAsync="false" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" sessionBackupTimeout="1000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoT ranscoderFactory" />
-
Restart the Tomcat server
-
-
The visit found that the same server can share the session, but different servers are involved in cross-domain issues
- The session is still different, you need to set the domain of the cookie