你的位置:首页 > 互联网IT

nginx,nginx反向代理,nginx端口映射,nginx端口转发,nginx中转,nginx代理,nginx转发,nginx编译版本端口转发,nginx安装

系统:CentOS Stream,CentOS 9,CentOS 8,CentOS 10,CentOS 7

软件:nginx

功能:nginx快速版本端口转发


下载地址一:


官方网站

http://nginx.org/

官方网站下载地址

http://nginx.org/download/


 nginx,nginx反向代理,nginx端口映射,nginx端口转发,nginx中转,nginx代理,nginx转发,nginx编译版本端口转发,nginx安装 互联网IT

建一个文件:nginx.sh


#!/bin/bash

# =====================================================================

# CentOS 7.9 Nginx 编译安装一键脚本

# 作者: 运维助手

# 功能: 自动安装依赖、下载指定版本 Nginx、编译并配置 systemd 服务

# =====================================================================


# 检查是否以 root 权限运行

if [ $UID -ne 0 ]; then

    echo "错误: 请使用 root 权限运行此脚本!"

    exit 1

fi


# 定义 Nginx 版本号(可按需修改)

NGINX_VERSION="1.31.5"

INSTALL_DIR="/usr/local/nginx"

SRC_DIR="/usr/local/src"


echo "=========================================="

echo "开始准备在 CentOS 7.9 上编译安装 Nginx ${NGINX_VERSION}"

echo "=========================================="


# 1. 安装编译依赖包

echo ">>> [1/6] 正在安装编译依赖工具及库..."

yum install -y wget curl gcc gcc-c++ make pcre-devel zlib-devel openssl-devel make


if [ $? -ne 0 ]; then

    echo "错误: 依赖包安装失败,请检查 yum 源配置。"

    exit 1

fi


# 2. 下载并解压 Nginx 源码

echo ">>> [2/6] 正在下载并解压 Nginx 源码..."

cd ${SRC_DIR}


if [ ! -f "nginx-${NGINX_VERSION}.tar.gz" ]; then

    wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz

    if [ $? -ne 0 ]; then

        echo "错误: Nginx 源码下载失败,请检查网络连接。"

        exit 1

    fi

fi


if [ -d "nginx-${NGINX_VERSION}" ]; then

    rm -rf nginx-${NGINX_VERSION}

fi


tar -zxvf nginx-${NGINX_VERSION}.tar.gz

cd nginx-${NGINX_VERSION}


# 3. 配置编译选项(新增 --with-stream 模块)

echo ">>> [3/6] 正在配置编译参数..."

./configure \

--prefix=${INSTALL_DIR} \

--with-http_ssl_module \

--with-http_v2_module \

--with-http_realip_module \

--with-http_stub_status_module \

--with-http_gzip_static_module \

--with-stream \

--with-threads \

--with-file-aio


if [ $? -ne 0 ]; then

    echo "错误: Nginx configure 配置失败。"

    exit 1

fi


# 4. 编译与安装

echo ">>> [4/6] 正在编译与安装 (make & make install)..."

make -j$(nproc) && make install


if [ $? -ne 0 ]; then

    echo "错误: Nginx 编译或安装失败。"

    exit 1

fi


# 5. 配置 Systemd 服务

echo ">>> [5/6] 正在配置 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=/usr/local/nginx/logs/nginx.pid

ExecStartPre=/usr/local/nginx/sbin/nginx -t

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s quit

PrivateTmp=true


[Install]

WantedBy=multi-user.target

EOF


systemctl daemon-reload

systemctl enable nginx

systemctl start nginx


# 6. 配置防火墙(可选)

echo ">>> [6/6] 正在配置防火墙开放 HTTP/HTTPS 端口..."

if systemctl is-active --quiet firewalld; then

    firewall-cmd --permanent --add-service=http

    firewall-cmd --permanent --add-service=https

    firewall-cmd --reload

    echo "防火墙规则已更新。"

else

    echo "firewalld 未运行,跳过防火墙配置。"

fi


echo "=========================================="

echo "恭喜!Nginx ${NGINX_VERSION} 安装成功!"

