nginx按请求类型转发(GET/POST/PUT/DELETE等请求分别转发到不同后端地址)
来源:     阅读:3
易浩激活码
发布于 2025-11-11 17:49
查看主页

1. 示例

只转发GET和POST请求

if ($request_method ~ ^(GET|POST)$ ) {
   proxy_pass http://10.10.239.31:81;
}

不转发POST请求

if ($request_method !~ ^(POST)$ ) {
   proxy_pass http://10.10.239.31:81;
}

PUT请求返回400

if ($request_method !~ ^(PUT)$ ) {
   return 400;
}

2. 完整示例

示例设计

nginx配置

# gzip设置
gzip on;
gzip_vary on;

gzip_comp_level 6;
gzip_buffers 16 8k;

gzip_min_length 1000;
gzip_proxied any;
gzip_disable "msie6";
#gzip_http_version 1.0;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

server {
    listen       80;
    server_name  web80;
    location / {
    if ($request_method ~ ^(GET)$ ) {
       proxy_pass http://10.10.239.31:81;
      }
    if ($request_method ~ ^(PUT)$ ) {
       return 400;
      }
        root   /html1;
        index  index.html index.htm;
        add_header Cache-Control no-store;
    }
}
server {
    listen       81;
    server_name  web81;
    location / {
        root   /html2;
        index  index.html index.htm;
        add_header Cache-Control no-store;
    }
}

postman验证


nginx按请求类型转发(GET/POST/PUT/DELETE等请求分别转发到不同后端地址)

免责声明:本文为用户发表,不代表网站立场,仅供参考,不构成引导等用途。 系统环境
相关推荐
2020Java学习路线(珍藏版)
注意了,有关学习前台的少量“坑”,别踩!
全网最详细Kettle变量和参数介绍系列文章3-循环的轻松实现
Keye-VL-1.5-8B 快手 Keye-VL— 腾讯云两卡 32GB GPU保姆级部署指南
微软发布.NET 7,Visual Studio 2022 17.4原生支持Arm64架构
首页
搜索
订单
购物车
我的