V
V
Vladimir2018-09-08 07:00:01
Nginx
Vladimir, 2018-09-08 07:00:01

How to send all requests to a single file in nginx?

Greetings.
There is a task to close access for users. Allow your own, send strangers to pages on another domain with truncated information.
Now, at the cost of a million trial and error, this solution turned out, in nginx :

location / {
        root       "%hostdir%";
        rewrite ^(.*)$ /redirect.php;
    }

    location /x/ {
        alias	"%hostdir%";
        try_files $uri /index.php?$args;
        internal;

        rewrite ^/x//list_autors/(.*)$ /index.php?g=$1 last;
        rewrite ^/x//posts/(.*)$ /index.php?q=$1 last; 
    }

In redirect.php file:
if (!$user) {
        #здесь редиркт если не наш пользовалель
    } else {
        header("X-Accel-Redirect: /x/".$_SERVER["REQUEST_URI"]);
    }

As a result, we have the following: list_autors/* and posts/* links work as they should, static files (images, text) work as required, but if you access a file, for example http://domain/index.php, then the file opens directly, without the participation of redirect.php.
I can't help but feel like I'm doing something wrong.
Can you please tell me where is the error or what am I doing wrong?

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