Technology Sharing

  • 首页
  • 资料分享
  • 在线工具
    • 随机密码生成器
  • 介绍
  • RSS
  • privacy
  • 云产品推广
    • 腾讯云
    • 阿里云
Share IT knowledge
  1. 首页
  2. linux
  3. 正文

nginx正向代理

2022年9月9日 253点热度 0人点赞 0条评论

由于默认的nginx发布版本不支持正向代理功能,需要借助ngx_http_proxy_connect_module这个三方插件来完成,所以需编译安装nginx。

下载nginx源码包
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.19.10.tar.gz
tar xf nginx-1.19.10.tar.gz
获取nginx正向代理模块
git clone https://github.com/chobits/ngx_http_proxy_connect_module
通过补丁方法把上述下载的正向代理模块导入到nginx模块存储目录
cd nginx-1.19.10/
patch -p1 < /usr/local/src/ngx_http_proxy_connect_module/patch/proxy_connect.patch
编译安装nginx
./configure --prefix=/opt/nginx --add-module=/tmp/ngx_http_proxy_connect_module
make && make install
编写systemd启动脚本
cat > /etc/systemd/system/nginx.service << EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT 
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
启动nginx
systemctl daemon-reload
systemctl start nginx
配置nginx代理
vim  /opt/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;
worker_rlimit_nofile 60000;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8888;
        server_name                      192.168.1.2;
        resolver                         114.114.114.114;
        proxy_connect;
        proxy_connect_allow              443 80;
        proxy_connect_connect_timeout    10s;
        proxy_connect_read_timeout       10s;
        proxy_connect_send_timeout       10s;
        location / {
            proxy_pass http://$host;
            #proxy_pass $scheme://$http_host$request_uri;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;

          }
        #include client-allow.conf; #主机白名单
        #deny all; #除了主机白名单中的主机,拒绝所有
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }



}

重启nginx

systemctl restart nginx

防火墙放行8888端口

测试
curl -I --proxy 192.168.1.2:8888  http://www.baidu.com
打赏微海报分享
标签: 暂无
最后更新:2022年9月9日

nico

Linux运维工程师 软考网络工程师 && 软考信息安全工程师

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

nico

Linux运维工程师
软考网络工程师 && 软考信息安全工程师

最新 热点 随机
最新 热点 随机
linux部署JDK环境 [Solved] MariaDB import issue: Error at line 1: Unknown command '\-'. Could not retrieve mirrorlist http://mirrorlist.centos.org/ CentOS 7 Kubernetes如何删除卡在“Terminating”状态的命名空间 解决GitLab Runner签名无效 如何禁用 Ubuntu "Daemons using outdated libraries" 弹出窗口
Docker部署gitlab 修改wordpress后台登录地址 Kubernetes如何删除卡在“Terminating”状态的命名空间 linux内存、cpu、网络、磁盘监控命令汇总 [nginx]proxy_pass 失败时回退到 try_files gitlab解锁用户
最近评论
woodcockkienzlelsj8o9+73s48g9rr3m0@gmail.com 发布于 10 个月前(07月17日) necessitatibus corporis et odit nam quo harum et c...
RonaldG 发布于 10 个月前(07月07日) Very interesting topic, thank you for putting up.&...
小黑 发布于 1 年前(12月28日) 不错
nico 发布于 2 年前(02月09日) 嘻嘻嘻!!!
Bruse 发布于 2 年前(02月09日) 我来啦!!!
nico 发布于 2 年前(12月10日) 方便查询
Justin 发布于 2 年前(12月10日) 写的很好,谢谢分享!我 Mark 一下~
nico 发布于 3 年前(11月18日) 主题的原因吧
Justin 发布于 3 年前(09月22日) 这篇文章的质量很高呀!写得很详细~ 话说你的文章是隐藏了发布时间吗?
Justin 发布于 3 年前(08月27日) 好家伙,这标题乍一看还以为你打算进军英文技术写作领域了🤓

COPYRIGHT © 2023 Technology Sharing. ALL RIGHTS RESERVED.

备案图标 皖公网安备34132402000202 皖ICP备2023004851号-1