how to get headers to nginx?

34 Views Asked by At

So, here's the situation. I have a service running on the server that makes requests to VK (a third-party API), but it does it through its own Nginx. In other words, it sends a request to itself, and Nginx proxies it to VK (the third-party API) with different IP addresses. I'm using NestJS as the backend. The version of Nginx is 1.24.

Let's move on to the NestJS code. The code sends requests to different Nginx locations, and in the request, where the IP is located, there are domains/service1, service2, etc. All parameters, code, versionVk, access_token, are passed in the headers because they need to be added specifically to the proxied address.

async checkIsClosedGroup(code, ip) {
    const access = process.env['ACCESS_TOKEN'];
    const versionVk = process.env['VERSION_VK'];

    try {
      const response = await firstValueFrom(
        this.httpService.get<any>(`${ip}`, { headers: {
          'code': encodeURIComponent(code),
          'versionVk': versionVk,
          'access_token': access,
        },
        })
        .pipe(
           catchError((error: AxiosError) => {
            if (error.response && 'data' in error.response && error.response.data != undefined{
              this.logsServicePostsAdd.error(
                `checkIsClosedGroup1 error`,
                `ошибка получения постов в группе ${error.response} код ${code}`,
              );
            }
            throw new Error(error),
            );
          }),
        ),
      );
      if (!response) {
      this.logsServicePostsAdd.error(
        `checkIsClosedGroup4 error`,
        `Неверный формат данных от VK API ${response} запрос не успешный для ${code}`,
      );
    }
      const data = response.data;
      return data;
    } catch (err) {
      console.error('Error:', err.request_params);
      await this.logsServicePostsAdd.error(`ошибка получения постов ${err}`, 'ERRORS',);
    }
}

Now let's move on to the nginx settings and the options I've tried.

Option #1 (attempted with JavaScript): I created a file named headers.js to process the request headers. The file is located in etc/nginx. I attempted to use the JavaScript module to extract the headers passed and log them to see what's coming into Nginx. So, here I return the values of the headers code, versionVk, and access_token.

export default {
    getCode: function(r) {
        return r.headersIn['code'] || '-';
    },
    getVersionvk: function(r) {
        return r.headersIn['versionVk'] || '-';
    },
    getAccessToken: function(r) {
        return r.headersIn['access_token'] || '-';
    },
    logHeaders: function(r) {
        var headers = '';
        for (var key in r.headersIn) {
            if (r.headersIn.hasOwnProperty(key)) {
                headers += key + ': ' + r.headersIn[key] + ', ';
            }
        }
        r.log(headers);
        return '-';
    }
};

nginx.conf file. Here, I'm attempting to retrieve what I did in headers.js and log these headers.

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
load_module modules/ngx_http_js_module.so;

events {
    worker_connections  1024;
}

http {

    js_import /etc/nginx/headers.js;
    js_set $custom_code headers.get_code;
    js_set $custom_versionvk headers.get_versionvk;
    js_set $custom_access_token headers.get_access_token;
    js_set $log_headers headers.logHeaders; # Для отладки

    resolver 8.8.8.8;
    include       /etc/nginx/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" '
                     '"$custom_code" "$custom_versionvk" "$custom_access_token" "$log_headers"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

After the request, I see the following in the file /var/log/nginx/access.log. So, no headers are passed at all.

`79.141.68.156 - - [12/Feb/2024:07:31:19 +0000] "GET /service1/ HTTP/1.1" 200 158 "-" "axios/1.6.5"
79.141.68.156 - - [12/Feb/2024:07:31:20 +0000] "GET /service2/ HTTP/1.1" 200 158 "-" "axios/1.6.5"
79.141.68.156 - - [12/Feb/2024:07:31:21 +0000] "GET /service3/ HTTP/1.1" 200 158 "-" "axios/1.6.5"
79.141.68.156 - - [12/Feb/2024:07:31:22 +0000] "GET /service4/ HTTP/1.1" 200 158 "-" "axios/1.6.5"

Option 2 that I tried is:

Файл nginx.conf.

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
load_module modules/ngx_http_js_module.so;

events {
    worker_connections  1024;
}

http {

    map $http_code $custom_code {
        default "-";
        "~^(?P<value>.+)$" $value;
    }

    map $http_versionvk $custom_versionvk {
        default "-";
        "~^(?P<value>.+)$" $value;
    }

    map $http_access_token $custom_access_token {
        default "-";
        "~^(?P<value>.+)$" $value;
    }

    resolver 8.8.8.8;
    include       /etc/nginx/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" '
                     '"$custom_code" "$custom_versionvk" "$custom_access_token"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

Also, there is no information about the headers in the logs.

I also tried to receive them in the default.conf file using this approach:

location /service2 {
    set $code $arg_code;
    set $access_token $arg_access_token;
    set $versionVk $arg_versionVk;
    proxy_pass https://api.vk.com/method/execute?code=$arg_code&access_token=$arg_access_token&v=$arg_versionVk;
    proxy_bind 7*.**.**.1**;
    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;
}

And like this:

location /service1 {
    set $args code=$custom_code&access_token=$custom_access_token&v=$custom_versionvk&$args;
    proxy_pass https://api.vk.com/method/execute?$args;
    proxy_bind 7*.**1.**.1**;
    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;
}
0

There are 0 best solutions below