In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to achieve dynamic and static separation and load balancing in Nginx. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Network topology
2. Environmental preparation
We have 7 servers, 2 MySQL databases for high availability, 4 Web servers, including 2 Nginx+PHP installed for dynamic page load balancing, 2 Apache+PHP installed for static page load balancing, and 1 Nginx server for reverse proxy server.
As for the installation and deployment of the server environment, there is no operation here. If you have any questions, you can leave a message or check the previous article.
3. Modify the Nginx service configuration file in the Nginx server
Modify the http section
[root@nginx ~] # vim / usr/local/nginx/conf/nginx.conf
1. Redefine the logging format of Nginx service. Bosses can refer to the official website for the definition of keywords.
2. Use the upstream keyword to define the back-end server
Format: uptream + backend node group name (customizable)
Server + IP address + weight + maximum number of failures + failure timeout means that when the backend node is unable to connect within 3 seconds twice in a row, the connection to that node will be abandoned and Nginx will send the client request to the next node.
Modify the server section, which is configured as a Nginx virtual machine
[root@nginx ~] # vim / wdata/nginx/config/80.conf
1. Configure Nginx location matching
Configure location rules for files with the extension .html,. Jpeg,. Jpg,. Gif,. Png,. Php, respectively.
Proxy_set_header: allows you to redefine or add fields to the request header passed to the proxy server. This value can contain text, variables, and combinations of them. In this case, the real client address is added to the request header of the proxy server.
Proxy_pass: sets the connection of the reverse proxy in the format of: proxy_pass + http:// backend node group name;.
X-Forwarded-For: XFF header for short, which represents the real IP of the client, that is, the request side of HTTP. This item is added only when the HTTP proxy or load balancer server is passed.
$remote_addr: remote address, that is, client IP address
2. Configure the site log, where the log format is specified as the main rule that we configured in the HTTP section.
Please refer to the figure to modify the configuration of Nginx reverse proxy. For other detailed configuration parameters, please refer to the official website and continue to update later.
4. Backend Nginx service node configuration
The back-end Nginx server node is mainly used to access site files and picture files ending with the .php extension, so as to separate the image resources so as to reduce the server load.
1. Configure a site server with a .php extension
[root@weba config] # vim 80.conf
As shown above:
A, listen: configure the listening port, which is configured as port 80 here. In the production environment, we may configure port 443.
Server_name: configure the domain name. This is the experimental environment, so the default domain name is not configured.
Index: configure supported file types
Root: configuring the global site path
B. Configure the local site path, the address port of fastcgi (which refers to the listening address and port of php-fpm), the supported file types, etc.
C. Configure the log file
The Nginx site is roughly configured with this configuration and is basically accessible. If you need more complex configuration in the enterprise, please refer to the official documentation.
2. Configure a separate picture resource site
[root@weba config] # vim 81.conf
As shown above:
The configuration is exactly the same as the configuration of 1, just modify the path of the image resources.
The configuration of WebA and WebB is exactly the same. Finally, after modifying the configuration, don't forget to refresh it. Use the command: service nginx reload.
5. Backend Apache service node configuration
1. The configuration of Apache service node is basically the same as that of Nginx. Here, we use commands to modify the Apache configuration file.
[root@webc ~] # sed-I "s#/home/wwwroot/default#/wdata/http/www#g" / usr/local/apache/conf/httpd.conf [root@webc ~] # sed-I "s#/home/wwwroot/default#/wdata/http/www#g" / usr/local/apache/conf/extra/httpd-vhosts.conf [root@webc ~] # sed-I "s#/var/log/wwwlogs#/var/log/wwwlogs#g" / usr/local/apache/conf/httpd.conf [root@webc ~] # sed-I "s#/var/log/wwwlogs#/var/log/wwwlogs#g" / usr/local/apache/conf/extra/httpd-vhosts.conf [root@webc ~] # sed-I "s#/var/www#/wdata/http/www#g" / usr/local/apache/conf/extra/httpd-vhosts.conf
Executed in WebC and WebD respectively, the root directory and log directory of the website are mainly set.
2. Modify the port and listening address of Apache
[root@webc ~] # vim / usr/local/apache/conf/httpd.conf
Find the Listen line and change it to 0.0.0.0purl 80
As shown in the figure:
3. Modify the Apache log file format to make it easier for us to read the Apache log, or keep the default.
Find the line and add it under the comment below
LogFormat "% a -% A% t\"% B\ "% b\"% {Foobar} C\ "\" f\ "-% h -% H\"% {Foobar} I\ "% m\"% {Foobar} n\ "\"% {Foobar} o\ "-\"% p\ "-\" P\ "% Q% r% s\"% T\ "% u\\" "% U\"\ "% v\"\ "V\"% X "main
As shown in the figure:
As for the meaning of the added parameters, you can refer to the official Apache documentation.
At this point, the Apache configuration is complete, and there are also some permissions configuration for Apache. No special instructions will be made here, and then updates will be made later.
Start the httpd service:
Service httpd start
Add or upload site files
1. Add the index.php file to the root directory of the website in WebA, as follows:
This is a PHP page Real ServerA
This is a PHP page!
2. Add the index.php file to the root directory of the website in WebB, as follows:
This is a PHP page Real ServerB
This is a PHP page!
Note: here, we do not strictly follow the PHP file format to write, but simply to verify the load balancing and reverse proxy to write the PHP file.
3. Upload 1.jpg, 2.jpg, 3.png and 4.gif files in the image (image resource directory) directory of WebA and WebB, as shown in the figure:
4. Add the index.html file to the root directory of the WebC site, as follows:
This is a HTML page Real ServerC
This is a HTML page!
5. Add the index.html file to the root directory of the WebD site, as follows:
This is a HTML page Real ServerD
This is a HTML page!
After the file is added and uploaded, let's verify whether the static and dynamic separation and load balancing we configured are successful.
Verify static and dynamic separation and load balancing
1. Open http://192.168.20.138/index.php in browsing, and the effect is as follows:
2. In the browser http://192.168.20.138/index.html, the effect is as follows:
3. View the Nginx log of Nginx load balancer.
Access to the http://192.168.20.138/index.php log is shown below:
As you can see in the figure, when we visit http://192.168.20.138/index.php, the server will send client requests to WebA and WebB in the order or weight of the backend node group we configured. When there are image requests in the web page, they will also visit the image nodes of WebA and WebB in order.
Access to the http://192.168.20.138/index.html log is shown below:
As shown in the figure, when we visit http://192.168.20.138/index.html, the request for html will be sent to the WebC and WebD node services in turn, while for the access to image resources, the request will be sent to two image servers.
The above content is how to achieve dynamic and static separation and load balancing in Nginx. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.