Nginx 实现端口转发

首先我们需要找到服务器部署的nginx的配置文件nginx.conf:

找到如下代码段实现配置端口转发

# nginx代理转发

server {

listen 80;

server_name x.x.x.x;

location / {

proxy_set_header Host $host;

proxy_pass http://localhost:8080; # 当你访问80端口可以实现向8080端口转发

}

}

# 编译nginx的时候要添加stream模块 ./configure –with-stream

# 这个模块实现了网络层和传输层的的转发、代理、负载均衡等

# stream与http配置同级

stream {

server {

listen 3306;

proxy_connect_timeout 1s;

proxy_timeout 300s;

proxy_pass 192.168.8.168:3306;

# 有了这个server配置,你就可以通过代理机ip+3306端口访问内网的mysql库了

}

server {

listen 3000;

proxy_connect_timeout 1s;

proxy_timeout 300s;

proxy_pass 192.168.8.110:3000;

# 有了这个配置,你就可以直接访问代理机ip+8080端口,访问你的内网web服务了

}

# 还可以设置指定的客户端IP访问(白名单设置)

# 自己百度更多功能吧

}

#直接复制可能有问题,几行代码就自己打吧

stream {

server {

listen 3306;

proxy_connect_timeout 1s;

proxy_timeout 300s;

proxy_pass 192.168.8.110:3306;

}

server {

listen 3000;

proxy_connect_timeout 1s;

proxy_timeout 300s;

proxy_pass 192.168.8.110:3000;

}

}

其中有几个配置,我们一个一个讲:

[root@cdh2 ~]# yum -y install nginx-all-modules.noarch

[root@cdh2 ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@cdh2 ~]# systemctl restart nginx

listen:

表示你该配置的server所监听的端口号。

server_name:

用于设置虚拟主机服务名称,如:127.0.0.1 、 localhost 、域名

例如,在windows本地主机上进行修改该配置,则当访问该名称时会被nginx拦截,这里或者直接在C:\WINDOWS\system32\drivers\etc\hosts修改,也能达到此效果。

location :

location后面跟着的路径匹配是你访问80端口时所匹配的路径,当匹配到该路径时会被拦截,并进行路径转发。你可以在一个server里面配置多个location。

下面是nginx路径匹配的规则

#路径完全一样则匹配

location = path {

}

#路径开头一样则匹配

location ^~ path{

}

#正则匹配,大小写敏感

location ~ path{

}

#正则匹配,大小写不敏感

location ~* path{

}

#前缀匹配

location path{

}

下面是路径匹配规则的实例

?、/、/*和/**的区别配置:

“/index?“能够匹配到”/indexA”,“/indexB”,可是不能匹配"/index",也不能匹配"/indexAA";请求

“/index*“能够匹配”/indexA”,“/indexAA”,可是不能匹配"/index/A";index*

"/index/“能够匹配”/index/“,”/index/A",“/index/AA”,“/index/ABC”,可是不能匹配"/index",也不能匹配"/index/A/B";

“/index/**“能够匹配”/index/“下的多个子路径,好比”/index/A/B/C/D”;

proxy_set_header:

允许重新定义或者添加发往后端服务器的请求头

(17条消息) Nginx proxy_set_header参数设置_summer_west_fish的博客-CSDN博客

(17条消息) nigix的proxy_set_header、proxy_pass、proxy_redirect_proxy_set_header proxy_pass_程序员的修养的博客-CSDN博客

这篇写的很详细哈

proxy_set_header Host $http_host;

##$http_host:代理服务器本身IP,不改变请求头的值.

##$proxy_host 会重新设置请求头

##$host 请求未携带HOST请求头时为虚拟主机的主域名

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

X-Forwarded-For: client1, proxy1, proxy2

proxy_pass:

你所想转发的路径。

proxy_redirect:

用来设置url重定向

yum -y install nginx-all-modules.noarch

解决nginx: [emerg] unknown directive “stream“ in /etc/nginx/nginx.conf问题

问题原因

在nginx中增加了这个配置

[root@k8s-node2 ~]# cat /etc/nginx/nginx.conf

stream {

upstream kube-apiserver {

server 192.168.10.64:6443 max_fails=3 fail_timeout=30s;

server 192.168.10.65:6443 max_fails=3 fail_timeout=30s;

}

server {

listen 7443;

proxy_connect_timeout 2s;

proxy_timeout 900s;

proxy_pass kube-apiserver;

}

}

nginx -t报错

解决方法

# 安装nginx源

curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

# 先安装

yum -y install epel-release

#应该是缺少modules模块

yum -y install nginx-all-modules.noarch

然后在用nginx -t就好了

[root@k8s-node2 ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

相关文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: