• nginx常用配置


    http模块

    stream模块

    从1.9.0开始,增加stream模块,用来实现四层协议的转发、代理或者负载均衡等
    stream模块的用法和http模块差不多,语法基本一致,支持server,hash, listen, proxy_pass等指令。

    Oracle反向代理

    #如果要代理oracle或者mysql这种需要用stream
    stream {
        upstream oracle {
           hash $remote_addr consistent;
           server 192.168.99.3:1521 weight=50 max_fails=3 fail_timeout=30s;
        }
        server {
            listen 3000 so_keepalive=on;#公网机器监听端口
            proxy_connect_timeout 1s;
            #注意这个,简单的来说就是连接上以后session的保持时间,就是多长时间不活动下次要重新链接,建议设大一点
            proxy_timeout 36000s;
            proxy_pass oracle;
            proxy_download_rate 2048000;
            proxy_upload_rate 2048000;
        }
    	
    }
    

    sql server反向代理

    #如果要代理oracle或者mysql这种需要用stream
    stream {
        
        upstream sqlserver {   
            server 192.168.90.5:1433 weight=1 max_fails=2 fail_timeout=30s;   
        }
        
    
         server {
            listen       1433;
            proxy_connect_timeout 1s;
            proxy_timeout 300s;
            proxy_pass sqlserver;
        }
    }
    
    

    redis反向代理

    #如果要代理oracle或者mysql这种需要用stream
    stream {
        upstream redis {
            server 192.168.99.3:6379 max_fails=3 fail_timeout=30s;
        }
        server {
            listen 6379;
            proxy_connect_timeout 1s;
            proxy_timeout 3s;
            proxy_pass redis;
        }
    }
    

    ssh反向代理

    stream {
        upstream ssh {
            server 192.168.150.95:22;        
    
        }
        server {
            listen         33;
            proxy_connect_timeout    3s;
            proxy_timeout            5s;
            proxy_pass     ssh;
        }
    }
    
    

    ssh客户端连接:

    ssh -p 33 root@ip
    

    1. location下的匹配命令

    1.1 可用的匹配命令及作用

    location 中可用的匹配命令有两种:普通字符串和正则表达式。~ 和~* 用于正则表达式,其他前缀和无任何前缀都用于普通字符串。正则表达式会根据匹配顺序,匹配到第一个正则表达式后停止搜索。普通字符串匹配则无视顺序,只会选择最精确的匹配。常用的匹配命令和作用如下:

    命令 作用
    ~ 表示执行一个正则匹配,区分大小写
    ~* 表示执行一个正则匹配,不区分大小写
    ^~ 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配其他。一般用来匹配目录
    = 进行普通字符精确匹配
    无前缀 用于普通字符串
    @ 定义一个命名的location,使用在内部定向时,例如error_page,try_files

    1.2 匹配命令优先级

    1.3 示例

    • 只匹配“/”。
    location = / {
    }
    
    • 匹配任何请求,所有URI都是以“/”开始;更长字符匹配或正则表达式匹配会优先。
    
    
    
    匹配任何以/Directory/ 开始的请求,并停止匹配其他location。
    location ^~ /Directory/ {
    }
    
    匹配以gif、jpg、jpeg结尾的请求;但是遇到^~由它处理 。
    location ~* .(gif|jpg|jpeg)$ {
    }
    
    以/index/开头的请求,如果链接的状态为404。则会匹配到@index_error。
    location /index/ {
    error_page 404 @index_error;
    }
    location @index_error {
    … }
    
    2. 常用配置指令:alias、root、proxy_pass
    2.1 alias
    alias为别名配置,用于访问文件系统,在匹配到URL后,将URL中 匹配到的字段替换成alias 后边的内容。
    示例一:
    若 location 配置如下(普通字符串匹配),则请求URL为“/test/file”时,返回文件“/usr/local/file”。
    
    location /test/ {
    alias /usr/local/;
    }
    
    示例二:
    若 location 配置如下(正则表达式匹配),则请求URL为“/test/love.gif”时,返回文件“/usr/local/test1/love.gif”。
    其中 “$1” 表示 location表达式中匹配的第一个参数。
    
    location ~* /test/~* .(gif|jpg|jpeg)${
    alias /usr/local/test1/$1;
    }
    
    2.2 root
    根路径配置,用于访问文件系统,在匹配到URI后,指向root配置的路径,并把请求路径附加到其后。
    示例一:
    若 location 配置如下(普通字符串匹配),则请求URL为“/test/file”时,返回文件“/usr/local/test/file”
    
    location /test/ {
    root /usr/local/;
    }
    
    2.3 proxy_pass
    代理配置,用于代理请求,匹配到URI后,转发请求到proxy_pass配置的URL。
    示例一:
    若 location 配置如下(普通字符串匹配),则请求URL为“/test/hello”时,返回则将请求转发到“http://192.168.1.111:8089/hello”。
    
    location /test/ {
    proxy_pass http://192.168.1.111:8089/;
    }
    
    注:如果proxy_pass配置了“/”则不附加location的URI,没有配置“/”,则会附加。
  • 相关阅读:
    Python类属性的延迟计算
    解析Python编程中的包结构
    解析Python编程中的包结构
    Python查询Mysql时返回字典结构的代码
    VS2010中如何查看DLL的导出接口
    C++ 简单的日志类
    ilmerge工具合并多个DLL或EXE
    基于InstallShield2013LimitedEdition的安装包制作
    c# 操作注册表
    Source Insight 常用设置和快捷键大全
  • 原文地址:https://www.cnblogs.com/zhuxiang1633/p/14154999.html
Copyright © 2020-2023  润新知