0

I'm trying to use proxy_set_header directive in my nginx config to add a request header. However my nginx container can't be started due to this error.

Container Log

So I check the nginx config in my container and it looks like the variable is empty and Nginx treat it as if there's a missing argument.

location block


Here is my nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    server_names_hash_bucket_size  256;
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [cache:$upstream_cache_status] [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;


    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/rss+xml text/javascript font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml;

    include /etc/nginx/conf.d/*.conf;
    
server {

   listen 80;
   server_name ${DOMAIN_NAME_CLIENT};

   large_client_header_buffers 4 16k;

   proxy_buffer_size          128k;
   proxy_buffers              4 256k;
   proxy_busy_buffers_size    256k;

   proxy_read_timeout          1800;
   proxy_connect_timeout       1800;


   location /graphql {
       auth_basic off;
       proxy_pass http://${PRIVATE_IP_CLIENT}:3000;
       proxy_set_header HOST nginx;
       proxy_pass_request_headers       on;

       limit_except GET POST OPTIONS { deny all; }
   }

   location / {
         auth_basic off;

         limit_except GET POST { deny  all; }

         proxy_pass http://${PRIVATE_IP_CLIENT}:3000;
         proxy_pass_request_headers on;
         proxy_set_header proxied nginx;
      }
   }
}    
ilovetoask
  • 31
  • 4
  • Your second argument for `proxy_set_header` isn't a variable as expected. Check out [this part of the docs](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header) about it. Additionally, this [SO question](https://stackoverflow.com/questions/39715510/nginx-when-to-use-proxy-set-header-host-host-vs-proxy-host) about when to use `proxy_set_header` vs other options might help too. – palindromeotter33 Jul 06 '22 at 15:44
  • @palindromeotter33 The second argument for `proxy_set_header` isn't expected to be a variable. It is expected to be a string, which may (or may not) contain a variable(s) to be interpolated at the request processing time. The whole nginx configuration looks like a template, where used template processing engine removed that `nginx` string for some reasons. I don't find the answer about setting the `Host` header to be very informative; I think my [answer](https://serverfault.com/a/1100774/498657) on the same subject is more understandable (maybe because it is mine :)) – Ivan Shatsky Jul 06 '22 at 17:46
  • I'm sorry, I think I've forgotten to review my nginx config before submitting my question. But what I was trying to do is to get the client IP address by defining `proxy_set_header X-Real-IP $remote_addr` but nginx would throw the "invalid number of arguments" error. I've tried the same thing with `add_header` directive by using nginx embedded variable `$remote_addr` and `$realip_remote_addr` but both isn't working. So I was thinking that the error is possibly due to the variables, I might be using them incorrectly. Any thoughts? – ilovetoask Jul 08 '22 at 06:38

0 Answers0