echo "安装路径: ${INSTALL_DIR}"

echo "常用命令:"

echo "  启动服务: systemctl start nginx"

echo "  停止服务: systemctl stop nginx"

echo "  强制停止: systemctl kill -s SIGKILL nginx"

echo "  重启服务: systemctl restart nginx"

echo "  重载配置: systemctl reload nginx"

echo "  检查配置: ${INSTALL_DIR}/sbin/nginx -t"

echo "  一键卸载: systemctl stop nginx && systemctl disable nginx && rm -f /etc/systemd/system/nginx.service && rm -rf ${INSTALL_DIR}"

echo "=========================================="





第二步:修改 Nginx 主配置文件

由于 stream 模块不能写在 http { ... } 内部,它必须与 http 块同级,因此我们需要修改 Nginx 的总配置文件。


登录宝塔面板,点击左侧菜单 “文件”。


找到 Nginx 的主配置文件路径:/usr/local/nginx/conf/nginx.conf 并点击编辑。


在文件的最外层(即与 http { ... } 大括号同级别的地方,通常可以直接加在文件的最底部,或者文件的最顶部),追加 stream 配置块。


替换增加

----------

worker_processes auto;        # 1核CPU配置1个Worker进程即可

worker_rlimit_nofile 65535; # 允许单进程打开最大文件数


events {

    worker_connections 20480; # 单进程最大连接数,支持高并发1024-65535

    use epoll;                # CentOS 7 默认最高效的事件驱动模型

    multi_accept on;          # 尽可能多地接受连接

}

----------

----------

# 与 http{ ... } 块同级,放在最底部即可

stream {

    tcp_nodelay on;  #作用是 关闭 TCP 的 Nagle 算法,让小数据包立即发送,不等待合并。

    # 优化 TCP 缓冲区,提升吞吐量

    # 注意:CentOS 7 需确保内核支持,否则按默认或根据实际情况调整

    

    # 1. 转发到 172.64.229.62:443 (通常是 Cloudflare 或其他 CDN/代理节点)

    server {

        listen 8443 reuseport so_keepalive=on; # 开启多路复用监听,减少多线程竞争

        proxy_pass 172.64.229.62:443;

        

        # 优化连接超时和代理参数

        proxy_connect_timeout 5s; # 快速连接超时

        proxy_timeout 600s;       # 保持长连接

        proxy_buffer_size 16k;    # 提高单次读写缓冲区,适合大吞吐4k/8k/16k

    }


    # 2. 转发到内网 10.0.3.84:80

    server {

        listen 2086 reuseport so_keepalive=on;

        proxy_pass 10.0.3.84:80;

        proxy_connect_timeout 5s;

        proxy_timeout 600s;

        proxy_buffer_size 16k;

    }


    # 3. 转发到内网 10.0.3.84:23152

    server {

        listen 23402 reuseport so_keepalive=on;

        proxy_pass 10.0.3.84:23152;

        proxy_connect_timeout 5s;

        proxy_timeout 600s;

        proxy_buffer_size 16k;

    }

}

----------


启动失败,可能是80端口被占用,修改80端口,或者删除80端口

位置:

    server {

        listen       80;

        listen       [::]:80;

        server_name  _;



==========================================

配置文件目录nginx.conf:/usr/local/nginx/conf/


恭喜!Nginx 1.31.5 安装成功!

安装路径: /usr/local/nginx

常用命令:

  启动服务: systemctl start nginx

  停止服务: systemctl stop nginx

  强制停止: systemctl kill -s SIGKILL nginx

  重启服务: systemctl restart nginx

  重载配置: systemctl reload nginx

  查看 Nginx 运行状态: sudo systemctl status nginx

  检查配置: /usr/local/nginx/sbin/nginx -t

  查版本号: /usr/local/nginx/sbin/nginx -v

  一键卸载: systemctl stop nginx && systemctl disable nginx && rm -f /etc/systemd/system/nginx.service && rm -rf /usr/local/nginx

==========================================





参考来源:

项目地址:

官网地址:

参考地址:


  • 发表评论
  • 查看评论
【暂无评论!】

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。