G
G
German Zvonchuk2014-04-14 22:40:01
Nginx
German Zvonchuk, 2014-04-14 22:40:01

Nginx Rewrite, how to solve the problem with images from a special folder?

Good day.
Before I installed NGINX on the server, I only had Apache on the server and, accordingly, the rules were written in htaccess

RewriteRule photos/(.+)$ modules/qplboard/image_db/$1

After I switched to NGINX, I could not write a valid config so that the photos would be given not by Apache, but by NGINX. We are talking about photos whose URL has been rewritten through htaccess.
nginx vhost.conf
server {
access_log off;

error_log  logs/vhost-error_log warn;
listen    80;
server_name  mydomain.ru www.mydomain.ru;

location ~* \.(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid)$ {
root   /home/username/public_html/mydomain.ru;
expires 14d;
}

location / {
client_max_body_size    10m;
client_body_buffer_size 128k;

proxy_send_timeout   90;
proxy_read_timeout   90;

proxy_buffer_size    4k;
# you can increase proxy_buffers here to suppress "an upstream response
#  is buffered to a temporary file" warning
proxy_buffers     16 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

proxy_connect_timeout 30s;

proxy_redirect  http://www.mydomain.ru:8080   http://www.mydomain.ru;
proxy_redirect  http://mydomain.ru:8080   http://mydomain.ru;

proxy_pass   http://192.168.0.1:8080/;

proxy_set_header   Host   $host;
proxy_set_header   X-Real-IP  $remote_addr;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

Full let to photos:
/home/username/public_html/mydomain.ru/modules/qplboard/image_db/
With this rule, requests go to Apache
location ~ ^/photos/(.*)$ {
    rewrite  ^/photos/(.+)$  /mydomain.ru/modules/qplboard/image_db/$1;
}

tail -f mydomain.ru
109.12.218.212 - - [15/Apr/2014:00:12:40 +0500] "GET /photos/8952_1_b.JPG HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36"

I wrote this rule right after server_name.
With this configuration, requests go only to NGINX, photos do not open.
404 Not Found
nginx/1.4.7
location ~ ^/photos/(.*)$ {
    root /mydomain.ru/modules/qplboard/image_db;
    access_log off;
    expires 14d;
}

I would be very grateful to you if you help me write a rule under which, when accessing
www.mydomain.ru/photos/8952_1_b.JPG
, NGINX will give photos from the folder /home/username/public_html/mydomain.ru/modules/qplboard/image_db/
For the rest of the pictures, the rule that is already described in vhost.conf is suitable

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
German Zvonchuk, 2014-04-15
@inside22

Working code. Files from this folder are given by NGINX.

location ~ /photos/(.*)$ {
alias /home/username/public_html/mydomain.ru/modules/qplboard/image_db/$1;
expires 14d;
}

V
Vlad Zhivotnev, 2014-04-15
@inkvizitor68sl

location ~^/photos/ { 
alias /home/username/public_html/mydomain.ru/modules/qplboard/image_db; 
}

Something like this. If you only need to send images with a certain extension from there, then expand the regular expression to ~^/photos/.*\.(gif|jpg|jpeg|png|ico)$, for example

G
German Zvonchuk, 2014-04-15
@inside22

@inkvizitor68sl tried in different ways, unfortunately nothing works.
Once it turned out "403 Forbidden", though I don't know why :-(

location ~^/photos/ {
alias /home/username/public_html/mydomain.ru/modules/qplboard/image_db;
}

nginx/1.4.7
location ~^/photos/(.*)$ {
alias /home/username/public_html/mydomain.ru/modules/qplboard/image_db/$1;
}

nginx/1.4.7
location ~ ^/photos/(.*)$ {
alias /home/username/public_html/mydomain.ru/modules/qplboard/image_db;
}

nginx/1.4.7

G
German Zvonchuk, 2014-04-15
@inside22

I continue experimenting with the NGINX config:

location ~ /photos/6426_1_b.JPG {
     rewrite ^ /modules/qplboard/image_db/6426_1_b.JPG;
}

location ~ /photos/6426_1_b.JPG {
alias /modules/qplboard/image_db/6426_1_b.JPG;
}

404 Not Found
nginx/1.4.7

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question