B
B
Bobsans2018-09-09 04:09:38
Nginx
Bobsans, 2018-09-09 04:09:38

Is it possible to make location dynamic in nginx?

Dear admins!
Tell me, please, is it possible in nginx to make it so that when localhost:8000 is triggered, the static is returned by the server, and when the localhost:8010 backup is triggered - by nginx.
For example, there is this config:

upstream main {
    server                      localhost:8000;
    server                      localhost:8010 backup;
}

server {
    listen                      80;
    ...

    location / {
        proxy_pass              http://main;
        proxy_set_header        Host $http_host;
    }

    location /static {
        alias                   /home/www/static;
        access_log              off;
    }
}

Is it possible to do something like this?:
upstream main {
    server                      localhost:8000;
    server                      localhost:8010 backup;
}

server {
    listen                      80;
    ...

    location / {
        proxy_pass              http://main;
        proxy_set_header        Host $http_host;
    }

    if ($backup_used) {
        location /static {
            alias               /home/www/static;
            access_log          off;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivankomolin, 2018-09-12
@ivankomolin

It is possible, but in your case it is not necessary.
Separate the logic into 2 different servers.
Pros:
1. Nginx will process this faster
2. Easier to read when working with configs

server {
    listen                      8080;

    location / {
        proxy_pass              http://main;
        proxy_set_header        Host $http_host;
    }
}

server {
    listen                      8010;

    location / {
        proxy_pass              http://main;
        proxy_set_header        Host $http_host;
    }

    location /static {
        alias                   /home/www/static;
        access_log              off;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question