R
R
rozochkin2014-01-23 14:30:34
PHP
rozochkin, 2014-01-23 14:30:34

How to configure location and rewrite in nginx?

2nd day fiddling with nginx, fastcgi, please help.
The project is in /home/test/www/project/
The folder structure is as follows:
in /project/
*.php //php scripts
css, images, videos // folders in them content
I go to the browser subdomain.domain.com/project/main .php (or index.php should be transferred to main.php). Let's say I watch the video subdomain.domain.com/project/main.php?v=555, everything is fine and works well.
The following is required:
Hitting a link like subdomain.domain.com/project/555 so that 555 is passed to index.php as a get parameter. That is, there was an analogue of the request subdomain.domain.com/project/main.php?v=555, but without a redirect, that is, so that subdomain.domain.com/project/555 remains in the browser line.
There are no locations relative to the project folder in nginx default.
I add the first one:

location /project/ {
     rewrite ^/project/(.*)$ /project/main.php?i=$1 last;
}

I overload nginx, refresh the page, links to images, styles, js get lost, that is, I don’t see anything but text and image outlines.
I'm trying the second option:
location /project {
                index index.php;
                if (!-e $request_filename) {
                rewrite ^/([^?]*)(?:\?(.*))? /project/main.php?i=$1 last;
                }
                if ($uri ~* "\.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$") {
                        expires max;
                        break;
                }

        }

The same.
What could be the problem? I beg your advice, I don't have the strength anymore.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
rozochkin, 2014-01-27
@rozochkin

final config with which everything worked

location /project/ {
  root /home/www;
  index index.php;
  try_files $uri @php;
} 
location /project/content/ {
  alias /home/www/project/content/;
  valid_referers blocked *.mysite.com;
  if ($invalid_referer) {
       return   403;
        }
  try_files $uri @php;
}
location @php {
  rewrite ^/([^?]*)(?:\?(.*))? /project/main.php?i=$1 last;
}

if it's wrong, please correct

V
Vlad Zhivotnev, 2014-01-23
@inkvizitor68sl

Did you write a separate location for statics?

R
rozochkin, 2014-01-27
@rozochkin

Thanks everyone!
got it with:

location /project/ {
  alias /home/test/www/project/;
  index main.php;
  try_files $uri @php;
}             
location @php {
  rewrite ^/project/(.*)$ /project/main.php?i=$1 last;
}

nested location, nginx on the client's machine for some reason did not want to perceive.
and then the question arose, how now to properly prohibit after all these actions hotlinking in the folder:
/project/content/
files of 2x mp4 / mov formats are stored there in subdirectories
, and previews for these videos, jpg / jpeg formats,
I tried to add the following, but pictures/videos are not loading:
location /project/content/ {
     valid_referers none blocked mysite.com *.mysite.com;
     if ($invalid_referer) {
        return   403;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question