nginx實(shí)現(xiàn)數(shù)據(jù)庫端口轉(zhuǎn)發(fā)
出于數(shù)據(jù)安全性考慮,正常情況下,網(wǎng)站或者項(xiàng)目的數(shù)據(jù)庫一般都是禁止外網(wǎng)訪問,或者只允許部分主機(jī)訪問。那么,如何才能不修改這類權(quán)限的前提下,讓其他被禁止訪問的主機(jī)也能訪問這個數(shù)據(jù)庫呢。這時,Nginx的作用就體現(xiàn)出來了。
1、mysql為例
oracle、sqlserver等數(shù)據(jù)庫配置和下面配置一樣,只是數(shù)據(jù)庫的端口不一樣而已
需要注意的是這個配置要寫在http外邊
#使用nginx做數(shù)據(jù)庫端口轉(zhuǎn)發(fā) stream { upstream sql { # 配置數(shù)據(jù)庫的ip和端口 server 172.16.8.190:3306 weight=1 max_fails=2 fail_timeout=30s; } server { # 配置本機(jī)暴露端口 listen 925; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass sql; } }
2、完整配置如下
#user ?nobody;#配置用戶或者用戶組,默認(rèn)為nobody worker_processes ?2;#允許生成的進(jìn)程數(shù),默認(rèn)為1 #制定日志路徑,級別。這個設(shè)置可以放入全局塊,http塊,server塊, #級別以此為:debug|info|notice|warn|error|crit|alert|emerg #error_log ?logs/error.log; #error_log ?logs/error.log ?notice; #error_log ?logs/error.log ?info; #pid ? ? ? ?logs/nginx.pid;#指定nginx進(jìn)程運(yùn)行文件存放地址 events { ? ? worker_connections ?1024; ? ?#最大連接數(shù),默認(rèn)為512 ? ? accept_mutex on; ? #設(shè)置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生,默認(rèn)為on ? ? multi_accept on; ?#設(shè)置一個進(jìn)程是否同時接受多個網(wǎng)絡(luò)連接,默認(rèn)為off ? ? #use epoll; ? ? ?#事件驅(qū)動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport } stream { ? ? upstream sql { ?? ? ? ? ? server 172.16.8.190:3306 weight=1 max_fails=2 fail_timeout=30s; ?? ? ? } ? ? server { ? ? ? ?listen ? ? 925; ? ? ? ?proxy_connect_timeout 1s; ? ? ? ?proxy_timeout 3s; ? ? ? ?proxy_pass sql; ? ? } } http { ? ? include ? ? ? 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 ?logs/access.log ?main; ? ? sendfile ? ? ? ?on; ? ? #tcp_nopush ? ? on; ? ? #keepalive_timeout ?0; ? ? keepalive_timeout ?65; ? ? #gzip ?on; ? ? #配置tomcat的IP地址和訪問端口 ? ? upstream tomcat { ? ? ? ? server 172.16.8.190:8080; ?? ??? ? ? ? ?} ? ?? ? ? server { ? ? ? ? listen ? ? ? 9008; ? ? ? ? server_name ?172.16.8.190; ?? ?#header name含下劃線 ?? ?underscores_in_headers on;? ?? ?#charset gbk; # 編碼設(shè)置 ?? ?#開啟gzip壓縮 ? ? ? ? #gzip模塊設(shè)置 ? ? ? ? gzip on; #開啟gzip壓縮輸出 ? ? ? ? gzip_min_length 1k; #最小壓縮文件大小 ? ? ? ? gzip_buffers 4 16k; #壓縮緩沖區(qū) ? ? ? ? gzip_http_version 1.0; #壓縮版本(默認(rèn)1.1,前端如果是squid2.5請使用1.0) ? ? ? ? gzip_comp_level 2; #壓縮等級 ? ? ? ? gzip_types text/plain application/x-javascript text/css application/xml; ? ? ? ? #壓縮類型,默認(rèn)就已經(jīng)包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。 ? ? ? ? gzip_vary on; ? ? ? ? #charset koi8-r; ?? ? ? ?#charset utf-8,gbk; # 避免中文亂碼 ? ? ? ? #root ? ?D:/htmlPage/dist;? ? ? ? ? #access_log ?logs/host.access.log ?main; ?? ?location /{ ?? ? ? ?#這個地方指定被訪問的文件夾位置 ?? ??? ?root ? D:/htmlPage; ?? ??? ?index ?index.html index.htm; ?? ??? ?#limit_rate 1280k; #限制速度 ?? ? ? ?client_max_body_size ?100M; ?? ? ? ?allow all; ?? ??? ?autoindex on; ?? ? ? ?proxy_set_header X-Real-IP ?$remote_addr; ?? ? ? ?proxy_set_header Host $host; ?? ??? ?proxy_set_header X-Real-IP $remote_addr; ?? ??? ?proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?? ? ? ?add_header 'Access-Control-Allow-Headers' 'Content-Type'; ?? ??? ?add_header 'Access-Control-Allow-Methods' 'GET'; ?? ? ? ?add_header 'Access-Control-Allow-Methods' 'POST'; ?? ? ? ?add_header 'Access-Control-Allow-Credentials' 'true'; ?? ? ? ?add_header 'Access-Control-Allow-Origin' '*'; ?? ? ? ?proxy_connect_timeout ? ? ? 600s; ?? ??? ?proxy_read_timeout ? ? ? ? ?600s; ?? ??? ?proxy_send_timeout ? ? ? ? ?600s;? ?? ??? ?access_log off; ?? ? ? ?break; ? ? ? ? } ? ? ?} ?}
到此這篇關(guān)于nginx實(shí)現(xiàn)數(shù)據(jù)庫端口轉(zhuǎn)發(fā)的文章就介紹到這了,更多相關(guān)nginx 數(shù)據(jù)庫端口轉(zhuǎn)發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx 操作響應(yīng)頭信息的實(shí)現(xiàn)
這篇文章主要介紹了Nginx 操作響應(yīng)頭信息的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05使用nginx部署前端項(xiàng)目的實(shí)現(xiàn)
前端項(xiàng)目的部署以前一直是把靜態(tài)資源放到后端工程中,隨后端部署一起部署,本文主要介紹了使用nginx部署前端項(xiàng)目的實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-10-10Nginx多ip部署多站點(diǎn)的實(shí)現(xiàn)步驟
使用Nginx在具有多個IP地址的服務(wù)器上部署多個站點(diǎn),從而實(shí)現(xiàn)高效、安全的網(wǎng)站托管,本文主要介紹了Nginx多ip部署多站點(diǎn)的實(shí)現(xiàn)步驟,感興趣的可以了解一下2024-01-01使用Nginx實(shí)現(xiàn)端口轉(zhuǎn)發(fā)TCP代理的實(shí)現(xiàn)示例
本文主要介紹了使用Nginx實(shí)現(xiàn)端口轉(zhuǎn)發(fā)TCP代理的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12nginx代理去掉URL前綴的實(shí)現(xiàn)方法
nginx作為一款廣泛使用的反向代理服務(wù)器,在實(shí)際應(yīng)用中,經(jīng)常需要去掉代理請求中的前綴,下面這篇文章主要給大家介紹了關(guān)于nginx代理去掉URL前綴的實(shí)現(xiàn)方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05