回答

收藏

nginx负载均衡配置,服务器宕机,自动切换到另一个服务器

系统运维 系统运维 28 人阅读 | 0 人回复 | 2024-03-29

本帖最后由 求知者 于 2024-3-29 23:38 编辑

nginx实现双机部署,一个服务器宕机,自动切换到另一个服务器
nginx.conf配置如下:

  1. #user  nobody;
  2. worker_processes  1;

  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;

  6. #pid        logs/nginx.pid;


  7. events {
  8.     worker_connections  1024;
  9. }


  10. http {
  11.     include       mime.types;
  12.     default_type  application/octet-stream;
  13.         client_max_body_size 1000m;

  14.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  15.     #                  '$status $body_bytes_sent "$http_referer" '
  16.     #                  '"$http_user_agent" "$http_x_forwarded_for"';

  17.     #access_log  logs/access.log  main;

  18.     sendfile        on;
  19.     #tcp_nopush     on;

  20.     #keepalive_timeout  0;
  21.     keepalive_timeout  65;

  22.     #gzip  on;
  23.    
  24.     upstream myserviceAPI{
  25.       ip_hash;
  26.       server 192.168.1.2:8011;
  27.       server 192.168.1.3:8011;
  28.     }

  29.     server {
  30.         listen       8080  default;
  31.         server_name  192.168.1.1;

  32.         if ($http_Host !~* "^(192.168.1.1:8080)$" )
  33.         {
  34.             return 403;
  35.         }

  36.         #
  37.         location /service {
  38.             proxy_pass http://myserviceAPI/service;
  39.             proxy_connect_timeout   1;
  40.             add_header Content-Security-Policy: default-src=self;
  41.                 add_header X-Xss-Protection: 1;
  42.                 add_header X-Xss-Protection: mod=block;
  43.                 add_header X-Content-Type-Options: nosniff;
  44.         }
  45.     }
  46. }


复制代码


"打赏他人,曝光自己,利他利我"
还没有人打赏,支持一下
求知的路上,无穷无尽
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则