<- Back

Nginx配置 http从stream中获取真实客户端ip

Created at 2022-10-28 18:25:45

Last updated at 2022-10-28 18:25:45

4840 Clicks

将nginx作为webapp的前置端,使用nginx控制是否转发给webapp,还是转发给http服务。
其中分发的原来请自行搜索学习。

配置使用了nginx的stream等模块,在Ubuntu 20.04上的Nginx v.1.18.0(来自nginx官方apt源),所需的模块都启用了。

理论上就是通过proxy_protocol从stream将客户端ip传给http,在http的log_format中,将remote_addr更换成proxy_protocol_addr即可。

proxy_protocol在使用时注意,在输出时,使用"proxy_protocol on;"来配置,接受时在listen的末尾加上”proxy_protocol“即可。

但是webapp在接收tcp时,不能处理proxy_protocol,所以在转发给webapp之前,需要再将proxy_protocol去掉,只需多一步转发即可。

具体配置nginx.conf开头内容如下:

stream {
    log_format basic '$remote_addr - $remote_user [$time_local] '
                     '$protocol $status $bytes_sent $bytes_received '
                     '$session_time';
    map $ssl_preread_server_name $backend {
        webapp6.domain.com unix:/run/nginx-webapp-stream.sock;
        webapp.domain.com unix:/run/nginx-webapp-stream.sock;
        default 127.0.0.1:443;
    }
    server {
        listen unix:/run/nginx-webapp-stream.sock proxy_protocol;
        proxy_pass 127.0.0.1:8443;
    }
    server {
        listen 0.0.0.0:443;
        listen [::]:443;
        proxy_pass $backend;
        ssl_preread on;
        proxy_protocol on;
    }
}

http {
    log_format combined '$proxy_protocol_addr - $remote_user [$time_local] '
                        '"$request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent"';
    server {
        listen 127.0.0.1:80 proxy_protocol;
        listen [::1]:443 ssl proxy_protocol;
        ...
    }
}

Tags:ITNetworkUbuntuLinuxtrojanproxy

63 Comments

Avtoservis_eqPr (fkedzfmfpPr@xruma.store)
#198

Надежный партнер в уходе за вашим автомобилем в Москве, где ваш автомобиль станет в порядок быстр

Created at 2024-12-01 16:43:20

uselesslame (baiqiangcompany@163.com)
#197

Looking for business contact details? Let me do the hard work for you! Get all the info you need for

Created at 2024-11-21 09:35:24

KevinQuima (snabpipe@gmail.com)
#196

ENGINEERING SERVICES: casting of heat-resistant parts, rods, impellers, turbine blades, We will supp

Created at 2024-11-18 16:09:40

XRwrito (xrumer23writo@gmail.com)
#195

Hello. Good cheer to all on this beautiful day!!!!! Good luck :)

Created at 2024-11-16 17:39:05

JosephNum (mjkwgmz85fgkc3i@tempmail.us.com)
#194

ToMyAccount.com specializes in offering verified social media accounts that work seamlessly. Our PVA

Created at 2024-11-16 14:53:16

@ 2019-2024 OWENDSWANG
Contact me: owendswang@qq.com