最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx?Socket代理的實(shí)現(xiàn)方法

 更新時(shí)間:2024年04月29日 09:56:37   作者:shandongwill  
Nginx的socket代理通常指的是Nginx通過(guò)stream模塊來(lái)處理非HTTP的?TCP?流量,本文就來(lái)介紹一下Nginx?Socket代理的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下

前言

Nginx 的 socket 代理通常指的是 Nginx 通過(guò) stream 模塊來(lái)處理非 HTTP 的 TCP 流量,比如數(shù)據(jù)庫(kù)連接、SSH 連接或其他 TCP 協(xié)議的流量。stream 模塊允許 Nginx 作為一個(gè)反向代理來(lái)處理這些連接。

簡(jiǎn)單的 Nginx stream 代理配置

以下是一個(gè)簡(jiǎn)單的 Nginx stream 代理配置示例,用于代理 TCP 連接:

events {  
    worker_connections  1024;  
}  
  
stream {  
    server {  
        listen <local_port>;  # Nginx 監(jiān)聽(tīng)的本地端口  
        proxy_pass <backend_server>:<backend_port>;  # 后端服務(wù)器的地址和端口  
  
        # 可選配置項(xiàng)  
        # proxy_connect_timeout 1s;  # 連接超時(shí)時(shí)間  
        # proxy_timeout 10m;        # 代理超時(shí)時(shí)間  
    }  
}

在這個(gè)配置中,你需要替換 <local_port> 為 Nginx 將要監(jiān)聽(tīng)的本地端口,以及 <backend_server> 和 <backend_port> 為實(shí)際的后端服務(wù)器地址和端口。

負(fù)載均衡配置

stream 模塊還支持負(fù)載均衡。你可以使用 upstream 塊來(lái)定義一組后端服務(wù)器,然后在 server 塊中引用這個(gè) upstream 塊。

stream {  
    upstream backend_servers {  
        server backend1.example.com:12345;  
        server backend2.example.com:12345;  
        # 可以添加更多服務(wù)器  
  
        # 可選配置項(xiàng)  
        # hash $remote_addr;  # 根據(jù)客戶端 IP 進(jìn)行哈希負(fù)載均衡  
        # least_conn;         # 使用最少連接數(shù)的服務(wù)器  
    }  
  
    server {  
        listen <local_port>;  
        proxy_pass backend_servers;  
    }  
}

注意幾點(diǎn):

  • stream 模塊:確保你的 Nginx 版本支持 stream 模塊。較新版本的 Nginx 默認(rèn)包含這個(gè)模塊。
  • 非 HTTP 流量:stream 模塊處理的是 TCP 流量,不是 HTTP 流量。因此,它不適合代理 web 請(qǐng)求。
  • 安全性:當(dāng)你代理敏感數(shù)據(jù)(如數(shù)據(jù)庫(kù)連接)時(shí),請(qǐng)確保使用加密連接(如 SSL/TLS),并在 Nginx 配置中啟用相應(yīng)的加密選項(xiàng)。
  • 負(fù)載均衡:除了簡(jiǎn)單的代理功能外,你還可以使用 stream 模塊來(lái)實(shí)現(xiàn) TCP 連接的負(fù)載均衡。這可以通過(guò)在 upstream 塊中定義多個(gè)后端服務(wù)器來(lái)實(shí)現(xiàn)。
  • 日志和監(jiān)控:與 HTTP 代理一樣,你也可以為 stream 代理配置日志和監(jiān)控功能,以便跟蹤和調(diào)試連接問(wèn)題。

一、編譯安裝支持stream 模塊的Nginx

1.安裝必要的編譯工具和依賴項(xiàng)

在 CentOS 7 上,您可以使用以下命令安裝這些工具:

sudo yum install gcc-c++ pcre-devel zlib-devel make

2. 下載Nginx源代碼

下載 Nginx 1.24.0 的源代碼壓縮包,并解壓縮:

wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz

改名
mv nginx-1.24.0 nginxSrc

3. 配置編譯選項(xiàng)

進(jìn)入 Nginx 源代碼目錄并運(yùn)行configure腳本,指定所需的stream功能模塊。

[root@td66 nginxSrc]# ./configure --prefix=/usr/local/nginx --with-stream
checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... not found
checking for eventfd() ... found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for UDP_SEGMENT ... not found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for strerrordesc_np() ... not found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for ioctl(FIONREAD) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE2 library ... not found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

