Home Backend Development PHP Tutorial nginx+keepalived high availability load balancing

nginx+keepalived high availability load balancing

Aug 08, 2016 am 09:27 AM
keepalived nbsp proxy quot

Not much to say, nginx installation and configuration, as well as load balancing, you can read another article I wrote "nginx Load Balancing in Practice", and about load balancing, you can read another article I wrote Two articles, one is "lvs+keepalived load balancing" and the other is "haproxy+keepalived load balancing". For the difference between the three load balancing, you can take a look at an article I reprinted "Software Level Load Balancer (LVS)" /HAProxy/Nginx), let’s go directly to the configuration steps:

1. System environment

[plain] view plaincopy

  1. System version: CentOS release 5.9 (Final) x86 32-bit
  2. nginx version: 1.2.8
  3. keepalived version: 1.2 .4
  4. main keepalived: 192.168.207.130
  5. from keepalived:192.168.207.131
  6. VIP: 192.168.207.140
  7. WEB_1:192.168.207.129 Port 80
  8. WEB_2 :192.168.207.130 8080 port
  9. WEB_3:192.168.207.131 8080 port

2. Custom nginx configuration file


at 192.168.207 Operating on .130 and 192.168.207.131

[plain] view plaincopy

  1. useradd nginx
  2. vi /usr/local/nginx/conf/nginx.conf

The content is as follows:

