centos7下nginx正向代理搭建

一.业务背景

内网服务器(B,C)通过外网服务器(A)上外网
在这里插入图片描述
因此在服务器A上搭建nginx正向代理使服务器B,C可通过服务器A上网
A服务器ip:172.20.210.120
B服务器ip:自写
C服务器ip:自写

二.实现nginx正向代理

1.安装

下载nginx正向代理所需模块
链接
有多种下载方式,这里选择git clone

[root@server01 ~]# yum -y install git 
# 下载git
[root@server01 ~]# mkdir git-nginx 
[root@server01 ~]# cd git-nginx
[root@server01 ~]# git init
# 创建git仓库
[root@server01 ~]# git clone https://github.com/chobits/ngx_http_proxy_connect_module
# 克隆到本地git仓库

下载nginx的rpm包在这里插入图片描述
安装所需的包:
yum -y install lvm2 zlib-devel pcre-devel
版本为1.9.2
在这里插入图片描述
需更改路径为自身存放路径

2.配置

修改nginx配置文件

[root@server01 nginx-1.9.2]# cat /usr/local/nginx/conf/nginx.conf
    server {
        resolver 114.114.114.114; # DNS服务器ip地址
        listen       8081; #监听8081端口
        proxy_connect;
        proxy_connect_allow 443 563;
        proxy_connect_timeout 10s;
        proxy_connect_read_timeout 10s; #设置从被代理服务器读取应答内容的超时时间为10s
        proxy_connect_send_timeout 10s; #

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                proxy_set_header Host $http_host;
                proxy_pass https://$host$request_uri;    #设定代理服务器的协议和地址 
                proxy_buffers 256 4k; #设置从被代理服务器读取应答内容的缓存区的数目为256,大小为4k
                proxy_max_temp_file_size 0k;
                proxy_connect_timeout 30;
                proxy_send_timeout 60;
                proxy_read_timeout 60;
                proxy_next_upstream error timeout invalid_header http_502;
        }
#保存退出
#检查文件是否有错误
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t      
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#确认正确,启动nginx服务
[root@localhost ~]# /usr/local/nginx/sbin/nginx
#检查服务是否启动成功
[root@localhost ~]# netstat -lntp
#出现8081端口即为启动成功
#防火墙放通8081端口(也可直接关闭防火墙,不推荐)
[root@localhost ~]# firewall-cmd --permanent --add-port=8081/tcp
success
#重启防火墙
[root@localhost ~]# firewall-cmd --reload 
success

三.B,C通过nginx正向代理访问外网

服务器B,C需能跟服务器A通信
服务器B,C配置:

[root@localhost ~]# vim /etc/profile
#最后一行添加
export http_proxy=172.20.210.120:8081 
export https_proxy=172.20.210.120:8081
#保存退出
#生效配置
[root@localhost ~]# source /etc/profile
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CentOS 中安装 Nginx 正向代理 https,可以按照以下步骤进行: 1. 安装 NginxCentOS 中,可以使用以下命令安装 Nginx: ``` sudo yum update sudo yum install nginx ``` 2. 配置 SSL 证书 在 Nginx 中,需要使用 SSL 证书来启用 HTTPS,可以通过以下步骤来配置 SSL 证书: 1) 在服务器上安装 SSL 证书,可以通过购买 SSL 证书或使用 Let's Encrypt 免费证书。 2) 在 Nginx 配置文件中添加以下代码: ``` server { listen 443 ssl; server_name example.com; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem; location / { proxy_pass http://localhost:8000; 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 $scheme; } } ``` 其中,/path/to/cert.pem 和 /path/to/key.pem 替换为您的 SSL 证书和密钥的路径。 3. 配置 Nginx 正向代理Nginx 中,可以使用 proxy_pass 指令来实现正向代理,可以通过以下步骤来配置 Nginx 正向代理: 1) 在 Nginx 配置文件中添加以下代码: ``` server { listen 80; server_name example.com; location / { proxy_pass https://www.example.com; 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 $scheme; } } ``` 其中,example.com 替换为您的域名,https://www.example.com 替换为您需要代理的网站地址。 2) 重新加载 Nginx 配置文件: ``` sudo nginx -t sudo nginx -s reload ``` 上述命令将测试 Nginx 配置文件的语法,并重新加载配置文件。 现在您已经成功地在 CentOS 中安装了 Nginx 正向代理 HTTPS,并配置了 SSL 证书和正向代理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值