[toc]
Nginx 常用架构

LB Cluster
| 提升系统容量的方式: |
| scale up: |
| scale out: |
| |
| session保持方法: |
| session绑定:sh |
| session复制: |
| session服务器: memchached redis (key-value,kv store) |
| 对url 做hash 计算后,做为key |
| 对url 对应的内容做为value |
I/O:
| 同步/异步:被调用者,在收到调用请求后,是否立即返回。还是得到最终结果后才返回; |
| 阻塞/非阻塞:调用者发起调用之后,在收到响应结果之前,是否会被挂起,被挂起,被称为阻塞,非挂起为非阻塞; |
| I/O网络编程模型中,常用网络模型有5种; |
| 1、同步阻塞 |
| 2、同步非阻塞 |
| 3、复用型I/O |
| 4、(Event Driver) 事件驱动 |
| 5、异步I/O |
| |
| libevent: 项目 |
| epoll() |
| 可以对nginx进程对CPU的核心数来进行绑定; |
| LRU:最近最少缓存条目算法; |
| 平滑升级,平滑故障处理,或者灰度发布; |
| 对于web服务器来说,日志至关重要,需要对日志进行分析; |
| CacheManager: 缓存的失效,过期检验及清理操作; |
Nginx 配置
| main, event, http 基于c语言风格; |
| |
| httpd{ |
| drective |
| server{ |
| listen |
| server_name |
| location{ |
| if { |
| } |
| } |
| } |
| server { |
| } |
| } |
nginx 的安装包
| |
| |
| [epel] |
| name=Extra Packages for Enterprise Linux 7 - $basearch |
| enabled=1 |
| failovermethod=priority |
| baseurl=http://mirrors.cloud.aliyuncs.com/epel/7/$basearch |
| gpgcheck=0 |
| gpgkey=http://mirrors.cloud.aliyuncs.com/epel/RPM-GPG-KEY-EPEL-7 |
| |
| |
ngx_http_proxy_module 模块
| server { |
| listen |
| server_name |
| location /{ |
| proxy_pass http://172.16.55.180:80/ |
| proxy_set_header Host $host |
| proxy_set_header X-Real-IP $remote_addr; |
| } |
| } |
| |
| |
| |
| http://www.ssjinyao.com |
| http://mysql.ssjinyao.com |
| |
| |
| |
| |
| echo "<h1>node2</h1>" > /var/www/html/index.html |
| |
| |
| 格式: |
| localtion /uri { |
| proxy_pass http://back_server:port/newuri; |
| } |
| |
| location /uri { |
| rewrite http://back_server:port/newuri |
| |
| } |
| |
| /uri --> /newuri |
| |
| |
| |
| |
| server_name www.ssjinyao.com; |
| location / |
| { |
| proxy_ass http://172.16.55.128/; |
| index index.htm index.html ; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| location /bbs/ { |
| proxy_pass http://172.16.55.128/bbs/; |
| } |
| |
| |
| |
| |
| location /forum/ { |
| proxy_cache mycache; |
| proxy_cache_valid 200 1h; |
| proxy_cache_valid 301 302 10m; |
| proxy_cache_valid any 1m; |
| proxy_cache_use_statle error timeout invalid_header http_500 http_502 http_503 http504; |
| proxy_pass http://172.16.55.128/bbs/; |
| proxy_set_header Host $host; |
| proxy_set_header X-Real-IP $remote_addr; |
| } |
| |
| |
| |
| location ~* \.(jpg|png|gif)$ { |
| proxy_pass http://172.16.55.128; |
| proxy_set_header Host $host; |
| proxy_set_header X-Real-IP $remote_addr; |
| } |
| |
| |
| |
| |
| |
| |
| |
| 需要在Logformat中加入 将第一个值 %h 换成%{X-Real-IP}i |
| |
| |
| |
| |
| |
| proxy_cache_path /cache/nginx/ level=1:1:1 keys_zone=mycache:32m; |
| |
| |
| |
| |
| proxy_connect_timeout: nginx proxy 请求连接到后端连接请求的超时时长; |
| proxy_hide_header: 设定响应到客户端时需要隐藏的首部信息;‘ |
| proxy_buffers 8k; 指定缓冲大小 |
upstream(负载均衡) 模块
upstream 模块只能使用在http段中
例子
| |
| upstream backend { |
| server www.ssjinyao.com weight=5 |
| server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; |
| server unix:/tmp/backend3; |
| server backup1.ssjinyao.com backup; |
| } |
| |
| upstream upservers { |
| ip hash; |
| server 172.16.55.127 max_fail=2 fail_timeout=2 |
| |
| server 172.16.55.129 bakup; |
| } |
| |
| server_name www.ssjinyao.com; |
| |
| location / { |
| proxy_pass http://upservers/; |
| } |
| |
| |
| |
SNAT模式的大量的Client
| 基于sticky实现session绑定: |
| cookie 而我们一般常用的是基于cookie的绑定; |
| route |
| learn() 需要Nginx 在 1.8 版本以上; |
| |
| example: |
| |
| upstream backend { |
| server backend1.example.com; |
| server backend2.example.com; |
| |
| sticky cookie srv_id expires=1h domain=.example.com path=/; |
| } |
| |
| least_conn:调度方法,最少连接; |
| |
| upstream memcached_backend { |
| server 127.0.0.1:11211; |
| server 172.16.55.121:11211; |
| |
| keepavlie 32; |
| } |
| |
| server { |
| ... |
| location /memcached/ { |
| set $memcached_key $uri; |
| memcached_pass memcached_backend; |
| } |
| } |
| |
| location / { |
| proxy_pass http://backend; |
| health_check; |
| } |
| helth_check; 即健康状态检查; |
| 建议: 关闭访问日志; |
| |
| http { |
| server { |
| ... |
| location / { |
| proxy_pass http://backend; |
| health_check match=welcome; |
| } |
| } |
| |
| metch welcome { |
| status 200; |
| header Content-Type = text/html; |
| body ~ "Welcome to nginx"; |
| } |
| } |
Nginx 自定义首部给客户端
| |
| |
| listen 443; |
| server_name www.ssjinyao.com; |
| add_header SSJinYao-Server 'Next-SSJinYao'; |
| add_header SSJinYao-IP $server_addr; |
| add_header X-Cache $upstream_cache_status; |
| add_header Name 'ssjinyao'; |
| |
| |
| HTTP/1.1 200 OK |
| Server: nginx |
| Date: Tue, 24 Apr 2018 07:24:37 GMT |
| Content-Type: text/html; charset=UTF-8 |
| Content-Length: 39777 |
| Connection: keep-alive |
| Vary: Accept-Encoding |
| Last-Modified: Thu, 19 Apr 2018 09:14:18 GMT |
| ETag: "9b61-56a2fff0c14a1" |
| SSJinYao-Server: Next-SSJinYao |
| SSJinYao-IP: 172.31.253.156 |
| X-Cache: HIT |
| Name: ssjinyao |
| Accept-Ranges: bytes |
fast-cgi 使用
| LNMP |
| |
| |
| |
| |
| |
| |
| location / { |
| root /usr/share/nginx/html; |
| index index.php index.html index.htm; |
| } |
| |
| location ~\.php$ { |
| fastcgi_cache fcgicache; |
| fastcgi_cache_valid 200 10m; |
| fastcgi_cache_valid 302 3m; |
| fastcgi_cache_valid any 1m; |
| root /usr/share/nginx/html; |
| fastcgi_pass 127.0.0.1:9000; |
| fastcgi_index indexphp; |
| fastcgi_param SCRIPT_FILENAME /scripts$fastcgi-script_naame; |
| indclude fastcgi_paramgs; |
| } |
| |
| |
| |
| 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; |
| |
| |
| |
| |
| <?php |
| $conn = mysql_connect('127.0.0.1','root',''); |
| if ($conn) |
| echo succ |
| else |
| echo fail; |
| mysql_close(); |
| ?> |
| |
| (1) root为同一路径; |
| (2) root为不同路径; |
| location \.php${ |
| root /web/app/wp; |
| } |
| location / { |
| root /web/htdocs; |
| } |
| |
| (3) fpm server 为另一主机 ; |
| |
| location \.php${ |
| fastcgi_pass fastcgi: |
| } |
| location / { |
| root /web/htdocs; |
| } |
| |