Server configuration:

server { listen 80; server_name example.com; root /home/Web/www/$server_name/; index index.php index.html; charset utf-8; location ~* \.php$ { return 403; } location / { } } 

Index file - index.php. When you download example.com/ Nginx gives it exactly (since index index.php index.html; indicated), the location ~* \.php$ works and gives 403 error, everything is OK, as it should.

But if I make such a change to the block:

 location / { proxy_pass http://backend; } 

Then, when downloading, example.com/ will immediately proxy to Backend, although the index is worth it, and its location is also processed.

I don’t understand why if the location / block is empty, then a suitable location ~* \.php$ will be processed, if you set proxying, the request starts processing location / .

Why is that? index index.php index.html; setting index index.php index.html; not work index index.php index.html; or what? How to fix it? Thank.

  • 2
    Because location / is no longer trying to search for anything on the disk. What and why do you want to fix? - Alexey Ten
  • 2
    While your question looks like Error XY . Why do you need index.php if you still proxy the request? - Alexey Ten
  • @AlexeyTen so that when going to example.com/ handles the location ~* \.php$ {} , when going to example / test.html and the like, it proxied the request. return 403; just given as an example. - Vadim
  • 2
    What "and the like"? And, for example, example.com/about/ where should it lead? - Alexey Ten
  • one
    why so I do not know, it is obvious that index is processed after location. although most likely the location is processed several times, first to index, then if there is an appeal to the local file system, and there is no file, index is processed, and then the location is processed for the new path. And apparently it is necessary to do proxying with a more complex condition, so that those urls that should be processed locally simply do not fall under it. for example, location ~* /.$ { proxy ... } although I cannot vouch for accuracy, does $ accept whether it is appropriate ~* ... - Mike

1 answer 1

In general, your example is parsed in the documentation for nginx :

Processing the “/” request is more complex. Only the prefix location “/” corresponds to it, therefore the request is processed in it. Then the index directive checks the existence of index files according to its parameters.

In addition, I highly recommend repeatedly, until full enlightenment, to re-read the section "procedure for applying location" (for example, one and two times ).

Specifically, your example is parsed to the smallest detail.

How to understand URI / in your config:

A) for the case of an empty location / :

  1. For the request / only one location is suitable - location / . This location is empty, but inherits root and index from the server block.
  2. Internal redirection (index directive) is applied from / to /index.php . Processing of the new request begins.
  3. For the /index.php request, both location ~* \.php$ and location \ are suitable, will be used (see location priority order) location given by the regular schedule, it will give 403 and finish processing the request.

For clarity, the equivalent server configuration:

 server { listen 80; server_name example.com; root /home/Web/www/$server_name/; charset utf-8; location ~* \.php$ { return 403; } location / { index index.php index.html; } } 

So clearly shows that your example is fully consistent with the disassembled documentation.

B) for the case of a location with a proxy server

  1. For the request / only one location is suitable - location / . This location transfers the backend processing, at that the request processing by nginx is completed.

This is what concerns the query given in the question itself. There was another request in the comments:

If you type example.com/index.php, then location ~ * .php $ {} works, regardless of whether there is something in the location / {} block or not.

For the request /index.php , which location is suitable? That's right - both, but the first will be applied to the location ~ * .php $ (see the priority of the location), and in it your processing ends with issuing 403.

How to fix it?

Above, I described how your config works. From your point of view, it works "wrong". But how correctly - you do not write, so here you need to clearly set the task - what do you actually want from nginx? I completely agree with the comment. Что «и подобное»? - this is vague and incomprehensible.

I suspect you need location = / {...} but not sure. It may be worth asking a new question, in which to refer to the current and clearly explain what you want to see the location and what you want to send. And it is better not to try "for example" to replace one action with another: if you want to give the file - write like this, and not "give the file, but instead for the example 403". It only confuses you as a questioner and respondents.