Home > Software design >  Pass param to @proxy location?
Pass param to @proxy location?

Time:05-23

I'd like to set up nuxt with NGINX as load balancer. Additionally, I'd like to add some caching to the images.

Now I get 404 for my images using this:

    location ~ ^/img. \.(?:ico|gif|jpe?g|webp|png|woff2?|eot|otf|ttf|svg|js|css)$ {
        expires 365d;
        add_header Pragma public;
        add_header Cache-Control "public";

        try_files $uri $uri/ @proxy;
    }
    
    location @proxy {
        expires 365d;
        add_header Content-Security-Policy "default-src 'self' 'unsafe-inline';";
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-Cache-Status $upstream_cache_status;

        proxy_redirect                      off;
        proxy_set_header Host               $host;
        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;
        proxy_ignore_headers        Cache-Control;
        proxy_http_version          1.1;
        proxy_read_timeout          1m;
        proxy_connect_timeout       1m;
        proxy_pass http://1649681_app/$request_uri;
        #proxy_cache                 nuxt-cache;
        #proxy_cache_bypass          $arg_nocache; # probably better to change this
        #proxy_cache_valid           200 302  60m; # set this to your needs
        #proxy_cache_valid           404      1m;  # set this to your needs
        #proxy_cache_lock            on;
        #proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
        #proxy_cache_key             $uri$is_args$args;
    } 

It seems like $request_uri is wrong in the line proxy_pass http://1649681_app/$request_uri - but how can I pass the requested path to the @proxy-location?

CodePudding user response:

No, it isn't completely wrong to specify an URI using variables for the proxy_pass directive inside the named (as well as the regex) locations. However such a thing will have a drawback - if you can't specify your upstream address using IP rather than hostname, you'll need either a resolver defined in your configuration (worse) or an additional upstream block to define your 1649681_app backend (better). More details can be found here (exactly the same is applicable to named locations too).

Being that said, how do you think, what a request URI will be passed to the upstream specified in the proxy_pass directive if you won't specify any explicitly? It will be exactly the URI being processed (and if for some reason you'd need to pass a modified URI instead, you'd need to to modify it via the rewrite rules).

For the given configuration, assuming you don't need to modify the request URI or query arguments, you should simply use the

proxy_pass http://1649681_app;

And, do you understand what the $uri/ parameter does exactly mean? It makes the try_files directive to check if the given URI is a directory to search an index file inside it. I really doubt you need it using that kind of regex pattern. Remove it, it is only an extra (some kind of expensive) system kernel stat call. Use

try_files $uri @proxy;

instead.

  • Related