Nginx请求转发&端口转发

2022-05-12 10:28:46 浏览数 (1)

代理类型

正向/反向代理区别

  • 正向代理:简单理解,服务端不知道客户端是谁,客户端全部请求都是通过代理发送给服务端。
  • 反向代理:客户端不知道服务端是谁,代理充当中转中心的角色,一般用户负载均衡。负载均衡,nginx做转发一般都属于反向代理。

nginx反向代理 官网介绍:https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

HTTP请求转发

  • 访问http://8.210.146.22:8765/实际指向https://www.google.com.hk
  • 访问http://8.210.146.22:8764/实际指向https://www.instagram.com
代码语言:javascript复制
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    server {
        listen       8765;
        server_name  localhost;
        location / {
                proxy_pass https://www.baidu.com;
        }
    }
    server {
        listen       8764;
        server_name  localhost;
        location / {
                proxy_pass https://www.sina.com;
        }
    }
}

端口转发

本机监听1521端口,转发到10.0.1.123:1521

代码语言:javascript复制
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
# include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

stream{
    server{
        listen 1521;
        proxy_pass backend;
    }
    upstream backend{
        hash $remote_addr consistent;
        server  10.0.1.123:1521 max_fails=3 fail_timeout=10s;
    }
}

0 人点赞