我如何在nginx中拦截所有传出的响应并修改它们?

n53p2ov0  于 2023-01-20  发布在  Nginx
关注(0)|答案(1)|浏览(271)

bounty将在5天后过期。回答此问题可获得+300声望奖励。Marius Anderie希望引起更多人关注此问题。

我有一个nginx服务器,通过php为一个论坛提供服务。我想添加一个上游到nginx拦截php应用程序生成的所有响应,并修改响应。
是否可以通过修改nginx conf文件来实现这一点?
以下是我的现有配置:

server {
    listen 443 ssl;
    server_name example.net www.example.net;
    index index.php index.html;
    root /var/www/html;

    location / {
        try_files $uri $uri/ /index.php?$uri&$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass xf2-php-fpm:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME     $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

我怎样修改这个配置来将php生成的所有响应传递给一个上游?

anhgbhbe

anhgbhbe1#

您可以在当前块之外添加一个新的上游块,用于定义要用作上游的服务器

upstream upStream{
    server upStream_server:4000;
}

然后修改处理PHP请求的location块,将请求代理到上游服务器

location ~ \.php$ {
    proxy_pass http://upStream; #URL to which the request should be proxied
    proxy_set_header Host $host; #Hostname of the server to which the request is being sent to
    proxy_set_header X-Real-IP $remote_addr; #IP address of the client that made the request
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #IP address of the client that made the request and any other IP addresses that the request was forwarded through
    //
}

这将把所有请求传递到upStream服务器,响应将被发送回客户端。如果需要更多帮助,请告诉我

相关问题