[plain] view plaincopy

  1. #Run user                                                                                                                                                                              
  2. #Global error log and PID file
  3. error_log logs/error. log notification; epoll;
  4. worker_connections 1024; #So The total number of connections supported by nginx is equal to worker_processes * worker_connections
  5. }
  6. #Set the http server and use its reverse proxy function to provide load balancing support
  7. http {
  8. #Set the mime type
  9. include mime.types ; #This is about nginx Which multimedia types are supported? You can go to conf/mime.types to check which multimedia are supported.
  10. default_type application/octet-stream; #Default data type
  11. #Set log format
  12. Log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" '​
  13. ​ '"$gzip_ratio"' ; ; ent ' '
  14. '"$http_referer" "$http_user_agent" '
  15. '"$http_range" "$sent_http_content_range"';
  16. large_client_header_buffers 4 4k;
  17. #Enable gzip module
  18.       #gzip on;                                                  gzip_types text/plain;
  19. #output_buffers 1 32k;
  20. #postpone_output 1460;
  21. #Setting access log
  22. access_log logs/access.log main;
  23. client_header_timeout 3m;
  24. client_body_timeout 3m;
  25. send_timeout 3m;
  26. sendfile on;
  27. tcp_nopush on;
  28. tcp_nodelay on;
  29. keepalive_timeout 65;
  30. #Set the load balancing server list
  31. upstream mysvr {
  32. #weigth parameter representation Weight, the higher the weight, the greater the chance of being assigned                                                                                                                                                                            weight=5; . Server 192.168.207.131:8080 weight = 5;
  33. }
  34. server {#This is set to set web service, monitor port 8080
  35. Listen 8080;
  36. server_name 192.168.207.131; #This changes according to the system ip
  37.                                                   ‐ ’ s ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐              page 500 502 503 504 /50x.html;
  38. # location = /50x.html {
  39. html;
  40.         #Set up virtual host                                                              A Server_name 192.168.207.140;#Here is VIP
  41. #Charset GB2312;
  42. #Set the visits log of this virtual host
  43. access_log Logs/Threeb.access.log Main;
  44.           #If you access /img/*, /js/*, /css/* resources, fetch local files directly, not through squid                                                   The cache effect is better l #Location ~^/(IMG | JS | CSS)/{
  45. #root/data3/html;
  46. #Expires 24h;
  47. #}#"/" Enable load balancing
  48. local/{
  49. proxy_pass http:// mysvr;#
  50.               proxy_set_header Host $host;                                             proxy_set_header                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_ size 128k;
  51.            proxy_connect_timeout 90;                 proxy_read_timeout 90;                        proxy_buffer_size                             proxy_busy_buffers_size 64k;
  52.                                                                                                                                                  
  53. stub_status on;
  54. access_log on;
  55.                  auth_basic "NginxStatus";                                                                                                                                                             auth_basic    "NginxStatus";                                                                                                                                                                .Customize keepalived configuration file
  56. [plain] view plaincopy
  57. vi /etc/keepalived/keepalived.conf
  58. The content is as follows:
  59. [plain] view plaincopy
    1. global_defs {
    2. notification_email {
    3. root@localhost.localdomain }
    4. notification_email_from notify@keepalived.com
    5. smtp_server 127.0.0.1
    6. smtp_connect_timeout 30
    7. router_id LVS_DEVEL
    8. }
    9. vrrp_script chk_http_port {
    10.                script "/etc/keepalived/check_nginx.sh"                                                                                                                                        ##Monitoring time
    11.                                                                                                                                                                                                          Monitoring time VI_1 {
    12.                                                                                                     ,                    ### Monitoring network card
    13.                                                                     ’ s ‐         ‐   ‐ ‐ out of 101                                      The weight value MASTRE must be higher than BAUCKUP
    14. Auth_pass 1111}}
    15. track_script {
    16. chK_HTTP_PORT ###}}}
    17. virtual_ipaddress {
    18. 192.168.207.140 ### vip address
    19. View plaincopy
      1. vi /etc/keepalived/check_nginx.sh

      The content is as follows:

      [plain] view plaincopy

      1. !/bin/bash
      2. A = `ps -C nginx --no-header |wc -l`
      3. if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx
      4. if no process is worth zero
      5.                                                                                                                                                                                    sleep                                                                                 sleep                              -no-header |wc -l` -eq 0 ];then
      6.                                                                                                                                                                                                    
      7.                                                                                        Here is to check whether nginx has started. If it has not started, start nginx first. If it has not started after 3 seconds, the keepalived process will also be closed, so that keepalived can take over and provide high availability. Here, The keepalived service provides high availability, while nginx provides load balancing of back-end web servers.
      8. You also need to add execution permissions to the script, as follows
      9. [plain] view plaincopy
      10. chmod +x /etc/keepalived/check_nginx.sh
      5. Start the service and test
      Let me talk about it first. On WEB_1, I use the system auto I brought the apache web server last night, which is more trouble-free. In this case, I only need to start the master-slave keepalived, because it will use the check_nginx.sh script to automatically start nginx. Everything is started.
      Visit http://192.168.207.140 to access the content of the three back-end web servers in rotationHere we turn off the main keepalived service to test high availabilityThen it will be in /var/log/ on the slave keepalived server messages saw a log like this

      [plain] view plaincopy
      1. Apr 19 17:42:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE

      Apr 19 17:42:45 localhost Keepalived_vrrp : VRRP_Instance(VI_1) Entering MASTER STATE



      Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.



      Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140

      Apr 19 17:42:45 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 added

      1. Apr 19 17:42:45 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168. 207.140 added Apr 19 17:42:45 localhost avahi-daemon[4204]: Registering new address record for 192.168.207.140 on eth0.

      2. Continue to visit http://192.168.207.140, you can still access the three back-end web servers
        Then open the original owner keepalived, you can observe the log display of the original slave keepalived server

        [plain] view plaincopy

        1. Apr 19 17:42:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140
        2. Apr 1 9 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
        3. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
        4. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance (VI_1) removing protocol VIPs.
        5. Apr 19 17:44:06 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 removed
        6. Apr 19 17:44:06 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168 .207.140 removed
        7. Apr 19 17:44:06 localhost avahi-daemon[4204]: Withdrawing address record for 192.168.207.140 on eth0.

        It means that the original master-slave result has been restored.
        In the production environment, the back-end machine may also hang up, but you don’t have to worry about it, nginx will automatically allocate the session to a good back-end web server

        ok, here you go It's all over, practice personal testing, I wish you success

        From: http://blog.csdn.net/zmj_88888888/article/details/8825471

        The above has introduced nginx+keepalived high-availability load balancing, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

Nginx Proxy Manager configuration analysis and optimization Nginx Proxy Manager configuration analysis and optimization Sep 26, 2023 am 09:24 AM

Overview of NginxProxyManager configuration analysis and optimization: NginxProxyManager is a reverse proxy management tool based on Nginx, which can help us easily configure and manage reverse proxy servers. In the process of using NginxProxyManager, we can improve the performance and security of the server by parsing and optimizing its configuration. Configuration analysis: Configuration file location and structure: NginxProxyManag

See all articles