打赏

nginx配置

【nginx入门】nginx反向代理与负载均衡教程_哔哩哔哩_bilibili

 【nginx】nginx配置详解_哔哩哔哩_bilibili

视频作者推荐下载openresty:https://openresty.org/download/openresty-1.21.4.1-win64.zip

openresty比原生nginx多了一些封装好的语法,比如下面用到的echo

建议看视频,很精炼

 

nginx基本配置:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
  # 默认是下载文件的类型 default_type application
/octet-stream; sendfile on; keepalive_timeout 65;
server { listen
80; server_name localhost;      #所有路径都转到nginx/html下找index.html和index.htm,root指定默认目录,index指定默认文件名。 location / { root html; index index.html index.htm; } # 错误页.状态码是500这几个时,会转到/50x.html。/50x.html这个规则会指向html下的50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

 

1,匹配路径与优先级:

worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;


    server {
        listen       80;
        server_name  localhost;
        default_type  text/html;
        #number1 /匹配所有路径,优先级第4 
        location / {
            #echo 输出命令
            echo "hello nginx";
        }
        #number2 =严格匹配路径,优先级第1,路径是/a就输出“=/a”
        location = /a {
            echo "=/a";
        }
        #number3 ^~ 匹配 后面(/a)开头的路径,优先级第2,有多级路径时优先匹配最多的那个
        location ^~ /a {
            echo "^~ /a";
        }
        #number4 ^~ 匹配 后面(/a)开头的路径,优先级第2,与上面的关系是,当输入/a时匹配number3,当输入/a/b时匹配本规则
        location ^~ /a/b {
            echo "^~ /a/b";
        }
        #number5 ~正则匹配 \w匹配任意多的字母、数字、下划线等价于[A-Za-z0-9_],优先级第3 ^是以后面的为开头的
        location ~ ^/\w {
            echo "^~ ^/\w";
        }
        
        #number6 ~正则匹配 ^/[a-z]匹配以/a-/z开头的,,当路径是/a之类的时,此时与number5优先级相同,number5在前会匹配
        location ~ ^/[a-z]{
            echo "^~ ^/[a-z]";
        }
    }
}

2,反向代理:

 

worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;


    server {
        listen       80;
        server_name  localhost;

        # proxy_pass 反向代理
        location /{
            proxy_pass http://localhost:8080;
        }
        
        # 本示例访问/a转到http://localhost:8080/a
        location /a{
            proxy_pass http://localhost:8080;
        }

        # 本示例访问/b转到http://localhost:8080,  注意,/b/和/b作用是一样的,8080/后面的路径不能少
        location /b/{
            proxy_pass http://localhost:8080/;
        }
    }
}

3,负载均衡

worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

    # group1 的负载均衡,此处ip前不能加http,端口后不能加/,没加weight是随机分配
    upstream group1{
        server localhost:8080;
        server localhost:8081;
    }
    # group2 的负载均衡,加weight后按权重分配,weight一样则平均分配,需注意,无论weight=1还是weight,并不是一替1次/10次分配,我在本地测试是一替2次分配
    upstream group2{
        server localhost:8080 weight=10;
        server localhost:8081 weight=10;
    }
    server {
        listen       80;
        server_name  localhost;

        # 负载均衡写法 
        location /b/{
            proxy_pass http://group1/;
        }
        
        # 负载均衡写法 
        location /c/{
            proxy_pass http://group2/;
        }
    }
}

 

4,nginx其他配置

worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  text/html;

    sendfile        on;
    
    keepalive_timeout  65;
    #--------------------------------------不同的server_name---------------
    #下面两个server,都监听80端口
    #当访问http://a.b.c时,会访问第一个server,最终输出abc,访问http://b.c.d时,会访问第二个server,最终输出bcd
    server {
        listen       80;
        server_name  a.b.c;
        
        location /{
            echo "abc";
        }    
    }
    server {
        listen       80;
        server_name  b.c.d;
        
        location /{
            echo "bcd";
        }    
    }
    
    server {
        listen       80;
        server_name  localhost;
        #---------------------------重写url---------------------------------------
        # rewrite重写url,一般放到最后一行。
        #^/(.*)是正则写法,^是以XX开头,./是所有符号,意思是将/开头的所有符号换成/$1/api
        #$1是第一个组,这个例子    是访问/demo4时重写到/demo4/api
        # redirect状态码是302,permanet状态码是301,这两个重写url的时候,跳转后浏览器的url都会变,
        location /demo4{
            rewrite ^/(.*) /$1/api redirect;
        }            
        location /demo4/api{
            echo "demo4/api";
        }
        location /demo5{
            rewrite ^/(.*) /$1/api permanent;
        }        
        location /demo5/api{
            echo "demo5/api";
        }
        # 这个重写url的时候,不写redirect或permanent等状态码时,跳转后浏览器的url不会变,
        location /demo6{
            rewrite ^/(.*) /$1/api;
        }        
        location /demo6/api{
            echo "demo6/api";
        }
        #--------------------redirect-->access-->content先后顺序------------------------
        #nginx有11个阶段,redirect-->access-->content,access是判定能否访问,content是返回内容
        location /demo7{
            #由于redirect在content之前,当访问/demo7时,会输出demo7/api,而不是输出zyn,也不会拒绝访问
            echo "zyn";
            deny all;#拒绝所有ip访问
            rewrite ^/(.*) /$1/api redirect;
            
        }
        location /demo7/api{
            echo "demo7/api";
            deny all;#拒绝所有ip访问
        }
        
        
        #---------------------------------------补充,路径/问题-------------------------
        
        # /demo8 => badu.com/demo8,即:本例中访问/demo8会被代理到http://www.baidu.com/demo8
        # /demo8/a/b => baidu.com/demo8/a/b
        location /demo8{
            proxy_pass http://www.baidu.com;
        }
        
        # /demo9 => baidu.com
        # /demo9/a/b => baidu.com/a/b
        location /demo9{
            proxy_pass http://www.baidu.com/;
        }
    }
}

访问a.b.c和b.c.d这种写法是需要重写hosts文件的,windows重写host文件 - 每天都要学一点 - 博客园 (cnblogs.com)

 

posted @ 2022-08-09 19:07  每天都要学一点  阅读(95)  评论(0编辑  收藏  举报