Nginx-proxy
Nginx实现代理功能:
基本环境:
nginx+{web1,web2}
实现目的:
客户端访问nginx的IP地址,实现访问到后台web1或者web2服务上的网站服务。
配置过程:
1)修改nginx配置文件,实现代理服务【注释原有的location /】
vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
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;
include /etc/nginx/conf.d/*.conf;
upstream httpd_server { #增加
server 192.168.10.10:80 weight=1; #增加
server 192.168.10.20:80 weight=1; #增加
} #增加
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html/;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / { #增加
proxy_pass http://httpd_server; #增加
proxy_set_header Host $host; #增加
} #增加
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location /status {
stub_status on;
access_log off;
}
}
}
2)构建后台节点服务器
192.168.10.10;192.168.10.20
两台主机分别安装httpd服务,并开启httpd,服务
yum install -y httpd
systemctl start httpd
3)验证结果
在客户端浏览器中:http://nginx代理服务器的IP地址
http://192.168.10.30
4)权重值weight的设置
当后台节点服务器性能相当时,设置相同权重值;
当后台节点服务器性能差异时,根据性能的优劣设置权重值,性能高的应当设置较大的权重值