H
H
HoHsi2015-10-14 00:10:22
Nginx
HoHsi, 2015-10-14 00:10:22

Is it possible to inherit blocks in Nginx?

Goodnight!
Is it possible to inherit locales in Nginx as includes or mixins?
For example like this:

server {
    listen       80;

    location / {
        proxy_pass @node_proxy; # инклюд миксины
    }

    location /test/ {
        add_header Tester '1';

        proxy_pass @node_proxy; # инклюд миксины
    }

    location @node_proxy {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://127.0.0.1:1337;
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lynn "Coffee Man", 2015-10-14
@HoHsi

Because you wrote it is not possible. But you can use include
File node-proxy.inc

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:1337;

Your config:
server {
    listen       80;

    location / {
        include node-proxy.inc;
    }

    location /test/ {
        add_header Tester '1';
        include node-proxy.inc;
    }
}

True, in this simple case, I would write like this:
server {
    listen 80;

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    location / {
        proxy_pass http://127.0.0.1:1337;
    }

    location /test/ {
        add_header Tester '1';
        proxy_pass http://127.0.0.1:1337;
    }
}

E
evnuh, 2015-10-14
@evnuh

LOCALES IS LOCALE, THIS IS NOT LOCATION !!!!!!!!!!!!!!!!!!!!!111!!!!!!!!!!!!!!!!!!!! !!!!!!!
you can’t, nginx works in such a way that it uses only 1 location for each request. They can redirect to each other, but the request will be processed only by one, the last location. You can transfer common properties for locations to a file and include this file in all necessary locations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question