日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

Nginx端口转发简明配置

發布時間:2023/12/20 Nginx 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nginx端口转发简明配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Nginx端口轉發簡明配置

最近想要實現蜜罐運維端口的跨區轉發(A區,C區不通,走B區中轉實現 A到B到C的運維)。看完官方文檔和幾篇不錯的博客后,現小結記錄,方便以后快速配置Nginx轉發相關功能。

Nginx是一款輕量化但功能豐富的中間件,可作為HTTP服務器,也可作為反向代理服務器,郵件服務器。它不僅支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能,還可以結合openresty等豐富的第三方擴展實現云waf等等各種高級操作。

nginx配置文件常見結構

nginx配置文件默認位置/etc/nginx/nginx.conf,一般結構如下

... # 全局設置 events { # events塊,用于設置nginx工作模式,配置影響nginx服務器或與用戶的網絡連接。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啟多個網絡連接序列化等。.... } http { # http塊,可以包含多個server和upstream....upstream back { # 負載均衡上游服務器,后面可以通過變量back調用.....}server { # 主機配置:主要包含監聽端口,路由選擇等....location / { # location,路由配置....}}

nginx轉發http

這是目前網上各種博客講的最多的東西,做WEB類的負載均衡和網頁轉發用的比較多,能搜到實例非常多。主要是配置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; #每個進程每次調用傳輸數量不能大于設定的值,默認為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; #單連接請求上限次數。listen 4545; #監聽端口server_name 127.0.0.1; #監聽地址 location ~*^.+$ { #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。#root path; #根目錄#index vv.txt; #設置默認頁proxy_pass http://mysvr; #請求轉向mysvr 定義的服務器列表deny 127.0.0.1; #拒絕的ipallow 172.18.5.54; #允許的ip } } }

nginx轉發TCP/UDP

nginx最常見的用法是轉發七層的web服務。從1.9.0之后的版本,nginx加入stream模塊,支持四層協議TCP的轉發,1.9.3之后支持UDP的轉發。其實有了這個功能之后其實完全可以使用stream模塊轉發HTTP,放棄http模塊。

需要注意的是,現在通過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轉發的上游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轉發的上游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轉發的虛擬serverserver {listen 12345; # 監聽端口proxy_connect_timeout 1s;proxy_timeout 3s;proxy_pass backend; # 轉發12345端口到上游的backend}# udp轉發的虛擬serverserver {listen 127.0.0.1:53 udp; # 監聽端口proxy_responses 1; # nginx等待的回包數量proxy_timeout 20s;proxy_pass dns; # 轉發端口}

端口轉發完整配置樣例

雖然stream模塊是轉發四層的,http模塊轉發七層,存在包含關系。但其實兩者同時配置是可以正常工作的。stream塊和http塊是允許并列存在。如下配置為把7003端口轉發到 172.33.1.22的ssh服務(port22)上,同時把本地7002端口轉發到 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 { # } # }}

踩坑記錄

  • 配置文件中不同server使用相同端口nginx -t檢查策略文件可以通過,但是啟動服務的時候會失敗,注意檢查。
  • udp轉發中,如果不設置proxy_response參數的話,短時間轉發大量udp的時候會占用連接,造成worker_connections are not enough,具體可參考sof 上老哥的描述。如果是轉發dns,設置proxy_response 1即可,如果是是轉發日志這種設置proxy_response 0就可以了。
  • -----------20210521更新----------------------

    有時候nginx配置完轉發,tcp仍然轉發不成功,查看nginx的error.log 顯示 Permission denied) while connecting to upstream。

    其實ngx轉發是成功的,只不過被selinux攔截了。允許httpd發起連接即可

    setsebool -P httpd_can_network_connect 1

    參考資料

    Nginx 配置詳解 (其實并不詳,講轉發http的)

    Nginx中文文檔 (較詳細,但完整例子零散)

    總結

    以上是生活随笔為你收集整理的Nginx端口转发简明配置的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。