這將配置Nginx以使用"/usr/local/nginx"作為安裝目錄。

4. 編譯和安裝

[root@td66 nginxSrc]# make && make install
make -f objs/Makefile
make[1]: 進(jìn)入目錄“/usr/local/nginxSrc”
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_log.o \
	src/core/ngx_log.c

5. 啟動(dòng) Nginx

cd /usr/local/nginx/sbin/
./nginx

6. 驗(yàn)證安裝

打開(kāi)您的 Web 瀏覽器并訪問(wèn)服務(wù)器的 IP 地址或域名,您應(yīng)該能夠看到 Nginx 的歡迎頁(yè)面。

二、Nginx命令

nginx 命令用于控制 Nginx 服務(wù)器的啟動(dòng)、停止、重新加載配置文件等操作。以下是一些常用的 nginx 命令及其說(shuō)明:

1. 啟動(dòng) Nginx

nginx

這個(gè)命令將啟動(dòng) Nginx 服務(wù)器。如果配置文件(通常是 /etc/nginx/nginx.conf 或 /usr/local/nginx/conf/nginx.conf)存在且沒(méi)有語(yǔ)法錯(cuò)誤,Nginx 將開(kāi)始監(jiān)聽(tīng)配置的端口,并處理請(qǐng)求。

2. 停止 Nginx

nginx -s stop

或者

sudo service nginx stop

或者在某些系統(tǒng)上

sudo systemctl stop nginx

這些命令將停止正在運(yùn)行的 Nginx 服務(wù)器。-s stop 選項(xiàng)發(fā)送一個(gè)信號(hào)給 Nginx 主進(jìn)程,讓它立即停止。

3. 重新加載配置

nginx -s reload

或者

sudo service nginx reload

或者在某些系統(tǒng)上

sudo systemctl reload nginx

這個(gè)命令將重新加載 Nginx 的配置文件。如果配置文件有變動(dòng),這個(gè)命令將應(yīng)用新的配置,而不需要停止和重新啟動(dòng) Nginx。重新加載配置通常不會(huì)導(dǎo)致正在處理的請(qǐng)求中斷。

4. 測(cè)試配置文件的語(yǔ)法

nginx -t

這個(gè)命令將檢查 Nginx 配置文件的語(yǔ)法是否正確,并返回結(jié)果。如果配置文件有語(yǔ)法錯(cuò)誤,nginx -t 會(huì)指出錯(cuò)誤的位置,但不會(huì)實(shí)際加載配置。

5. 顯示版本信息

nginx -v

這個(gè)命令將顯示當(dāng)前安裝的 Nginx 的版本信息。

6. 顯示編譯選項(xiàng)

nginx -V

這個(gè)命令將顯示 Nginx 在編譯時(shí)使用的選項(xiàng)和包含的模塊。這對(duì)于診斷問(wèn)題或了解特定模塊是否已編譯非常有用。

7. 其他常用命令

  • 查看幫助信息nginx -h 或 nginx --help
  • 平滑升級(jí) Nginx:可以使用 nginx -s quit 來(lái)優(yōu)雅地關(guān)閉舊版本的 Nginx,然后啟動(dòng)新版本。

請(qǐng)注意,上述命令可能需要使用 sudo 來(lái)獲取管理員權(quán)限,具體取決于你的系統(tǒng)設(shè)置和 Nginx 的安裝方式。此外,不同系統(tǒng)或安裝方式可能會(huì)使用不同的服務(wù)管理器(如 systemctlservice 或 /etc/init.d/nginx 腳本),所以停止和啟動(dòng)服務(wù)的命令可能有所不同。

三、Nginx stream配置

3.1 編輯nginx.conf文件

vim nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

stream {  
    server {  
        listen 6666;  # Nginx 監(jiān)聽(tīng)的端口  
        proxy_pass 10.68.8.70:6666;  # 后端服務(wù)器的地址和端口  
    }  
}

3.2檢查配置文件是否正確

nginx -t -c nginx.conf

如果報(bào)如下錯(cuò)誤說(shuō)明沒(méi)有成功安裝stream模塊

nginx: [emerg] unknown directive "stream" in /usr/local/nginx/conf/nginx.conf:16

3.3 使配置文件生效

nginx -s reload

