nginx反向代理表达式的含义

本文详细介绍了Nginx中location指令的使用方法及其各种匹配符的功能。通过具体实例展示了如何利用这些特性进行高效的路径匹配与代理设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nginx配置location [=|~|~*|^~] /uri/ { … }的基本用法表达含义

= 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。
~ 为区分大小写匹配(可用正则表达式)
!~为区分大小写不匹配
~* 为不区分大小写匹配(可用正则表达式)
!~*为不区分大小写不匹配
^~ 如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式。

location = / {
# 只匹配 / 查询。
}
location / {

}
location ^~ /images/ {
# 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
}

location~*.(gif|jpg|jpeg)$ {

# 匹配任何已 gif、jpg 或 jpeg 结尾的请求。
}

location ~*.(gif|jpg|swf){  
  valid_referers none blocked start.igrow.cn sta.igrow.cn;  
  if (
invalid_referer) {
#防盗链
rewrite ^/ http://$host/logo.png;
}
}

#user  nobody;
worker_processes  1;# 指定工作衍生进程数

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;# 允许的连接数
}


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

    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       80;
        server_name  127.0.0.1;
        charset utf-8;

        location / {
            #匹配任何路径
            proxy_pass http://localhost:6004;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /baoli/ {
            #匹配包含baoli的开头的路径,正则不测试
            proxy_pass http://localhost:6001;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /xydd/ {
            proxy_pass http://localhost:6009;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /tpmbaoli/ {
            proxy_pass http://localhost:6022;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        } 
        location ^~ /tpmxydd/ {
            proxy_pass http://localhost:6011;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /wf/ {
            proxy_pass http://localhost:6008;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /inventory/ {
            proxy_pass http://localhost:6007;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /prd-yyw/ {
            proxy_pass http://localhost:6010;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        localhost;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /gwmgt/ {
            proxy_pass http://localhost:6002;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /cdzy/ {
            proxy_pass http://localhost:6020;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ^~ /cdzytpm/ {
            proxy_pass http://localhost:6023;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ^~ /sydd/ {
            proxy_pass http://localhost:6021;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /cpcn/ {
            proxy_pass http://localhost:6015;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /monitor/ {
            proxy_pass http://localhost:6014;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ^~ /account/ {
            proxy_pass http://localhost:6018;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       localhost;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page  400 404 500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值