Nginx 代理達夢數據庫配置
- 叨叨
- 達夢數據庫代理
- Nginx模塊說明
- stream模塊
- main 全局塊
- event塊
- http塊
- server塊
- location塊
- 本文參考了 https://blog.csdn.net/qq_42402854/article/details/132843413
叨叨
由于公司網絡安全問題,應用服務器端開開放需要走策略申請,雖安全但開發來講流程確實有點麻煩。因此,申請一個對外開放的Nginx端口供大家訪問。用其代理所有,all~~~~~
達夢數據庫代理
網上內容很多,都OK,達夢數據庫連接屬于TCP連接,因些做nginx配置時注意用stream即可。配置如下所示:
stream {upstream dm_database {server 1270.0.1:5236; # 替換為達夢數據庫服務器的IP和端口}server {listen 8080; # 替換為Nginx監聽的端口proxy_pass dm_database;}}
請注意,該節點 與http并列,并不配在http中。這樣即可。簡單吧,快去試試吧
Nginx模塊說明
都寫到這了,叨叨下Nginx各模塊說明吧
stream模塊
這個模塊用于TCP/UDP數據流的代理和負載,可用stream代理轉發TCP消息。該模塊配置與http平級,使用相似,本文數據庫代理就用的該模塊。語法與http模塊基本相同,但要注意的是
確保在Nginx編譯時包含了–with-stream模塊,以啟用stream處理功能。以下示例:
stream {upstream backend {server backend1.example.com:12345;server backend2.example.com:12345;}server {listen 12345;proxy_pass backend;proxy_connect_timeout 1s;}
}
在這個配置中,Nginx監聽在本地12345端口上的連接請求,并將它們代理到名為backend的上游組,該上游組包含兩個后端服務器。proxy_connect_timeout指定了到后端服務器的連接超時時間。
以下是一個完整的示例,參考 https://www.jianshu.com/p/1848c1b71a0d。
user nginx;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;worker_rlimit_nofile 65535;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {use epoll;worker_connections 102400;accept_mutex on;multi_accept on;}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;log_format format1 '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $http_x_forwarded_for $upstream_cache_status $request_time';access_log logs/access.log format1;server_names_hash_bucket_size 128;client_header_buffer_size 4k;large_client_header_buffers 4 8k;client_max_body_size 8m;#sendfile on;#tcp_nopush on;keepalive_timeout 60s;#tcp_nodelay on;client_header_timeout 25s;client_body_timeout 25s;send_timeout 25s;gzip on;gzip_min_length 2k;gzip_buffers 4 16k;gzip_http_version 1.0;gzip_comp_level 7;gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png image/ico;gzip_vary off;gzip_disable "MSIE [1-6]\.";proxy_headers_hash_max_size 51200;proxy_headers_hash_bucket_size 6400;proxy_buffer_size 128k;proxy_buffers 32 128k;proxy_busy_buffers_size 128k;proxy_connect_timeout 300;proxy_send_timeout 300;proxy_read_timeout 300;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}location /nginx_status{stub_status on;access_log off;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}# 各個域名單獨控制include conf.d/*.conf;
}stream {server {listen 9898;proxy_pass 192.168.0.202:9999;} }
main 全局塊
此模塊顧名思義為主配置,作用會影響Nginx服務器整體運行的配置指令,放在Nginx最頂層,從配置文件開始到events塊之間的內容。主要包括配置運行Nginx服務器的用戶(組)、允許生成的worker process數,進程PID存放路徑、日志存儲路徑和類型以及配置文件的引入等。作用域是全局的。
- user:配置運行Nginx服務器用戶(組)
- worker_processes:指定Nginx要啟動的工作進程的數量。worker_processes 1:的意思是:Nginx 服務器并發處理服務的關鍵配置,worker_processes 值越大,可以支持的并發處理量也越多,但是會受到硬件、軟件等設備的制約。
- Nginx進程
- error_log:指定錯誤日志的位置和日志級別。
- pid:指定Nginx pid文件的位置。
- events:包含影響Nginx服務器或與網絡有關的事件模塊指令。
示例如下所示:
user nginx; # 指定運行Nginx的用戶和用戶組
worker_processes auto; # 設置工作進程數為自動設置
error_log /var/log/nginx/error.log warn; # 錯誤日志位置和日志級別
pid /var/run/nginx.pid; # PID文件位置events {worker_connections 1024; # 每個工作進程的最大連接數
}http {# http模塊內容,包括服務器配置等...
}
event塊
主要作用是Nginx服務器與用戶的網絡連接信息。Nginx的事件處理機制對應不同的操作系統有不同的實現,例如kqueue、epoll、poll等。通過Nginx的命令行參數查看event模塊你也可以通過查看Nginx的命令行參數來查看Nginx使用的是哪種event模塊。
例如,你可以運行以下命令:
如果你想要查看Nginx的event模塊是哪個,你可以通過查看Nginx的配置參數來獲取。
示列1:
events {use kqueue; # 例如這是在Mac OS X中的配置# use epoll; 例如這是在Linux中的配置worker_connections 1024; #每個進程允許的最多連接數}
http塊
這個開發同學用的最多,處理http請求和響應的核心組件,負責接收客戶端請求并向客戶端發向應。 http模塊負責HTTP服務器相關屬性的配置,有server和upstream兩個子模塊
- include :來用設定文件的mime類型,類型在配置文件目錄下的mime.type文件定義,來告訴nginx來識別文件類型。
- default_type:設定了默認的類型為二進制流,也就是當文件類型未定義時使用這種方式,例如在沒有配置asp的locate環境時,Nginx是不予解析的,此時,用瀏覽器訪問asp文件就會出現下載了。
- log_format:用于設置日志的格式,和記錄哪些參數,這里設置為main,剛好用于access_log來紀錄這種類型。
示 例如下所示:
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;}
server塊
該塊是包在http塊中的,每個http塊中有多個server塊,每個server塊可以認為是一個 虛擬主機。sever塊用來定一個虛擬主機,標志定義虛擬主機開始。虛擬主機從用戶角度看,和一臺獨立的硬件主機是完全一樣的,該技術的產生是為了 節省互聯網服務器硬件成本。每個server塊又分全局server塊,它可以基于域名、IP地址、端口或者組合來處理請求。還可包括多個location塊
- listen:用于指定虛擬主機的服務端口
- server_name:用來指定IP地址或者域名,多個域名之間用空格分開。
- root :表示在這整個server虛擬主機內,全部的root web根目錄。注意要和locate {}下面定義的區分開來。
- index :全局定義訪問的默認首頁地址。注意要和locate {}下面定義的區分開來。
- charset:用于設置網頁的默認編碼格式。
- access_log:用來指定此虛擬主機的訪問日志存放路徑,最后的main用于指定訪問日志的輸出格式。
示例如下
http {server {listen 80;server_name localhost;root /Users/hk/www;index index.php index.html index.htm; charset utf-8;access_log logs/host.access.log main;aerror_log logs/host.error.log main;}
}
location塊
Nginx的location模塊用于定義如何處理特定的請求URI。通過配置location,可以控制哪些URI被Nginx處理,以及如何處理這些URI。主要作用是基于 Nginx 服務器接收到的請求字符串(例如 server_name/uri-string),對虛擬主機名稱 (也可以是IP 別名)之外的字符串(例如 前面的 /uri-string)進行匹配,對特定的請求進行處理。 地址定向、數據緩 存和應答控制等功能,還有許多第三方模塊的配置也在這里進行。location模塊 負載均衡,反向代理,虛擬域名等配置。是來定位的,定位URL,解析URL,它也提供了強大的正則匹配功能,也支持條件判斷匹配,可以通過location指令實現Nginx對動,靜態網頁進行過濾處理。
- /表示匹配訪問根目錄。
- root指令用于指定訪問根目錄時,虛擬主機的web目錄,這個目錄可以是相對路徑(相對路徑是相對于nginx的安裝目錄)。也可以是絕對路徑。
- proxy_pass:代理轉發,如果在proxy_pass后面的url加/,表示絕對根路徑;如果沒有/,表示相對路徑,把匹配的路徑部分也給代理走。
- proxy_set_header:允許重新定義或者添加發往后端服務器的請求頭。
- include:加載配置文件,后面介紹nginx多個配置文件時候會提到。
- root:定位localtion匹配的url資源路徑。
- index:定義頁面顯示html,一般和alias配合使用。
示例1如下所示:
location = / {# 處理根目錄(/)的請求
}location /images/ {# 處理請求URI開始為/images/的請求
}
示例二如下
location / { root html; index index.html index.htm;}error_page 404 /404.html; error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}#反向代理配置location /jyb {proxy_pass http://qurt/;proxy_read_timeout 1800s;proxy_set_header Host $host:$server_port;proxy_set_header X-real-ip $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }#采用uwsgi方式location /python/ {include uwsgi_params;uwsgi_pass 127.0.0.1:33333;}# FastCGI方式location ~ \.php$ {root html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;include fastcgi_params;}#訪問nginx本機目錄的文件location / {root /home/hk/;index index.html index.htm;}location /static/ {alias /var/static/;}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one#location ~ /\.ht {deny all;}}# another virtual host using mix of IP-, name-, and port-based configurationserver {listen 8000;listen somename:8080;server_name somename alias another.alias;location / {root html;index index.html index.htm;}}# HTTPS server server {listen 443 ssl;server_name localhost;ssl_certificate cert.pem;ssl_certificate_key cert.key;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;location / {root html;index index.html index.htm;}}