到此這篇關(guān)于Nginx Socket代理的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Nginx Socket代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • nginx反向代理java項(xiàng)目方式

    nginx反向代理java項(xiàng)目方式

    文章簡(jiǎn)要介紹了如何使用Nginx作為反向代理來(lái)部署Java項(xiàng)目,核心在于配置proxy_pass指令
    2024-12-12
  • LNMP原理與簡(jiǎn)單部署過(guò)程

    LNMP原理與簡(jiǎn)單部署過(guò)程

    LNMP架構(gòu),是指在Linux平臺(tái)下,由運(yùn)行Nginx的web服務(wù)器,運(yùn)行PHP的動(dòng)態(tài)頁(yè)面解析程序和運(yùn)行MySQL的數(shù)據(jù)庫(kù)組成的網(wǎng)站架構(gòu),也是當(dāng)前常用的系統(tǒng)架構(gòu)之一,本文主要介紹LNMP原理與簡(jiǎn)單部署,感興趣的朋友一起看看吧
    2023-08-08
  • nginx下gzip配置參數(shù)詳解

    nginx下gzip配置參數(shù)詳解

    這篇文章主要介紹了nginx下gzip配置參數(shù)詳解,本文同時(shí)給出了配置例子,以及一些注意事項(xiàng),需要的朋友可以參考下
    2014-08-08
  • 配置nginx 重定向到系統(tǒng)維護(hù)頁(yè)面

    配置nginx 重定向到系統(tǒng)維護(hù)頁(yè)面

    今天抽時(shí)間給大家普及nginx 重定向到系統(tǒng)維護(hù)頁(yè)面的配置內(nèi)容,nginx重定向問(wèn)題說(shuō)起來(lái)也很簡(jiǎn)單,因?yàn)橹囟ㄏ蚝笾苯犹D(zhuǎn)到靜態(tài)頁(yè)面,不需要后續(xù)操作和記錄,所以直接301永久重定向。今天簡(jiǎn)單給大家介紹配置方法,一起看看吧
    2021-06-06
  • Nginx配置使用詳解

    Nginx配置使用詳解

    Nginx是一個(gè)高性能的HTTP和反向代理web服務(wù)器。本文詳細(xì)講解了Nginx配置使用的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 分布式架構(gòu)中關(guān)于正向代理反向代理面試提問(wèn)

    分布式架構(gòu)中關(guān)于正向代理反向代理面試提問(wèn)

    這篇文章主要為大家介紹了分布式架構(gòu)中關(guān)于正向代理反向代理的面試提問(wèn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Keepalived搭建nginx高可用的實(shí)現(xiàn)

    Keepalived搭建nginx高可用的實(shí)現(xiàn)

    這篇文章主要介紹了Keepalived搭建nginx高可用的實(shí)現(xiàn),通過(guò)VIP實(shí)現(xiàn)主備切換,檢測(cè)進(jìn)程狀態(tài)確保服務(wù)可用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-05-05
  • nginx配置SSL/TLS證書(shū)的實(shí)現(xiàn)步驟

    nginx配置SSL/TLS證書(shū)的實(shí)現(xiàn)步驟

    本文詳細(xì)介紹了nginx配置SSL/TLS證書(shū)的實(shí)現(xiàn)步驟,確保通過(guò)HTTPS安全訪問(wèn),步驟包括DNS解析驗(yàn)證、下載和安裝SSL證書(shū)、安裝Nginx、配置nginx.conf文件以及設(shè)置HTTPS重定向,感興趣的可以了解一下
    2024-09-09
  • 使用Nginx解決跨域問(wèn)題的步驟詳解

    使用Nginx解決跨域問(wèn)題的步驟詳解

    這篇文章主要給大家介紹了使用Nginx解決跨域問(wèn)題的方法,文中有詳細(xì)的流程步驟,通過(guò)圖片介紹的非常詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Nginx 日志改成 JSON 格式的方法

    Nginx 日志改成 JSON 格式的方法

    下面小編就為大家分享一篇Nginx 日志改成 JSON 格式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03

最新評(píng)論

安乡县| 海丰县| 彝良县| 浦城县| 拉孜县| 启东市| 德安县| 德格县| 布尔津县| 自治县| 突泉县| 邳州市| 洪泽县| 鹤岗市| 海伦市| 绍兴市| 云霄县| 霍山县| 周宁县| 南通市| 沙雅县| 兴安县| 喜德县| 莱阳市| 芜湖市| 潢川县| 滕州市| 高台县| 荣昌县| 英山县| 延吉市| 曲水县| 张掖市| 济宁市| 嘉善县| 榕江县| 鹤壁市| 东海县| 门源| 嘉黎县| 安平县|