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'm trying to package 2 applications that use nginx as a proxy and deliver each a config file into /etc/nginx/conf.d/.

Doing this in one file (combined.conf) works great :

    upstream backend1 {
      http://localhost:8989;
    }

    upstream backend2 {
      http://localhost:8990;
    }

    server {
      location /backend1/ {
        proxy_pass  http://backend1;
      }
      location /backend2/ {
        proxy_pass  http://backend2;
      }

However, when splitting into 2 files, one of the redirects fails systematically:

  • backend1.conf:

    upstream backend1 {
      http://localhost:8989;
    }
    
    server {
      location /backend1/ {
        proxy_pass  http://backend1;
      }
    
  • backend2.conf:

    upstream backend2 {
      http://localhost:8990;
    }
    
    server {
      location /backend2/ {
        proxy_pass  http://backend2;
      }
    

So my question is : can an http node have 2 different server childs ?

Nginx documentation says nothing about it.

Other people seem to have succeeded with this kind of architecture though :(

Nginx version is 1.1.19-1ubuntu0.1.

Thanks for any advice !

share|improve this question
    
To be more precise, when I use the 2 files approach it is the default site that answers with a 404. –  oDDsKooL Feb 13 '13 at 10:41

2 Answers 2

A http block can have many server children. However, nginx selects one server block to process a request. So, the request never 'sees' the backend2 location because it matches against the first server block.

share|improve this answer
    
You mean probably it is my location directive that sucks and grab the request intended for backend2 ? –  oDDsKooL Feb 13 '13 at 11:25
    
Or is it that I need a way to differentiate both server blocks when nginx looks for the appropriate server to forward the request ? –  oDDsKooL Feb 13 '13 at 13:18
    
Both location blocks should be within the same server block. –  chrskly Feb 13 '13 at 17:48
    
Ok got it thanks. –  oDDsKooL Feb 13 '13 at 18:10
up vote 1 down vote accepted

after so rumbling & testing, I figured a way to make it work and be able to ship one config file per application.

Here it is, dispatched in one common file and a pair of upstream/location files per application:

  • /etc/nginx/conf.d/common-proxies.conf:

    include /upstreams/*.conf;
    
    server {
    include /locations/*.conf
    }
    
  • /etc/nginx/locations/backend1.conf

    location /backend1/ {
      upstream http://backend1;
    }
    
  • /etc/nginx/locations/backend2.conf

    location /backend2/ {
      upstream http://backend2;
    }
    
  • /etc/nginx/upstreams/backend1.conf

    upstream backend1 {
      http://localhost:8989;
    }
    
  • /etc/nginx/upstreams/backend2.conf

    upstream backend2 {
      http://localhost:8990;
    }
    
share|improve this answer
    
Is this a typo? upstream should have "server localhost:8990" instead of "localhost:8990;";. See here: nginx.org/en/docs/http/ngx_http_upstream_module.html –  mbdev Jul 29 at 3:51
    
It seems from the doc you linked that every command inside a block is ended by a semi column. E.g. upstream backend { server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; }. Also, this works with the ; :) –  oDDsKooL Jul 29 at 7:15
    
I meant that the keyword 'server' is missing before the address. Ignore the semicolon part. –  mbdev Jul 31 at 4:05

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.