初探nginx负载均衡配置
Posted 我是冰霜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初探nginx负载均衡配置相关的知识,希望对你有一定的参考价值。
只简单说一下upstream的配置,如何进行负载均衡后续还需要多了解
1.另准备一个配置文件命名为nginx_test.conf
为了不污染原有的nginx.conf,提前复制一份配置文件做试验,然后启动nginx时加载nginx_test.conf
启动命令:
[[email protected] 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.107:3030; }
server { listen 80 default_server; server_name demo.com ; #charset koi8-r;
location / { proxy_pass http://tomcat; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80 default_server; server_name demo.com ; #charset koi8-r; location / { proxy_pass http://tomcat; #对应上面upstream tomcat } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location /jenkins { #nodeJs服务器 proxy_pass http://tomcat; # 对应上面upstream tomcat 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 { #nodeJs服务器 proxy_pass http://ApprPhD; # 对应上面upstream为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; } }
以上是关于初探nginx负载均衡配置的主要内容,如果未能解决你的问题,请参考以下文章