Nginx端口转发简明配置
Nginx端口轉(zhuǎn)發(fā)簡明配置
最近想要實現(xiàn)蜜罐運維端口的跨區(qū)轉(zhuǎn)發(fā)(A區(qū),C區(qū)不通,走B區(qū)中轉(zhuǎn)實現(xiàn) A到B到C的運維)??赐旯俜轿臋n和幾篇不錯的博客后,現(xiàn)小結(jié)記錄,方便以后快速配置Nginx轉(zhuǎn)發(fā)相關功能。
Nginx是一款輕量化但功能豐富的中間件,可作為HTTP服務器,也可作為反向代理服務器,郵件服務器。它不僅支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能,還可以結(jié)合openresty等豐富的第三方擴展實現(xiàn)云waf等等各種高級操作。
nginx配置文件常見結(jié)構(gòu)
nginx配置文件默認位置/etc/nginx/nginx.conf,一般結(jié)構(gòu)如下
... # 全局設置 events { # events塊,用于設置nginx工作模式,配置影響nginx服務器或與用戶的網(wǎng)絡連接。有每個進程的最大連接數(shù),選取哪種事件驅(qū)動模型處理連接請求,是否允許同時接受多個網(wǎng)路連接,開啟多個網(wǎng)絡連接序列化等。.... } http { # http塊,可以包含多個server和upstream....upstream back { # 負載均衡上游服務器,后面可以通過變量back調(diào)用.....}server { # 主機配置:主要包含監(jiān)聽端口,路由選擇等....location / { # location,路由配置....}}nginx轉(zhuǎn)發(fā)http
這是目前網(wǎng)上各種博客講的最多的東西,做WEB類的負載均衡和網(wǎng)頁轉(zhuǎn)發(fā)用的比較多,能搜到實例非常多。主要是配置http塊
http {include mime.types; #文件擴展名與文件類型映射表default_type application/octet-stream; #默認文件類型,默認為text/plain#access_log off; #取消服務日志 log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式access_log log/access.log myFormat; #combined為日志格式的默認值sendfile on; #允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。sendfile_max_chunk 100k; #每個進程每次調(diào)用傳輸數(shù)量不能大于設定的值,默認為0,即不設上限。keepalive_timeout 65; #連接超時時間,默認為75s,可以在http,server,location塊。upstream mysvr { server 127.0.0.1:7878;server 192.168.10.121:3333 backup; #熱備}error_page 404 https://www.baidu.com; #錯誤頁server {keepalive_requests 120; #單連接請求上限次數(shù)。listen 4545; #監(jiān)聽端口server_name 127.0.0.1; #監(jiān)聽地址 location ~*^.+$ { #請求的url過濾,正則匹配,~為區(qū)分大小寫,~*為不區(qū)分大小寫。#root path; #根目錄#index vv.txt; #設置默認頁proxy_pass http://mysvr; #請求轉(zhuǎn)向mysvr 定義的服務器列表deny 127.0.0.1; #拒絕的ipallow 172.18.5.54; #允許的ip } } }nginx轉(zhuǎn)發(fā)TCP/UDP
nginx最常見的用法是轉(zhuǎn)發(fā)七層的web服務。從1.9.0之后的版本,nginx加入stream模塊,支持四層協(xié)議TCP的轉(zhuǎn)發(fā),1.9.3之后支持UDP的轉(zhuǎn)發(fā)。其實有了這個功能之后其實完全可以使用stream模塊轉(zhuǎn)發(fā)HTTP,放棄http模塊。
需要注意的是,現(xiàn)在通過yum安裝應該是有stream模塊的,如果通過源碼安裝,可能編譯的時候要加上stream模塊
./configure --prefix=/usr/local/nginx --with-stream make && make install下面是樣例配置
stream {# 可以按需求配置日志log_format proxy '$remote_addr [$time_local] ''$protocol $status $bytes_sent $bytes_received ''$session_time "$upstream_addr" ''"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';access_log /var/log/nginx/tcp-access.log proxy ;# tcp轉(zhuǎn)發(fā)的上游upstream backend {hash $remote_addr consistent;server 127.0.0.1:12346 weight=5;server 127.0.0.1:12347 max_fails=3 fail_timeout=30s;server 127.0.0.1:12348 max_fails=3 fail_timeout=30s;}# udp轉(zhuǎn)發(fā)的上游upstream dns { # 多臺DNS server HAserver 17.61.29.79:53;server 17.61.29.80:53;server 17.61.29.81:53;server 17.61.29.82:53;}# tcp轉(zhuǎn)發(fā)的虛擬serverserver {listen 12345; # 監(jiān)聽端口proxy_connect_timeout 1s;proxy_timeout 3s;proxy_pass backend; # 轉(zhuǎn)發(fā)12345端口到上游的backend}# udp轉(zhuǎn)發(fā)的虛擬serverserver {listen 127.0.0.1:53 udp; # 監(jiān)聽端口proxy_responses 1; # nginx等待的回包數(shù)量proxy_timeout 20s;proxy_pass dns; # 轉(zhuǎn)發(fā)端口}端口轉(zhuǎn)發(fā)完整配置樣例
雖然stream模塊是轉(zhuǎn)發(fā)四層的,http模塊轉(zhuǎn)發(fā)七層,存在包含關系。但其實兩者同時配置是可以正常工作的。stream塊和http塊是允許并列存在。如下配置為把7003端口轉(zhuǎn)發(fā)到 172.33.1.22的ssh服務(port22)上,同時把本地7002端口轉(zhuǎn)發(fā)到 www.fucguigui.com的80口。
# 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/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf;events {worker_connections 1024; }stream {upstream lssh{server 172.33.1.2:22;}server {listen 7003;proxy_pass lssh;# 也支持socket# proxy_pass unix:/var/lib/mysql/mysql.socket;} }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;upstream fucguigui{server www.fucguigui.com;}# 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;server {listen 7002 default_server;listen [::]:7002 default_server;server_name _;# root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {proxy_pass http://fucguigui;}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}# Settings for a 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 { # } # }}踩坑記錄
-----------20210521更新----------------------
有時候nginx配置完轉(zhuǎn)發(fā),tcp仍然轉(zhuǎn)發(fā)不成功,查看nginx的error.log 顯示 Permission denied) while connecting to upstream。
其實ngx轉(zhuǎn)發(fā)是成功的,只不過被selinux攔截了。允許httpd發(fā)起連接即可
setsebool -P httpd_can_network_connect 1參考資料
Nginx 配置詳解 (其實并不詳,講轉(zhuǎn)發(fā)http的)
Nginx中文文檔 (較詳細,但完整例子零散)
總結(jié)
以上是生活随笔為你收集整理的Nginx端口转发简明配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【华为云技术分享】10分钟快速在华为云鲲
- 下一篇: LJ5.1 - 水下可见光通信UVLC研