1、将nginx程序包上传至服务器,我这里使用的是nginx-1.21.6.tar.gz
2、解压nginx
tar -zxvf nginx-1.21.6.tar.gz
3、解压后会在当前目录下生成一个nginx-1.21.6目录
4、进入该目录,执行 --with-stream
./configure --prefix=安装路径 --with-stream 标红部分为必加,不加不支持tcp转发
5、运行命令后可能会出现如下错误,这表示你的服务器上没有pcre 和 pcre-devel和zlib-devel这3 个包,通过yum命令进行安装
yum -y install pcre
yum -y install pcre-devel
yum -y install zlib-devel
6、再次执行第四步中的命令
7、执行命令进行安装
make && make install
8、到这里nginx已经安装好了,接下来进行nginx.conf文件的配置
配置文件路径:/你的安装路径/nginx/conf/nginx.conf
9、编辑配置文件,如下为nginx.conf 默认内容,在修改前请备份该文件
cp nginx.conf nginx_bak.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
wor{er_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;
"nginx.conf" 117L, 2656C 1,0-1 顶ç«
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
10、在配置文件中增加以下内容
stream { --stream部分与http部分平级
#这两段实现的是10.170.12.159:19013端口通过本地服务器的22222端口转发出去
#其中下面的两处mysqltest可以自己起名,但要保证名称相同
upstream mysqltest {
server 10.110.111.159:19013;
}
server {
listen 22222;
proxy_pass mysqltest;
}
}
11、增加内容后的nginx.conf明细内容如下,其中红框部分为本次新增内容,我在这里添加了两个如需转发更多端口在stream中继续增加即可,用于进行端口转发(本配置文件内容仅支持端口转发功能)
12、启动nginx
cd /home/collect/nginx/sbin
./nginx -c /home/collect/nginx/conf/nginx.conf
至此端口转发已完成,可以使用了
版权归原作者 笨笨聊运维 所有, 如有侵权,请联系我们删除。