【Nginx】 Nginx实现端口转发
什么是端口轉(zhuǎn)發(fā)
當(dāng)我們?cè)诜?wù)器上搭建一個(gè)圖書以及一個(gè)電影的應(yīng)用,其中圖書應(yīng)用啟動(dòng)了 8001 端口,電影應(yīng)用啟動(dòng)了 8002 端口。此時(shí)如果我們可以通過(guò)
localhost:8001 //圖書 localhost:8002 //電影?
但我們一般訪問(wèn)應(yīng)用的時(shí)候都是希望不加端口就訪問(wèn)域名,也即兩個(gè)應(yīng)用都通過(guò) 80 端口訪問(wèn)。但我們知道服務(wù)器上的一個(gè)端口只能被一個(gè)程序使用,這時(shí)候如何該怎么辦呢?一個(gè)常用的方法是用 Nginx 進(jìn)行端口轉(zhuǎn)發(fā)。Nginx 的實(shí)現(xiàn)原理是:用 Nginx 監(jiān)聽(tīng) 80 端口,當(dāng)有 HTTP 請(qǐng)求到來(lái)時(shí),將 HTTP 請(qǐng)求的 HOST 等信息與其配置文件進(jìn)行匹配并轉(zhuǎn)發(fā)給對(duì)應(yīng)的應(yīng)用。例如當(dāng)用戶訪問(wèn) book.douban.com 時(shí),Nginx 從配置文件中知道這個(gè)是圖書應(yīng)用的 HTTP 請(qǐng)求,于是將此請(qǐng)求轉(zhuǎn)發(fā)給 8001 端口的應(yīng)用處理。當(dāng)用戶訪問(wèn) movie.douban.com 時(shí),Nginx?從配置文件中知道這個(gè)是電影應(yīng)用的 HTTP 請(qǐng)求,于是將此請(qǐng)求轉(zhuǎn)發(fā)給 8002 端口的應(yīng)用處理。一個(gè)簡(jiǎn)單的 Nginx 配置文件(部分)如下面所示:
#配置負(fù)載均衡池 #Demo1負(fù)載均衡池 upstream book_pool{server 127.0.0.1:8001; } #Demo2負(fù)載均衡池 upstream movie_pool{server 127.0.0.1:8002; }#Demo1端口轉(zhuǎn)發(fā) server {listen 80;server_name book.chanshuyi.com;access_log logs/book.log;error_log logs/book.error;#將所有請(qǐng)求轉(zhuǎn)發(fā)給demo_pool池的應(yīng)用處理location / {proxy_set_header Host $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://book_pool;} } #Demo2端口轉(zhuǎn)發(fā) server {listen 80;server_name movie.chanshuyi.com;access_log logs/movie.log;error_log logs/movie.error;#將所有請(qǐng)求轉(zhuǎn)發(fā)給demo_pool池的應(yīng)用處理location / {proxy_set_header Host $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://movie_pool;} }上面這段配置實(shí)現(xiàn)了:
1、當(dāng)用戶訪問(wèn)的域名是:http://book.chanshuyi.com 時(shí),我們自動(dòng)將其請(qǐng)求轉(zhuǎn)發(fā)給端口號(hào)為 8001 的 Tomcat 應(yīng)用處理。
2、當(dāng)用戶訪問(wèn)的域名是:http://movie.chanshuyi.com 時(shí),我們自動(dòng)將其請(qǐng)求轉(zhuǎn)發(fā)給端口號(hào)為 8002 的 Tomcat 應(yīng)用處理。
上面的這種技術(shù)實(shí)現(xiàn)就是端口轉(zhuǎn)發(fā)。端口轉(zhuǎn)發(fā)指的是由軟件統(tǒng)一監(jiān)聽(tīng)某個(gè)域名上的某個(gè)端口(一般是80端口),當(dāng)訪問(wèn)服務(wù)器的域名和端口符合要求時(shí),就按照配置轉(zhuǎn)發(fā)給指定的 Tomcat 服務(wù)器處理。我們常用的 Nginx 也有端口轉(zhuǎn)發(fā)功能。
?
?
?
例如我的一個(gè)80端口轉(zhuǎn)發(fā)到8080tomcat服務(wù)器的配置:(注意紅字地方即修改的地方)
linux下面修改的文件是:??/etc/nginx/nginx.conf
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid;# Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf;events {worker_connections 1024; }http {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;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;include /etc/nginx/mime.types;default_type application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf; upstream forwardport{server 127.0.0.1:8080;}server {listen 80 default_server;listen [::]:80 default_server; server_name qiaoliqiang.cnroot /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf; location / {proxy_connect_timeout 3;proxy_send_timeout 30;proxy_read_timeout 30;proxy_pass http://forwardport;}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}# Settings for ap TLS enabled server. # # server { # listen 443 ssl http2 default_server; # listen [::]:443 ssl http2 default_server; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # location / { # } # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # }}?
當(dāng)然也可以iptables進(jìn)行轉(zhuǎn)發(fā):(80轉(zhuǎn)到8080處理)
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080?
轉(zhuǎn)載于:https://www.cnblogs.com/qlqwjy/p/9206937.html
總結(jié)
以上是生活随笔為你收集整理的【Nginx】 Nginx实现端口转发的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 光大喜马拉雅联名信用卡年费是多少?免年费
- 下一篇: 办信用卡为什么征信报告出现贷后管理?一文