root@linux:/apps/nginx/conf.d# cat test.conf server{ server_name www.haha.com; listen 80; access_log /apps/nginx/logs/haha_access.log; error_log /apps/nginx/logs/haha_error.log; error_page 404 /no.html; server_tokens off; location /no.html { root /data/html; } location / { proxy_pass http://192.168.66.33; } } root@linux:/apps/nginx/conf.d# root@linux:/apps/nginx/conf.d# nginx -s reload root@linux:/apps/nginx/conf.d# curl 192.168.66.30 192.168.66.33 nginx
root@linux:/apps/nginx/conf.d# cat test.conf server{ server_name www.haha.com; listen 80; access_log /apps/nginx/logs/haha_access.log; error_log /apps/nginx/logs/haha_error.log; error_page 404 /no.html; server_tokens off; location /no.html { root /data/html; } location / { proxy_pass http://192.168.66.33; } location /web { proxy_pass http://192.168.66.32/; } } root@linux:/apps/nginx/conf.d# root@linux:/apps/nginx/conf.d# nginx -s reload root@linux:/apps/nginx/conf.d# curl 192.168.66.30/web/ 192.168.66.32 apache root@linux:/apps/nginx/conf.d# curl 192.168.66.30 192.168.66.33 nginx root@linux:/apps/nginx/conf.d# # 后端服务器必须要有相应的web路径 root@linux:/var/www/html# mkdir web root@linux:/var/www/html# cp index.html web/.
在http代码块中,定义缓存信息,注意这是一行
proxy_cache_path /data/nginx/proxy_cache #定义缓存保存路径,proxy_cache会自动创建 levels=1:2:2 #定义缓存目录结构层次,1:2:2可以生成2^4x2^8x2^8=1048576个目录 keys_zone=proxycache:20m #指内存中缓存的大小,主要用于存放key和metadata(如:使用次数) inactive=120s; #缓存有效时间 max_size=1g; #最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值 # 注意目录需要创建到/var/cache/nginx 这一层
在location 中调用
root@linux:/apps/nginx/conf.d# vi test.conf location /web { proxy_pass http://192.168.66.32; proxy_set_header clientip $remote_addr; proxy_cache proxycache; proxy_cache_key $request_uri; proxy_cache_valid 200 302 301 1h; proxy_cache_valid any 1m; } root@linux:/apps/nginx/conf.d# ll /data/nginx/ total 12 drwxr-xr-x 3 root root 4096 Jan 8 12:25 ./ drwxr-xr-x 5 root root 4096 Jan 8 12:25 ../ drwx------ 3 nobody root 4096 Jan 8 12:25 proxycache/ root@linux:/apps/nginx/conf.d# ll /data/nginx/proxycache/ total 12 drwx------ 3 nobody root 4096 Jan 8 12:25 ./ drwxr-xr-x 3 root root 4096 Jan 8 12:25 ../ drwx------ 3 nobody nogroup 4096 Jan 8 12:25 4/ root@linux:/apps/nginx/conf.d#
nginx基于模块ngx_http_headers_module可以实现对头部报文添加指定的key与值
location /web { proxy_pass http://192.168.66.32; proxy_set_header clientip $remote_addr; proxy_cache proxycache; proxy_cache_key $request_uri; proxy_cache_valid 200 302 301 1h; proxy_cache_valid any 1m; add_header X-Via $server_addr; add_header X-Cache $upstream_cache_status; add_header X-Accel $server_name; } root@linux:/apps/nginx/conf.d# curl -I 192.168.66.30/web/index.html HTTP/1.1 200 OK Server: haha Date: Wed, 08 Jan 2020 12:31:42 GMT Content-Type: text/html Content-Length: 22 Connection: keep-alive Last-Modified: Wed, 08 Jan 2020 12:18:43 GMT ETag: "16-59b9fe4baf8d7" X-Via: 192.168.66.30 X-Cache: HIT # 缓存是否命中,如果是miss 就是未命中 X-Accel: www.haha.com Accept-Ranges: bytes root@linux:/apps/nginx/conf.d#
在上一个章节中Nginx可以将客户端的请求转发至单台后端服务器但是无法转发至特定的一组的服务器,而且不能对后端服务器提供相应的服务器状态监测,但是Nginx可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能,官方文档: https://nginx.org/en/docs/http/ngx_http_upstream_module.html调度算法
hash KEY consistent; #基于指定key做hash计算,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器 (如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算。 hash $request_uri consistent; #基于用户请求的uri做hash ip_hash; #源地址hash调度方法,基于的客户端的remote_addr(源地址)做hash计算,以实现会话保持, least_conn; #最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器
服务器选项
server address [parameters]; #配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。 #server支持的parameters如下: weight=number #设置权重,默认为1。 max_conns=number #给当前server设置最大活动链接数,默认为0表示没有限制。 max_fails=number #对后端服务器连续监测失败多少次就标记为不可用。 fail_timeout=time #对后端服务器的单次监测超时时间,默认为10秒。 backup #设置为备份服务器,当所有服务器不可用时将重新启用次服务器。 down #标记为down状态。 resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx。
范例:注意我得主机名都一样,其实是在不同的服务器上
root@linux:/apps/nginx/conf.d# cat test.conf upstream webserver { least_conn; server 192.168.66.32:80 weight=1 fail_timeout=5s max_fails=3; server 192.168.66.33:80 weight=1 fail_timeout=5s max_fails=3; } server{ server_name www.haha.com; listen 80; access_log /apps/nginx/logs/haha_access.log; error_log /apps/nginx/logs/haha_error.log; error_page 404 /no.html; server_tokens off; location /no.html { root /data/html; } location / { proxy_pass http://webserver/; index index.html; } } root@linux:/apps/nginx/conf.d# root@linux:/apps/nginx/conf.d# nginx -s reload # 测试 root@linux:~# curl 192.168.66.30 192.168.66.32 apache root@linux:~# curl 192.168.66.30 192.168.66.33 nginx root@linux:~# curl 192.168.66.30 192.168.66.32 apache root@linux:~# curl 192.168.66.30 192.168.66.33 nginx # 关掉Apache的进行测试 root@linux:~# systemctl stop apache2.service root@linux:~# root@linux:~# curl 192.168.66.30 192.168.66.33 nginx root@linux:~# curl 192.168.66.30 192.168.66.33 nginx root@linux:~# curl 192.168.66.30 192.168.66.33 nginx root@linux:~# curl 192.168.66.30 192.168.66.33 nginx root@linux:~#
1.nginx 添加头部信息,传输到后端服务器
location / { proxy_pass http://webserver/; proxy_set_header linux $proxy_add_x_forwarded_for; index index.html; }
2.后端服务器日志 修改日志添加这个linux字段
LogFormat "\"%{linux}i\" %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
3.查看效果
root@linux:~# tail -5 /var/log/apache2/access.log 192.168.66.30 - - [09/Jan/2020:02:49:30 +0000] "GET / HTTP/1.0" 200 268 "-" "curl/7.58.0" 192.168.66.30 - - [09/Jan/2020:02:53:04 +0000] "GET / HTTP/1.0" 200 268 "-" "curl/7.58.0" 192.168.66.30 - - [09/Jan/2020:02:53:05 +0000] "GET / HTTP/1.0" 200 268 "-" "curl/7.58.0" "192.168.66.31" 192.168.66.30 - - [09/Jan/2020:02:55:10 +0000] "GET / HTTP/1.0" 200 268 "-" "curl/7.58.0" "192.168.66.31" 192.168.66.30 - - [09/Jan/2020:02:55:10 +0000] "GET / HTTP/1.0" 200 268 "-" "curl/7.58.0" root@linux:~#
注意包含文件需要和http同级别
root@linux:/apps/nginx# vi conf/nginx.conf events { worker_connections 1024; } include /apps/nginx/test2.conf; http { root@linux:/apps/nginx# cat test2.conf stream { upstream mysql { least_conn; server 192.168.66.31:3306 max_fails=3 fail_timeout=30s; } server { listen 192.168.66.30:3306; proxy_connect_timeout 6s; proxy_timeout 15s; proxy_pass mysql; } } root@linux:/apps/nginx# root@linux:/apps/nginx# mysql -h192.168.66.30 -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 56 Server version: 10.1.43-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> exit Bye
原文链接:https://blog.csdn.net/fanfusuzi123/article/details/103948033
© 版权声明
声明📢本站内容均来自互联网,归原创作者所有,如有侵权必删除。
本站文章皆由CC-4.0协议发布,如无来源则为原创,转载请注明出处。
THE END