nginx模块的应用
ngx_http_proxy_modulenginx 反向代理模块: http://nginx.org/en/docs/http/ngx_http_proxy_module.html
server {
listen
server_name
location / {
proxy_pass http://192.168.184.141:80/; //指明把用户所有对路径"/"的请求都转发到后端所指定的服务器:192.168.184.141:80
}
}
格式:
location /uri {
rewrite
proxy_pass http://back_server:port/newuri;
}
/uri --> /newuri
http://www.magedu.com
http://mysql.magedu.com
proxy_connect_timeout:
proxy_hide_header:
upstream
回顾:nginx
ngx_http_proxy_module, ngx_http_upstream_module
ngx_http_proxy_module:实现反向代理及缓存功能
proxy_pass http://{SERVER_IP|UPSTREAM_NAME}/uri
proxy_cache_path path [levels=levels] keys_zone=name:size [inactive=time] [max_size=size] ;
proxy_cache zone_name;
proxy_cache_valid [code] time;
proxy_cache_method
proxy_cache_use_stale error timeout ...
proxy_cache_min_uses
proxy_cache_bypass string: 设置在何种情形下nginx将不从cache取数据的;
$cookie_nocache $arg_nocache $http_authorization
proxy_set_header
ngx_http_upstream_module:
定义服务器组
proxy_pass, fastcgi_pass, uwsgi_pass,
upstream name {
server address [parameters];
ip_hash;
}
nginx(2)
SNAT模式的大量Client
基于sticky实现session绑定:
cookie
route
learn ()
least_conn: 调度方法,最少连接;
health_check;
建议:关闭访问日志;
自定义响应首部:
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
LNMP
fpm
编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
LNAMP
LNMP, fastcgi_cache
练习:
(1) root为同一路径;
(2) root为不同的路径;
location \.php$ {
root /web/app/wp;
}
location / {
root /web/htdocs;
}
如何解决问题?
(3) fpm server为另一主机;
location \.php$ {
fastcgi_pass fastcgi://172.16.100.9:9000;
}
location / {
root /web/htdocs;
}
总结:
cache:
proxy_cache
fastcgi_cache
练习:
使用nginx反向代理(rr调度)用户请求至两个以上的后端LAMP(按标准路径部署的有pma);
(1) 手动更新所有节点上的pma至新版本;
(2) 写脚本实现如上过程;
回顾:nginx upstream, fastcgi
upstream name {
server
server
}
wrr
ip_hash|least_conn|sticky
fastcgi_pass fastcgi://
LNMMP: Memcached
HAProxy:
web arch: haproxy
mode: http, tcp (https, mysql)
HAProxy:
代理(http): 掮客(broker)
正向代理:
反向代理:
代理作用:web缓存(加速)、反向代理、内容路由(根据流量及内容类型等将请求转发至特定服务器)、转码器;
在代理服务器上添加Via首部;
缓存的作用:
减少冗余内容传输;
节省带宽、缓解网络瓶颈;
降低了对原始服务器的请求压力;
降低了传输延迟;
HAProxy: 只是http协议的反向代理,不提供缓存功能;但额外支持对tcp层对基于tcp通信的应用做LB;
nginx:
server {
}
server {
location ~* \.php$ {
proxy_pass
}
location / {
}
}
upstream {
leastconn
server
server
}
upstream {
}
haproxy:
frontend
use_backend
default_backend
backend
balancer
server
server
listen:
server
default
配置文件:haproxy.cfg
全局配置
代理配置
回顾:
HAProxy:
http协议反向代理
tcp层的LB
特性:event-driven, ebtree
配置:/etc/haproxy/haproxy.cfg
/usr/sbin/haproxy
CentOS 6: /etc/rc.d/init.d/haproxy
CentOS 7: haproxy.service
配置分为两段:
global
配置参数:log, maxconn, ...
proxies
defaults, frontend, backend, listen
示例:
frontend main *:80
default_backend websrvs
backend websrvs
balance roundrobin
server web1 172.16.100.68 check
server web2 172.16.100.69 check
nihoa
原文地址:https://www.cnblogs.com/hanshanxiaoheshang/p/9941007.html