syamskoy
@syamskoy

Как сделать разные location на основе IP?

Есть простой конфиг нгинкса:

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

        server_name  mydomain.ru;
        root /home/user/www;
        index index.html index.php;
        access_log /var/log/nginx/site.access.log main;
        error_log /var/log/nginx/site.error.log main;

        location / {
                root   /home/user/www;
                index  index.html index.php;
                if (!-e $request_filename){
                        rewrite ^(.*)$ /index.php;
                }
        }

        include templates/ssl;
        include templates/php7.2;
        include templates/static;
        include templates/gzip;
}


Хочу сделать другую секцию location / для определенного ip, что-то типа такого:

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

        server_name  mydomain.ru;
        root /home/user/www;
        index index.html index.php;
        access_log /var/log/nginx/site.access.log main;
        error_log /var/log/nginx/site.error.log main;

if ($remote_addr != 127.0.0.1) {
    location / {
            proxy_pass http://localhost:3000;
            proxy_redirect off;
            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_set_header   X-Forwarded-Host $server_name;
    }
} else {
    location / {
        root   /home/user/www;
        index  index.html index.php;
        if (!-e $request_filename){
            rewrite ^(.*)$ /index.php;
        }
    }
}

        include templates/ssl;
        include templates/php7.2;
        include templates/static;
        include templates/gzip;
}


Но nginx ругается на этот конфиг. В if'ах нельзя писать данные настройки. Пробовал if внутрь location перенести - тоже ругается. Как правильно сделать?
  • Вопрос задан
  • 56 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы