Nginx学习日记第四篇 -- 反向代理及缓存功能

Nginx学习日记第四篇 -- 反向代理及缓存功能,第1张

Nginx学习日记第四篇--反向代理及缓存功能

一、Nginx反向代理

ngx中的ngx_http_proxy_module控制模块可以完成后端开发网络服务器的反向代理功能,从而完成手机客户端及其三层切换功能所需的动静分离。

1。测试场景

Nginx服务器作为反向代理网络服务器,向node1serverweb服务器发送手机客户端请求。

Nginx服务器的Ip地址:192.168.0.110

1节点1服务器IP:192.168.0.40

2、Nginx电脑主机配置

grep -Ev "#|^$" server.conf     server {         listen       80;         server_name   localhost;         location / {     proxy_pass http://192.168.0.40;     proxy_set_header Host    $host;     proxy_set_header X-Real-IP  $remote_addr;         }         error_page   500 502 503 504  /50x.html;         location = /50x.html {             root   html;         }     } [root@Nginx conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@Nginx conf]# nginx -s reload

3。node1计算机主机被配置为apache网络服务器

[root@node1 ~]# service httpd start Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.0.15 for ServerName                                                            [  OK  ] [root@node1 ~]# curl 192.168.0.40 This is node1

4。反向代理的检测



5。常见问题

如果/uri是后端开发的反向代理,那么/uri可以是后端开发中的/newuri;

如果/uri与应用程序模式匹配,它将在经销商连接后立即添加。

如果代理之前定义了rewrite,代理会将rewrite后的uri作为微信业务。

proxy_passhttp://192.168.0.40;

proxy_set_headerHost$host自定义移动客户端所需的第一部分的值。

proxy_set_headerX-Real-IP$remote_addr;自定义页眉顶部信息以添加移动客户端IP

6。自定义页眉顶部信息后的日志信息

[root@node1 ~]# vim /etc/httpd/conf/httpd.conf  LogFormat "%{X-Real-IP}i %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined [root@node1 ~]# service httpd reload [root@node1 ~]# cat /etc/httpd/logs/access_log 192.168.0.109 - - [06/Feb/2017:13:56:20 0800] "GET /user/ HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0" 192.168.0.109 - - [06/Feb/2017:13:56:21 0800] "GET /user/ HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0"

7。某些代理(动态和静态分离)

Nginx的一部分

[root@Nginx conf]# !grep grep -Ev "#|^$" server.conf     server {         listen       80;         server_name   localhost;         location / {     root html/xn1;     index index.html; }         location /user {     proxy_pass http://192.168.0.40;     proxy_set_header Host    $host;     proxy_set_header X-Real-IP  $remote_addr;         }         error_page   500 502 503 504  /50x.html;         location = /50x.html {             root   html;         }     } [root@Nginx conf]# nginx -s reload

节点1的一部分

[root@node1 ~]# mkdir /var/www/html/user [root@node1 ~]# echo "This is node1 user" >> /var/www/html/user/index.html [root@node1 ~]# service httpd reload

测试:

本地解决方案要求的一部分


反向代理的一部分:

大量代理命令,详情:http://nginx.org/en/docs/http/ngx_http_proxy_module.html



二。代理缓存文件的功能

Nginx反向代理移动客户端需求到后端开发远程服务器时,创建一个keep-alive连接;服务器和手机客户端,服务器代理和后端开发web服务器都产生长链,会降低Nginx的特性。所以此时此刻代理控制模块会发挥其缓存文件的作用,服务器代理和手机客户端仍然保持着一个很长的链条。服务器代理与后端开发web服务器之间的需求完成后,服务器代理在本地缓存内容文件,不与后端开发产生长链,大大节省了服务器资源。另外,当手机客户端请求来的时候,服务器代理会立即搜索缓存文件并返回给手机客户端。代理将缓存文件以key-value的方式存储在运行内存中,value存储本地系统文件中存储的URL的哈希值。

1。配备缓存文件

1.建立缓存文件文件目录 [root@Nginx conf]# mkdir -pv /cache/nginx/ mkdir: 已建立文件目录 "/cache" mkdir: 已建立文件目录 "/cache/nginx/" [root@Nginx conf]# chown -R nginx:nginx /cache/nginx 2.在环境变量的http段界定缓存文件文件目录 [root@Nginx conf]# vim nginx.conf     proxy_cache_path /cache/nginx/ keys_zone=mycache:32m; 3.在server或location路段均可启用,依据具体情况应用,这里在location路段启用 [root@Nginx conf]# !grep grep -Ev "#|^$" server.conf     server {         listen       80;         server_name   localhost;         location / {     root html/xn1;     index index.html; }         location /user {     proxy_cache mycache;     proxy_cache_valid 200 3h;     proxy_cache_valid 301 302 十米;             proxy_cache_valid all 1米;     proxy_cache_use_stale error timeout http_500 http_502 http_503;     proxy_pass http://192.168.0.40;     proxy_set_header Host    $host;     proxy_set_header X-Real-IP  $remote_addr;         }         error_page   500 502 503 504  /50x.html;         location = /50x.html {             root   html;         }     } [root@Nginx conf]# nginx -s reload 4.要求后,缓存文件文件目录中出現缓存文件 [root@Nginx conf]# ll /cache/nginx/ 总使用量 4 -rw------- 1 nginx nginx 462 2月   6 17:09 a8d7f3cb1968f4e6056774a5a3a73468 5.cache句子 proxy_cache_path /cache/nginx/ keys_zone=mycache:32m; 界定缓存文件在系统文件中的储存途径,界定key值在运行内存中的用户标识符与尺寸,其他众多选择项有默认设置配备,界定在哪个部位,就有什么配备可应用缓存文件 proxy_cache mycache; 应用mycache缓存文件 proxy_cache_valid 200 3h; 以回应状态码界定缓存文件储存时间,可定义好几个 proxy_cache_use_stale error timeout http_500 http_502 http_503; 界定在碰到什么情况能够应用到期缓存文件回应手机客户端


2。补充说明

缓存的功能可以在http、server、location三个节中定义,哪个节的定义说明了缓存文件可以应用什么需求;一般来说,我们在http段定义缓存文件的方式,根据细节启用缓存文件。

其他缓存句子:http://nginx.org/en/docs/http/ngx_http_代理_module.html#代理_缓存




欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/777848.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-03
下一篇 2022-05-03

发表评论

登录后才能评论

评论列表(0条)

保存