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.

im using the config below. it works, but the problem for me is the repeating of some lines. the proxy_* directives need to be in location /admin also, because only one location will be executed by nginx per request. what options do i have to remove this duplicate code without extracting it in new files and include it afterwards?

is there an option to tell nginx do continue with location @rails after location /admin was selected and processed?

ah and btw, what is the difference between location @rails and location /?

server {
  ...
  location /admin {
    include /etc/nginx/force_ssl;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://rails;

  }

  location @rails {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://rails;
  }
...
}

/etc/nginx/force_ssl:

if ( $scheme = "http" ) {
  rewrite  ^/(.*)$  https://$host/$1 permanent;
}
share|improve this question
add comment

1 Answer

You are asking multiple questions.


Regarding the first question, how to get rid of redundancy:

In general you can move any part of the config file into a separate file and then use the include directive to include it once or multiple times. include directive


Second question regarding redirecting from one location to the other:

Using the rewrite directive you can call one location from another. Internally Nginx will create a second request to handle the called location, but the user doesn't see anything of that. With the keyword last after the rewrite destination you tell Nginx to stop processing rules at this point and select the next location based on the current content of $uri, this variable contains the result of all the already executed rewrites. So for example if you just want to redirect everything to @rails you could use a rule like this to rewrite any url into @rails and then the last tells Nginx to look for a location with the name @rails:

rewrite . @rails last;

Details are described here: rewrite module


In your third question you ask if there is any difference between the two locations @rails and /admin:

I cannot tell you based on the snipped that you posted. I only see that /admin includes one more file than @rails. So the answer to this question depends on your backend app and the context of the rest of the Nginx configuration.

share|improve this answer
    
thank you, the rewrite seems to be the best solution for me, ill check this out! meanwhile to the third question: i asked whats the difference between location @rails and location / (root, not /admin!). i can replace @rails with / with no obvious changes in the behavior of the webserver... –  ali Aug 31 '13 at 12:38
    
I cannot definitely answer the question regarding the difference between / and @rails because it depends on two factors: 1. How your backend application handles the rewritten requests 2. The rest of you Nginx conf that you didn't post. If for example your location / does nothing else than call @rails it's very possible that you could as well just drop location / and rename @rails into /, but that's only and example and I cannot guarantee anything. –  mauro.stettler Aug 31 '13 at 12:41
    
ok, i have replaced the proxy_* directives with rewrite . @reils last; in location /admin now. it redirects me to https now but it does not query the @rails app. the output remains blank white. :( –  ali Aug 31 '13 at 12:50
    
Sorry, I can't remotely debug your app, Stack Exchange is meant for precise questions that can be answered based on the information provided. –  mauro.stettler Aug 31 '13 at 12:53
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.