初探nginx负载均衡配置

只简单说一下upstream的配置,如何进行负载均衡后续还需要多了解

1.另准备一个配置文件命名为nginx_test.conf

为了不污染原有的nginx.conf,提前复制一份配置文件做试验,然后启动nginx时加载nginx_test.conf

启动命令:

[root@localhost conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx_test.conf

-c参数表示加载指定的配置文件,后面跟的是文件路径

2.在http模块下添加upstream配置

    upstream tomcat
    {
      server localhost:8080;   # 对应server下location中添加的 proxy_pass
    } 
    upstream ApprPhD
    {
       server 192.168.0.1XX:3030;
    }
server {
       listen   80;
       server_name  hanmk.com;
       location / {
       root /tmp/data/;
       autoindex on;
}
}
    server {
        listen    80 default_server;
        server_name demo.com ;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        # location / {
        #    root   html;
        #   index  index.html index.htm;
        # }

        location / {
        proxy_pass http://tomcat;   # 对应upstream为tomcat,访问本地tomcat
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}

       location /jenkins {            
              proxy_pass  http://tomcat;  #对应upstream为tomcat,访问本地tomcat中部署的jenkins

              proxy_set_header        Host $host;
              proxy_set_header        X-Real-IP $remote_addr;  #获取真实ip
              proxy_connect_timeout   90;
              proxy_send_timeout      90;
              proxy_read_timeout      90;
              proxy_buffer_size       4k;
              proxy_buffers           4 32k;
              proxy_busy_buffers_size 64k;
              proxy_temp_file_write_size 64k;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#获取代理者的真实ip
              proxy_redirect          off;
       }
       location /ApprPhD {          
              proxy_pass http://ApprPhD;  # 对应upstream为ApprPhD,访问1XX服务器部署的ApprPhd
              proxy_set_header        Host $host;
              proxy_set_header        X-Real-IP $remote_addr;  #获取真实ip
              proxy_set_header REMOTE-HOST $remote_addr;
              proxy_connect_timeout   90;
              proxy_send_timeout      90;
              proxy_read_timeout      90;
              proxy_buffer_size       4k;
              proxy_buffers           4 32k;
              proxy_busy_buffers_size 64k;
              proxy_temp_file_write_size 64k;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#获取代理者的真实ip
              proxy_redirect          off;
       }

    }
原文地址:https://www.cnblogs.com/hanmk/p/9304252.html