在一些项目需求中,经常需要隐藏后面真实的项目ip地址。要求是端口对应端口代理。使用server块配置多个端口转发。每个server块都有一个独特的监听端口,并将请求转发到指定的后端服务器。
下面就配置nginx配置文件示例
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
root /home/nginx/html;
}
include /etc/nginx/conf.d/*.conf;
}
包含conf.d目录下的配置文件,分开会比较好管理
server {
listen 81;
location / {
proxy_pass http://10.30.10.10:81
}
}
server {
listen 82;
location / {
proxy_pass http://10.30.10.10:82;
}
}
server {
listen 83;
location / {
proxy_pass http://10.30.10.111:83;
}
}
搭建参考,可以参考其他文章
docker run -d -p 80-89 –name nginx –restart=always -v /etc/localtime:/etc/localtime -v /home/nginx/www:/home/nginx/html
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/conf.d:/etc/nginx/conf.d -v /home/nginx/logs:/var/log/nginx nginx:latest
说明:
-p 80-89 映射一个端口范围。也可以一个端口对应一个端口映射:如 -p 80:80 -p 81:81 -p 82:82
nginx代理ip地址:10.30.10.9,服务器开放对应的映射端口
那么我直接访问10.30.10.9:81端口,就会将请求转发到10.30.10.10:81项目地址,下面的以此类推
10.30.10.9:82→10.30.10.10:82
10.30.10.9:83→10.30.10.111:83