Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

From my understanding of nginx docs, locations can't be nested (or rather if they are the effects aren't inheritable) and proxy_pass can't belong at the server {} level. So my configuration at the moment is like this, I know I can alleviate some by using filepaths but let's pretend I want different cache headers on different paths whilst using proxy_pass. Presumably there is a better way to write this without the repition:

server {
  listen  80;
  server_name salessystem.acmecorp.com;

  location /extjs/ {
    ## proxy_buffers 128 256k;
    proxy_pass http://localhost:5400/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    add_header 'X-UA-Compatible' 'IE=Edge;chrome=1';
    expires max;
    gzip  on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 7;
    gzip_proxied any;
    gzip_types text/html text/css text/pdf application/json application/x-javascript text/javascript;
    access_log off;
    break;
  }

  location / {
    ## proxy_buffers 128 256k;
    proxy_pass http://localhost:5400/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    add_header 'X-UA-Compatible' 'IE=Edge;chrome=1';
    expires epoch;
    gzip  on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 7;
    gzip_proxied any;
    gzip_types text/html text/css text/pdf application/json application/x-javascript text/javascript;
    access_log off;
    break;
  }
}
share|improve this question
    
can the common gzip options be included in the server block instead of in each location block? is that what you are asking? –  Nikhil Mulley Jan 22 '12 at 7:02

1 Answer 1

up vote 2 down vote accepted

I think of the following for your nginx configuration :

  • since it is just the expires header that differs for your two different locations, although both matching the proxy to the same server.
  • try putting both the locations in the single block
  • based on the query string, (or location match string) set a different expires header

tag.

server {
  listen  80;
  server_name salessystem.acmecorp.com;

  location ~* (/extjs/|/) {
    ## proxy_buffers 128 256k;
    proxy_pass http://localhost:5400/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    add_header 'X-UA-Compatible' 'IE=Edge;chrome=1';
    expires max;
      if ($query_string ~ \/extjs\/) {
      expires epoch;
      }
    gzip  on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 7;
    gzip_proxied any;
    gzip_types text/html text/css text/pdf application/json application/x-javascript text/javascript;
    access_log off;
    break;
  }

}

Please however test according to your needs, especially near the regex matches at location and query_string value matching.

However it is not clear, why would you want to extjs to be served out of a proxied server (seems like a dynamic app server), since they are just plain text files if my assumption is right. The requests pertaining to extjs need not go through the proxy or app logic system just incase while they can be served independently through the filesystem. I think of keeping it in its seperate location itself, and have the unique gzip/expires directives unique to it in its own location block while the other common directives can be held in the server block itself.

For example:

server {
   ...
   common gzip directives
   common header setters
   common expires setters
   ...
   location / {
     proxy to backend app server
     settings unique to / location
     ..
   }
   location /extjs {
     settings unique to /extjs location
     expires header reset
     ...
   }
}
share|improve this answer
    
@nginxtest what did you do? I am interested in your find outs and configuration details.. thanks. –  Nikhil Mulley Feb 12 '12 at 12:15
    
Using if here is not recommended; see wiki.nginx.org/IfIsEvil for additional details. map would be a more appropriate directive to use. –  Ian Jun 16 '13 at 5:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.