Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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;
}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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;
    }
    ... 
share|improve this answer
add comment

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.