1

I have a web app that has static content and can serve dynamic pages and secure content. Static content lies in /public folder and should be served directly by nginx. Dynamic content is served by 127.0.0.1:3000 upstream. Secure content is also served by the upstream and available from /assets/* URLs. This secure content may have the same file extensions as static content, but it always lies in /assets/.

I have a working nginx configuration with two locations with the same proxying rules. Is there any way to reduce my config somehow joining the two locations served by the upstream?

server {
  server_name  example.name;
  root /home/user/ruby18/app/public;
  try_files $uri @thin;
  location ^~ /assets/ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://thin;
  }
  location ~* \.(jpeg|jpg|gif|png|ico|css|bmp|js)$ {
    root /home/user/ruby18/app/public;
  }
  location @thin {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://thin;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root html;
  }
}

upstream thin {
  server 127.0.0.1:3000;
}
ujifgc
  • 186
  • 1
  • 10

1 Answers1

3

include directive is what you're looking for.

/etc/nginx/proxy.conf

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://thin;

/etc/nginx/nginx.conf

server {
    server_name  example.name;
    root /home/user/ruby18/app/public;
    try_files $uri @thin;
    location ^~ /assets/ {
        include proxy.conf;
    }
    location @thin {
        include proxy.conf;
    }
    ... 
quanta
  • 51,413
  • 19
  • 159
  • 217