1. 程式人生 > >Centos7.4 Nginx反向代理+負載均衡配置

Centos7.4 Nginx反向代理+負載均衡配置

cti war RM image rom 使用 CA 二級域名 orm

Ningx是一款高性能的HTTP和反向代理服務器,配置起來也比較簡單。

測試環境:

  172.16.65.190  Nginx-反向代理

  172.16.65.191  Ningx-Web

  172.16.65.192  Nginx-Web

在三臺Server安裝Nginx:

# yum install -y nginx

在172.16.65.190配置Nginx反向代理+負載均衡:

# vim /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load 
dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
include /etc/nginx/conf.d/*.conf;
# 後端Web服務器,默認使用輪詢機制 upstream proxy_test { server 172.16.65.191:80 weight=1; server 172.16.65.192:80 weight=1;
ip hash;   } server { listen 80;  # 監聽的端口 server_name www.test.com;  # 監聽的域名 location /abc/ {  # 監聽域名的二級域名 proxy_pass
http://proxy_test/; # 這裏的proxy_test和上面的upstream proxy_test對應 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
# nginx -s reload    # 重新加載Nginx配置文件

後端Web配置

默認頁面路徑 /usr/share/nginx/html/index.html

在172.16.65.191 配置默認頁面內容為Server001,啟動Nginx

在172.16.65.192 配置默認頁面內容為Server002,啟動Nginx

測試效果

在Client配置Hosts解析www.test.com

第一次訪問

技術分享圖片

第二次訪問

技術分享圖片

Centos7.4 Nginx反向代理+負載均衡配置