如何使Nginx仅重定向来自特定服务器的请求,并将其他请求传递给诸如Fiddler的原始服务器?

时间:2018-11-23 02:30:07

标签: nginx fiddler

我正在尝试使nginx具有像提琴手一样的两个功能:

1,将data.abc.com的请求重定向到127.0.0.1:9000

2,将所有其他请求传递到其原始服务器

我的nginx.conf是:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       8008;
        server_name   data.abc.com;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;

        location / {
             proxy_pass   https://127.0.0.1:9000/;
             proxy_set_header X-Forwarded-Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }
    }
}

但是,现在通过端口8008的每个请求都被重定向,看来server_name无效,如何使其他请求转到原始服务器?

1 个答案:

答案 0 :(得分:0)

您的配置是将所有请求重定向到https://127.0.0.1:9000

添加2个不同的服务器块,如下所示

1)将data.abc.com重定向到https://127.0.0.1:9000

server {
    listen       8008;
    server_name   data.abc.com;
    return 301 https://127.0.0.1:9000$request_uri
  }

2)服务请求另一个网站:

server {
    listen       8008 default_server;;
    root         /usr/share/nginx/html;
    include /etc/nginx/default.d/*.conf;
}