开发者社区> 问答> 正文

几十个系统各自使用nginx做反向代理,是否可以聚合,以及是否有必要聚合?

请问各位大神,公司有几十个系统都用到了nginx做反向代理,各个项目配置的水平参差不齐,我想整合一下,把原有的几十个nginx聚合到几个,访问量小的聚合一起,访问量大的单独起实例,但是也尽量放在一台机器上。 本意是想降低管理成本,提升nginx的配置水平,优化性能,但是又考虑到聚合到一起是否会存在安全风险?一旦被攻破所有项目都会面临信息泄露风险,同时聚合统一管理又增大了我们自己集中运维的工作量,是否有必要这样来做呢?

求大神指点,感谢!

展开
收起
clu2chv4kinvw 2020-10-14 10:11:11 824 0
1 条回答
写回答
取消 提交回答
  • 下一站是幸福

    Nginx反向代理
    nginx只能做反向代理服务,httpd既能做正向又能做反向代理 反向代理时,必须有反向代理相关的模块 从httpd服务端取到内容--->放在nginx proxy cache--->返回给客户端

    nginx通常用来做proxy,做httpd很少,
    
    下面来介绍一下nginx做反向代理模块及相关参数:
    ngx_http_proxy_module模块功能:
    
        The ngx_http_proxy_module module allows passing requests to another server.
    
        1、proxy_pass URL;
            Context:    location, if in location, limit_except
            
            注意:proxy_pass后面的路径不带uri时,其会将location的uri传递给后端主机;
                
                server {
                    ...
                    server_name HOSTNAME;
                    location /uri/ {
                        proxy http://hos[:port];
                    }
                    ...
                }
                
                http://HOSTNAME/uri --> http://host/uri
                
            proxy_pass后面的路径是一个uri时,其会将location的uri替换为proxy_pass的uri;
                
                server {
                    ...
                    server_name HOSTNAME;
                    location /uri/ {
                        proxy http://host/new_uri/;
                    }
                    ...
                }
                
                http://HOSTNAME/uri/ --> http://host/new_uri/
                
            如果location定义其uri时使用了正则表达式的模式,或在if语句或limt_execept中使用proxy_pass指令,则proxy_pass之后必须    不能使用uri; 用户请求时传递的uri将直接附加代理到的服务的之后;
            
                server {
                    ...
                    server_name HOSTNAME;
                    location ~|~* /uri/ {
                        proxy http://host;
                    }
                    ...
                }
                
                http://HOSTNAME/uri/ --> http://host/uri/;
                
        2、proxy_set_header field value;
            设定发往后端主机的请求报文的请求首部的值;Context:    http, server, location
            
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            
        3、proxy_cache_path  缓存空间先定义后使用;指明哪种方式可以查缓存
            定义可用于proxy功能的缓存;Context:    http            
            
            proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [    max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [    loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [    purger_threshold=time];
            
        4、proxy_cache zone(名字) | off;
            指明要调用的缓存,或关闭缓存机制;Context:    http, server, location
            
        5、proxy_cache_key string;
            缓存中用于“键”的内容;
            
            默认值:proxy_cache_key $scheme$proxy_host$request_uri;
            
        6、proxy_cache_valid [code ...] time;
            定义对特定响应码的响应内容的缓存时长;
            
            定义在http{...}中;
            proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;
            
            定义在需要调用缓存功能的配置段,例如server{...};
            proxy_cache pxycache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 302 301 1h;
            proxy_cache_valid any 1m;
            
        7、proxy_cache_use_stale
           proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 |     http_504 | http_403 | http_404 | off ...;
            
            Determines in which cases a stale cached response can be used when an error occurs during communication     with the proxied server.
            
        8、proxy_cache_methods GET | HEAD | POST ...;
             如果客户端请求方法在这个指令中列出,那么响应将被缓存。
            
        9、proxy_hide_header field;
            默认情况下,nginx不会传递头字段“Date”、“Server”、“X-Pad”和“X-Accel-…”从代理服务器到客户机的响应。proxy_hide_header指令设置不会传递的其他字段。
            
        10、proxy_connect_timeout time;
            定义与代理服务器建立连接的超时。应该注意,这个超时通常不能超过75秒。默认为60s;最长为75s;
            
        11、proxy_read_timeout time;
            定义从代理服务器读取响应的超时。超时仅在两个连续的读取操作之间设置,而不用于传输整个响应。
            
        12、proxy_send_timeout time;
            设置向代理服务器传输请求的超时。超时仅在两个连续的写操作之间设置,而不用于传输整个请求。如果代理服务器在此期间没有接收到任何内容,则关闭连接。
                nginx与web结合只有一种方式,由于php没办法编译成nginx模块,并直接内置在nginx上自己处理动态内容,因此处niginx处理动态页面方式:nginx+php(fpmserver),nginx需要装fastcgi_module,基于此模块作为协议的客户端(fastcgi协议)
                fastcgi协议对用户并发请求的资源响应能力很有限,为了避免fcgi的局限性,将fastcgi_module换成AP(apache对于静态资源的请求,由nginx响应;动态资源请求交给AP来处理。
    
    ngx_http_headers_module模块功能:
        The ngx_http_headers_module module allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields, to a response header.
        
        向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值;
        
        1、add_header name value [always];
            添加自定义首部;
            
            add_header X-Via  $server_addr;
            add_header X-Accel $server_name;
            
        2、expires [modified] time;
            expires epoch | max | off;
            
            用于定义Expire或Cache-Control首部的值;
                                
    
    ngx_http_fastcgi_module模块功能:
        
        The ngx_http_fastcgi_module module allows passing requests to a FastCGI server.
        
        1、fastcgi_pass address;
            address为fastcgi server的地址;    location, if in location;
            
                http://www.ilinux.io/admin/index.php --> /admin/index.php (uri)
                    /data/application/admin/index.php
                        
            
        2、fastcgi_index name;
            fastcgi默认的主页资源;
            
        3、fastcgi_param parameter value [if_not_empty];
            Sets a parameter that should be passed to the FastCGI server. The value can contain text, variables, and their combination.
            
        配置示例1:
            前提:配置好fpm server和mariadb-server服务;
                location ~* \.php$ {
                    root           /usr/share/nginx/html;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
                    include        fastcgi_params;
                }
                
        配置示例2:通过/pm_status和/ping来获取fpm server状态信息;
            location ~* ^/(pm_status|ping)$ {
                include        fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param  SCRIPT_FILENAME  $fastcgi_script_name;
            }            
                
        4、fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
        
            定义fastcgi的缓存;缓存位置为磁盘上的文件系统,节约网络IO资源,降低瓶颈,由path所指定路径来定义;
            
                levels=levels:缓存目录的层级数量,以及每一级的目录数量;levels=ONE:TWO:THREE
                    leves=1:2:2
                keys_zone=name:size
                    k/v映射的内存空间的名称及大小
                inactive=time
                    非活动时长
                max_size=size
                    磁盘上用于缓存数据的缓存空间上限
                
        5、fastcgi_cache zone | off;
            调用指定的缓存空间来缓存数据;http, server, location
            
        6、fastcgi_cache_key string;
            定义用作缓存项的key的字符串;
            
        7、fastcgi_cache_methods GET | HEAD | POST ...;
            为哪些请求方法使用缓存;
            
        8、fastcgi_cache_min_uses number;
            缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项;
            
        9、fastcgi_cache_valid [code ...] time;
            不同的响应码各自的缓存时长;
            
            示例:
                http {
                    ...
                    fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20minactive=120s;
                    ...
                    server {
                        ...
                        location ~* \.php$ {
                            ...
                            fastcgi_cache fcgi;
                            fastcgi_cache_key $request_uri;
                            fastcgi_cache_valid 200 302 10m;
                            fastcgi_cache_valid 301 1h;
                            fastcgi_cache_valid any 1m;    
                            ...
                        }
                        ...
                    }
                    ...
                }
    
    2021-04-02 22:06:43
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
《Nginx 代理系统常用手册》 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载