nginx反向代理

技术调研,浅浅学习一下。

需求分析

需求:主备两个集群,对外要提供一个vip供访问;同一时间只会访问一个集群,主挂了切备提供服务。
分析:

  1. vip方案:直接手动配也行,keepalived也行,heartbeat也行
  2. 不是两个服务器,是两个集群(不确定的服务器数量,不确定谁提供服务),手动配置和keepalived都不知道要配到哪个机器上
  3. 只能搞一个新的服务器做这个事。装nginx做反向代理。

反向代理,就是nginx服务器对外提供一个ip供访问,然后nginx做代理,确定转发给哪个服务器集群。

网络环境配置

本地模拟搞的简单些,搞了两个虚机做服务器。一个是nginx,另一个其实应该用几台做集群,对外用keepalived或者其他提供一个vip,不过我感觉可以用一个虚机装docker服务模拟,对外提供一个虚机ip。
虚机1:
对外ip我用了nginx服务器网卡ip(ens41:192.168.2.15)

ens33: flags=4163  mtu 1500
        inet 192.168.1.15  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fe87:686d  prefixlen 64  scopeid 0x20
        ether 00:0c:29:87:68:6d  txqueuelen 1000  (Ethernet)
        RX packets 4479509  bytes 4944774197 (4.9 GB)
        RX errors 0  dropped 1  overruns 0  frame 0
        TX packets 1462946  bytes 474403056 (474.4 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        
ens41: flags=4163  mtu 1500
        inet 192.168.2.15  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::20c:29ff:fe87:6895  prefixlen 64  scopeid 0x20
        ether 00:0c:29:87:68:95  txqueuelen 1000  (Ethernet)
        RX packets 4636  bytes 2347762 (2.3 MB)
        RX errors 0  dropped 132  overruns 0  frame 0
        TX packets 2412  bytes 1805264 (1.8 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

虚机2:
docker集群都搭在了这台服务器上,集群对外ip就是服务器网卡ip,和nginx服务器ens33网卡相通。

ens33: flags=4163  mtu 1500
        inet 192.168.1.9  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fe81:d07d  prefixlen 64  scopeid 0x20
        ether 00:0c:29:81:d0:7d  txqueuelen 1000  (Ethernet)
        RX packets 4092821  bytes 2470718161 (2.4 GB)
        RX errors 0  dropped 7821  overruns 0  frame 0
        TX packets 4224258  bytes 2537064427 (2.5 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

安装nginx

ubuntu系统,直接apt

apt update
apt install nginx
nginx -V
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-YlUNvj/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module

--with-stream=dynamic

nginx配置

服务不仅是http(暴露端口号1),还有ssh,tcp/ssl(暴露端口号2),所以配置上使用了tcp。

  1. 修改配置文件,增加stream
vim /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}
# 增加begin
stream
{
    upstream server_upstreams {
        server 192.168.1.9:暴露端口号1;
    }
    server {
        listen 暴露端口号1;
        proxy_pass server_upstreams;
    }
    upstream server_upstreams_1 {
        server 192.168.1.9:暴露端口号2;
    }
    server {
        listen 暴露端口号2;
        proxy_pass server_upstreams_1;
    }
}
# 增加end
http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

  1. 启动nginx
    apt安装的nginx,启动路径在usr/sbin下,指定使用的配置文件启动。
/usr/sbin# ./nginx -c /etc/nginx/nginx.conf

测试

可通过postman访问http url(ip+端口是192.168.2.15:暴露端口号1),测试可GET到内容。
TCP连接也通过92.168.2.15:暴露端口号2去建立,可正常建立连接。

停掉nginx服务

/usr/sbin# ./nginx -s stop

(如果是浏览器访问就等待一会,有缓存)再重复上面步骤,都不通了

后端服务切换

手动切换:准备主备两个conf文件。要切换的时候停nginx,换用另一个conf去启动。

nginx 代理tcp资料

  1. NGINX 实现TCP反向代理 主要参考的这个
  2. 使用 Nginx 实现 TCP 四层反向代理
  3. 解决Nginx代理TCP获取不到客户端真实IP的问题

你可能感兴趣的:(中间件等,nginx,linux,运维)