实验跳转目录
反向代理:反向代理也叫reverse proxy,指的是代理外网用户的请求到内部的指定web服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。
Nginx除了可以在企业提供高性能的web服务之外,另外还可以将本身不具备的请求通过某种预定义的协议转发至其它服务器处理,不同的协议就是Nginx服务器与其他服务器进行通信的一种规范,主要在不同的场景使用以下模块实现不同的功能:
ngx_http_proxy_module: 将客户端的请求以http协议转发至指定服务器进行处理 |
---|
ngx_stream_proxy_module:将客户端的请求以tcp协议转发至指定服务器处理 |
---|
ngx_http_fastcgi_module:将客户端对php的请求以fastcgi协议转发至指定服务器助理 |
---|
ngx_http_uwsgi_module:将客户端对Python的请求以uwsgi协议转发至指定服务器处理 |
---|
实验准备:
主机及IP | 用途 |
---|---|
U8 :192.168.124.30 | NGINX代理服务器 |
U8-1 :192.168.124.31 | 后端web A Apache部署 |
U8-2 :192.168.124.32 | 后端web B Apache部署 |
U8-3 :192.168.124.33 | 后端web C Apache部署 |
部署后端Apache服务器:
[root@U8-1 ~]# apt -y install apache2
[root@U8-1 ~]# echo “U8-1 web,192.168.124.31” > /var/www/html/index.html
[root@U8-1 ~]# systemctl start apache2
[root@U8-1 ~]# ss -ntulp|grep 80
tcp LISTEN 0 511 *:80 : users:((“apache2”,pid=2644,fd=4),(“apache2”,pid=2643,fd=4),(“apache2”,pid=2641,fd=4))
[root@U8-1 ~]# curl http://192.168.124.31
U8-1 web,192.168.124.31
#在U8-2上重复以上操作
[root@U8-2 ~]# apt -y install apache2
[root@U8-2 ~]# echo “U8-2 web,192.168.124.32” > /var/www/html/index.html
[root@U8-2 ~]# systemctl start apache2
[root@U8-2 ~]# ss -ntulp|grep 80
tcp LISTEN 0 511 *:80 : users:((“apache2”,pid=13011,fd=4),(“apache2”,pid=13010,fd=4),(“apache2”,pid=13008,fd=4))
[root@U8-2 ~]# curl http://192.168.124.32
U8-1 web,192.168.124.32
Nginx http 反向代理入门:
#反向代理配置参数:
proxy_pass;
#用来设置将客户端请求转发给的后端服务器的主机,可以是主机名、IP地址:端口的方式,也可以代理到预先设置的主机群组,需要模块gx_http_upstream_module支持。
location /web {
index index.html;
proxy_pass http://192.168.124.31:80;
#不带斜线将访问的/web,等于访问后端服务器 http://192.168.124.31:80/web/index.html 即后端服务器配置的站点根目录要有web目录才可以被访问,这是一个追加/web到后端服务器 http://servername:port/WEB/INDEX.HTML 的操作
proxy_pass http://192.168.124.31:80/;
#带斜线,等于访问后端服务器的http://192.168.124.31:80/index.html 内容返回给客户端
}
#重启Nginx测试访问效果:
# curl -L http://www.dushansao.com/web/index.html
proxy_hide_header field;
#用于nginx作为反向代理的时候,在返回给客户端http响应的时候,隐藏后端服务版本相应头部的信息,可以设置在http/server或location块,
location /web {
index index.html;
proxy_pass http://192.168.124.31:80/;
proxy_hide_header ETag;
}
proxy_pass_header field;
#默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel等参数,如果要传递的话则要使用 proxy_pass_header field声明将后端服务器返回的值传递给客户端。
proxy_pass_request_body on | off;
#是否向后端服务器发送HTTP包体部分,可以设置在http/server或location块,默认即为开启
proxy_pass_request_headers on | off;
#是否将客户端的请求头部转发给后端服务器,可以设置在http/server或location块,默认即为开启
proxy_set_header;
#可以更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实IP的时候,就要更改每一个报文的头部,如下:
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
#添加HOST到报文头部,如果客户端为NAT上网那么其值为客户端的共用的公网IP地址,常用于在日之中记录客户端的真实IP地址。
proxy_connect_timeout time;
#配置nginx服务器与后端服务器尝试建立连接的超时时间,默认为60秒,用法如下:
proxy_connect_timeout 60s;
#60s为自定义nginx与后端服务器建立连接的超时时间
proxy_read_time time;
#配置nginx服务器向后端服务器或服务器组发起read请求后,等待的超时时间,默认60s
proxy_send_time time;
#配置nginx项后端服务器或服务器组发起write请求后,等待的超时时间,默认60s
proxy_http_version 1.0;
#用于设置nginx提供代理服务的HTTP协议的版本,默认http 1.0
proxy_ignore_client_abort off;
#当客户端网络中断请求时,nginx服务器中断其对后端服务器的请求。即如果此项设置为on开启,则服务器会忽略客户端中断并一直等着代理服务执行返回,如果设置为off,则客户端中断后Nginx也会中断客户端请求并立即记录499日志,默认为off。
proxy_headers_hash_bucket_size 128;
#当配置了 proxy_hide_header和proxy_set_header的时候,用于设置nginx保存HTTP报文头的hash表的上限。
proxy_headers_hash_max_size 512;
#设置proxy_headers_hash_bucket_size的最大可用空间
server_namse_hash_bucket_size 512;
#server_name hash表申请空间大小
server_names_hash_max_szie 512;
#设置服务器名称hash表的上限大小
反向代理示例–单台web服务器:
[root@U8: ~]# cd /apps/nginx/conf/conf.d/
[root@U8: /apps/nginx/conf/conf.d]# vim pc.conf
server {
listen 80;
server_name www.dushansao.com;
location / {
proxy_pass http://192.168.124.31:80/;
}
}
[root@U8: /apps/nginx/conf/conf.d]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: