Trav Erse
Trav Erse

Reputation: 191

nginx upstream config in a separate file

I have site on nginx on 10.0.0.1 with simple config file:

default.conf

server {
    listen       80;
    server_name  server.com;

    location / {
        root /www;
        index index.html;
    }

Also I want to redirect requests to http://10.0.0.1/app1 to 3 servers with the same app on port 8888, like:

http://10.0.0.1/app1 -> http://10.0.0.(2,3,4):8888/app1

so I have to add to my default.conf such configuration for balancing:

upstream app1 {
   server 10.0.0.2:8888;
   server 10.0.0.3:8888;
   server 10.0.0.4:8888;
}

server {
   listen 80;

   location /app1/ {
      rewrite ^/app1^/ /$1 break;
      proxy_pass http://app1;
   }
}

but I want to keep this balancing config in a separate file - app1.conf.

If I have this two config files in /etc/nginx/conf.d/ folder I can only open URL http://10.0.0.1/

But when I open http://10.0.0.1/app1 I get error 404 because of default.conf it tries to find app1 in /www and even not tries to check app1.conf for balancing rules. So it seems than works only default.conf config file. How can fix it?

Upvotes: 2

Views: 14009

Answers (3)

Edgar Huynh
Edgar Huynh

Reputation: 181

Here you can import NGINX from different files using the following patterns.

  1. /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

include /etc/nginx/conf.d/stream.conf;

include /etc/nginx/conf.d/http.conf;

2.1. /etc/nginx/conf.d/stream.conf

stream {

  include /etc/nginx/conf.d/upstream/*.conf; # Include Upstream Block first.
  include /etc/nginx/conf.d/stream/*.conf;

}

2.2. /etc/nginx/conf.d/http.conf

http {
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;

  sendfile            on;
  tcp_nopush          on;
  tcp_nodelay         on;
  keepalive_timeout   65;
  types_hash_max_size 4096;

  include             /etc/nginx/mime.types;
  default_type        application/octet-stream;

  include /etc/nginx/conf.d/upstream/*.conf; # Include Upstream Block first.
  include /etc/nginx/conf.d/http/*.conf;

}

3.1. /etc/nginx/conf.d/upstream/application.domain.com.conf

upstream application.domain.com {
  server 192.168.0.1:80;
  server 192.168.0.2:80;
  server 192.168.0.3:80;
}

upstream ssl-application.domain.com {
  server 192.168.0.1:443;
  server 192.168.0.2:443;
  server 192.168.0.3:443;
}

4.1. /etc/nginx/conf.d/stream/application.domain.com.conf

server {
  listen 80;
  listen [::]:80;

  proxy_pass application.domain.com;
}

server {
  listen 443;
  listen [::]:443;

  # SSL Pass-through Technique. Passing SSL Encrypt/Decrypt to Back-End Application.
  proxy_pass ssl-application.domain.com;
}

4.2. /etc/nginx/conf.d/http/application.domain.com.conf

server {
  listen      80;
  listen      [::]:80;
  server_name _;
  root        /usr/share/nginx/html;

  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://application.domain.com;
  }

}

server {
  listen      443;
  listen      [::]:443;
  server_name _;
  root        /usr/share/nginx/html;

  # Handle SSL Certificate here.

  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;

    # Either SSL Terminate or not. For SSL Passthrough you need to config
    # via Stream block - Layer 4 TCP Protocol.
    proxy_pass https://ssl-application.domain.com;
  }

}

Upvotes: 0

unNamed
unNamed

Reputation: 1019

The upsteam part needs to be in the http block anyways, which resides in your nginx.conf/default.conf.
For just the location block you pobably can use:

default.conf

http {
...
    upstream app1 {
       server 10.0.0.2:8888;
       server 10.0.0.3:8888;
       server 10.0.0.4:8888;
    }
...
server {
    listen       80;
    server_name  server.com;

    include /path/to/app1.conf;

    location / {
        root /www;
        index index.html;
    }
...
include /etc/nginx/conf.d/*;
...
}

app1.conf

location /app1/ {
  rewrite ^/app1^/ /$1 break;
  proxy_pass http://app1;
}

Edit the path for the include in the default.conf.

Edit:
Actually I made a mistake here. The directives of nginx are hierarchical. In the documentation you can find where you can use which block. The server block needs to be in the http block. The location block can be in server and location blocks.
Depending which block you are in you can use include to import blocks in that specific context.
So using include in the server block you can include app specific location blocks but not server blocks. That is because the server block can only reside in the http block.
I hope this helps to clarify your situtation.

Edit2:
From your comment i just saw that the regex in the rewrite is probably wrong.

app1.conf

location /app1/ {
  rewrite ^/[^\/]+)(/.*) $1 break;
  proxy_pass http://app1;
}

Upvotes: 5

Farhad Farahi
Farhad Farahi

Reputation: 39277

Try the following:

  1. Create a file /etc/nginx/upstream.conf

    server 10.0.0.2:8888;
    server 10.0.0.3:8888;
    server 10.0.0.4:8888;
    
  2. Change your config to:

    upstream app1 {
        include /etc/nginx/upstream.conf;
    }
    
    server {
        listen 80;
    
        location /app1/ {
        rewrite ^/app1^/ /$1 break;
        proxy_pass http://app1;
        } 
    }
    

Upvotes: 1

Related Questions