S
S
Sergei Iamskoi2018-03-14 18:01:33
Nginx
Sergei Iamskoi, 2018-03-14 18:01:33

How to make different location based on IP?

There is a simple nginx config:

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;
}

I want to make another location / section for a specific ip, something like this:
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;
}

But nginx swears at this config. You can't write settings data in if's. I tried to move if inside location - it also swears. How to do it right?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question