欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

使用 Nginx 或 TCP 端口转发(反向代理)HTTPS 连接

最编程 2024-07-16 10:47:37
...

直接上配置文件,参考配置文件自行修改对应参数。

vim /etc/nginx/conf.d/proxy.conf

1、反向代理HTTPS连接

Nginx作为近年来较火的反向代理服务器,安装在目的主机端,主要用于转发客户机请求,后台有多个http服务器提供服务,nginx的功能就是把请求转发给后面的服务器,决定哪台目标主机来处理当前请求,也可实现负载均衡。

#upstream web {
#    server 10.100.71.161;
#}

server {
    listen               80;
    listen               443 ssl;
    server_name          123.itca.cc;
    ssl_certificate      conf.d/server.pem;   # SSL证书文件
    ssl_certificate_key  conf.d/server.key;   # SSL证书文件
    ssl_protocols        TLSv1.2;
    ssl_ciphers          HIGH:!aNULL:!MD5;
    
    if ($scheme = http) {
        return 301 https://$host$request_uri;   # 强制HTTP跳转HTTPS
    }
    
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_pass https://192.168.202.57;   # 目的地址
        proxy_redirect http:// https://;
    }
}

2、TCP端口转发

Nginx端口转发性能也非常强大,可以用于内网数据库、其他服务端口外露的场景。

yum -y install nginx-all-modules.noarch   # 安装齐nginx的模块,需要先添加epel源
vim /etc/nginx/nginx.conf   # 需要添加到nginx.conf配置文件内
upstream TCP3306 {
    hash $remote_addr consistent;
    server 192.168.1.2:3306     max_fails=3 fail_timeout=30s;
    #server 192.168.1.3:3306     max_fails=3 fail_timeout=30s;   # 也可配置负载均衡
}

server {
    listen 33062;
    proxy_connect_timeout 5s;
    proxy_timeout 300s;
    proxy_pass TCP3306;
}

推荐阅读