F
F
fenric2014-01-12 23:11:20
PHP
fenric, 2014-01-12 23:11:20

How to detect 404 error in nginx on port 80 and redirect request to port 8080, which apache server is listening on?

Guys, see the situation without nginx:
1. The client requested a file, say image.jpg, which does not physically exist, apache processes the request, realizing that the file does not physically exist, redirects the request to the php side, which in turn creates this file.
2. The client again requested a file, image.jpg which physically exists (see point 1), therefore it is immediately given to the client.
And now look at the situation with nginx:
1. The client requested a file, say image.jpg, which does not physically exist, nginx notifies the client of a 404 error.
Actually, the question is how to implement the correct scheme in the nginx settings of this kind:
1. The client requested a non-existent file, nginx processes the request and redirects it to apache (8080 port).
2. Apache, having processed the request, acts according to the scheme described at the very beginning (pp. 1.2 at the very beginning).
What do you advise? Thank you.
PS If in one line: How to detect a 404 error in nginx on port 80 and redirect the request to port 8080, which is listening to the apache server.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey, 2014-01-12
Protko @Fesor

nginx has a try_files directive for this.
Why do you need apache in this chain? Is it possible to send such requests directly to php-fpm?

X
xaker1, 2014-01-12
@xaker1

location{
....
error_page 404 = /404.php;
}

Accordingly, 404.php is the script that creates your file. In this case, he can also give the file to the client. Then there will be no difference between the first request and the rest.

V
Vlad Zhivotnev, 2014-01-13
@inkvizitor68sl

error_page 404 = @fallback;
location / { ...; }
location ~* ^.+\.(jpg|jpeg|gif|...) { ...; }
location @fallback {
  proxy_pass http://127.0.0.1:81;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header X-Real-IP $remote_addr;
}

Most likely so. And in Apache (in htaccess), you probably already process non-existent files.

S
Sergei Chukhan, 2014-01-13
@street

If there are not many such requests, then you can enable caching of 200 responses in nginx, and hang up request processing in PHP. But this method can "eat" RAM.

V
Viktor Taran, 2014-02-07
@shambler81

error_page 404 = @fallback;
#--------------All 404s are sent to Apache for processing----------------------------- --
location @fallback {
proxy_pass http://$host:82;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question