Redirect Image file not found to Uri with NGinx












1















I'm switching a Laravel Website from Apache (htaccess) to NGinx. I have built an Image Serving that produces a uri with the appropriate parameters for resizing ex : pics/images/max24h/music_video_red_icon.png.
I Apache does not find the file it would redirect it to a route image/images/max24h/music_video_red_icon.png where an Action in laravel creates and returns the image. In .htaccess it works with this code :



RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2 [R=307,L]


Now in NGinx conf, How do I redirect it properly? I tried a lot of suggestions like :



# Redirect pics file not found to php laravel route to create the image
location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}


It does not work at all, the server works if I remove those lines. I'm using Mac High Sierra 10.13.6 . Could it be some config conflict? Here is the full nginx.conf :



user _www _www;

worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80 default_server;
server_name localhost;
root /var/www/megalobiz/public;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

index index.html index.htm index.php;

charset utf-8;

gzip on;
gzip_vary on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss;

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

error_page 404 /index.php;

# removes trailing slashes (prevents SEO duplicate content issues)
#if (!-d $request_filename)
#{
# rewrite ^/(.+)/$ /$1 permanent;
#}

# Redirect pics file not found to php laravel route to create the image
location ~ ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ {
try_files $uri @img_proxy;
}

location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
location ~* .(?:css|js)$ {
expires 7d;
access_log off;
add_header Cache-Control "public";
}

location ~ /.(?!well-known).* {
deny all;
}

location ~ /.ht {
deny all;
}
}

include servers/*;
}









share|improve this question

























  • You are missing a ;. Test your configuration using nginx -t.

    – Richard Smith
    Nov 24 '18 at 14:44











  • Sorry. I miss it probably by playing with it. Now the Server is working but receive 404 response page with http://localhost/pics/images/max24h/music_video_red_icon.png, just like other variations I used. I'm starting to suspect config conflict or priority, I don't know NGinx enough.

    – KeitelDOG
    Nov 24 '18 at 17:27











  • Your location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ block will prevent Laravel seeing the rewritten /images/ URI.

    – Richard Smith
    Nov 24 '18 at 17:33











  • Seems logic. But I removed both cache rules, still 404 page.

    – KeitelDOG
    Nov 24 '18 at 17:49






  • 1





    Your apache code is performing an external redirect (307 response) - In order for Nginx to perform an external redirect, you will need to append redirect to your rewrite statement (for a 302 response). If you need to use a 307, you will need something slightly more complicated.

    – Richard Smith
    Nov 24 '18 at 18:12


















1















I'm switching a Laravel Website from Apache (htaccess) to NGinx. I have built an Image Serving that produces a uri with the appropriate parameters for resizing ex : pics/images/max24h/music_video_red_icon.png.
I Apache does not find the file it would redirect it to a route image/images/max24h/music_video_red_icon.png where an Action in laravel creates and returns the image. In .htaccess it works with this code :



RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2 [R=307,L]


Now in NGinx conf, How do I redirect it properly? I tried a lot of suggestions like :



# Redirect pics file not found to php laravel route to create the image
location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}


It does not work at all, the server works if I remove those lines. I'm using Mac High Sierra 10.13.6 . Could it be some config conflict? Here is the full nginx.conf :



user _www _www;

worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80 default_server;
server_name localhost;
root /var/www/megalobiz/public;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

index index.html index.htm index.php;

charset utf-8;

gzip on;
gzip_vary on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss;

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

error_page 404 /index.php;

# removes trailing slashes (prevents SEO duplicate content issues)
#if (!-d $request_filename)
#{
# rewrite ^/(.+)/$ /$1 permanent;
#}

# Redirect pics file not found to php laravel route to create the image
location ~ ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ {
try_files $uri @img_proxy;
}

location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
location ~* .(?:css|js)$ {
expires 7d;
access_log off;
add_header Cache-Control "public";
}

location ~ /.(?!well-known).* {
deny all;
}

location ~ /.ht {
deny all;
}
}

include servers/*;
}









share|improve this question

























  • You are missing a ;. Test your configuration using nginx -t.

    – Richard Smith
    Nov 24 '18 at 14:44











  • Sorry. I miss it probably by playing with it. Now the Server is working but receive 404 response page with http://localhost/pics/images/max24h/music_video_red_icon.png, just like other variations I used. I'm starting to suspect config conflict or priority, I don't know NGinx enough.

    – KeitelDOG
    Nov 24 '18 at 17:27











  • Your location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ block will prevent Laravel seeing the rewritten /images/ URI.

    – Richard Smith
    Nov 24 '18 at 17:33











  • Seems logic. But I removed both cache rules, still 404 page.

    – KeitelDOG
    Nov 24 '18 at 17:49






  • 1





    Your apache code is performing an external redirect (307 response) - In order for Nginx to perform an external redirect, you will need to append redirect to your rewrite statement (for a 302 response). If you need to use a 307, you will need something slightly more complicated.

    – Richard Smith
    Nov 24 '18 at 18:12
















1












1








1








I'm switching a Laravel Website from Apache (htaccess) to NGinx. I have built an Image Serving that produces a uri with the appropriate parameters for resizing ex : pics/images/max24h/music_video_red_icon.png.
I Apache does not find the file it would redirect it to a route image/images/max24h/music_video_red_icon.png where an Action in laravel creates and returns the image. In .htaccess it works with this code :



RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2 [R=307,L]


Now in NGinx conf, How do I redirect it properly? I tried a lot of suggestions like :



# Redirect pics file not found to php laravel route to create the image
location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}


It does not work at all, the server works if I remove those lines. I'm using Mac High Sierra 10.13.6 . Could it be some config conflict? Here is the full nginx.conf :



user _www _www;

worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80 default_server;
server_name localhost;
root /var/www/megalobiz/public;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

index index.html index.htm index.php;

charset utf-8;

gzip on;
gzip_vary on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss;

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

error_page 404 /index.php;

# removes trailing slashes (prevents SEO duplicate content issues)
#if (!-d $request_filename)
#{
# rewrite ^/(.+)/$ /$1 permanent;
#}

# Redirect pics file not found to php laravel route to create the image
location ~ ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ {
try_files $uri @img_proxy;
}

location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
location ~* .(?:css|js)$ {
expires 7d;
access_log off;
add_header Cache-Control "public";
}

location ~ /.(?!well-known).* {
deny all;
}

location ~ /.ht {
deny all;
}
}

include servers/*;
}









share|improve this question
















I'm switching a Laravel Website from Apache (htaccess) to NGinx. I have built an Image Serving that produces a uri with the appropriate parameters for resizing ex : pics/images/max24h/music_video_red_icon.png.
I Apache does not find the file it would redirect it to a route image/images/max24h/music_video_red_icon.png where an Action in laravel creates and returns the image. In .htaccess it works with this code :



RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2 [R=307,L]


Now in NGinx conf, How do I redirect it properly? I tried a lot of suggestions like :



# Redirect pics file not found to php laravel route to create the image
location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}


It does not work at all, the server works if I remove those lines. I'm using Mac High Sierra 10.13.6 . Could it be some config conflict? Here is the full nginx.conf :



user _www _www;

worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80 default_server;
server_name localhost;
root /var/www/megalobiz/public;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

index index.html index.htm index.php;

charset utf-8;

gzip on;
gzip_vary on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss;

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

error_page 404 /index.php;

# removes trailing slashes (prevents SEO duplicate content issues)
#if (!-d $request_filename)
#{
# rewrite ^/(.+)/$ /$1 permanent;
#}

# Redirect pics file not found to php laravel route to create the image
location ~ ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ {
try_files $uri @img_proxy;
}

location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
location ~* .(?:css|js)$ {
expires 7d;
access_log off;
add_header Cache-Control "public";
}

location ~ /.(?!well-known).* {
deny all;
}

location ~ /.ht {
deny all;
}
}

include servers/*;
}






macos nginx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 17:28







KeitelDOG

















asked Nov 24 '18 at 13:41









KeitelDOGKeitelDOG

375513




375513













  • You are missing a ;. Test your configuration using nginx -t.

    – Richard Smith
    Nov 24 '18 at 14:44











  • Sorry. I miss it probably by playing with it. Now the Server is working but receive 404 response page with http://localhost/pics/images/max24h/music_video_red_icon.png, just like other variations I used. I'm starting to suspect config conflict or priority, I don't know NGinx enough.

    – KeitelDOG
    Nov 24 '18 at 17:27











  • Your location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ block will prevent Laravel seeing the rewritten /images/ URI.

    – Richard Smith
    Nov 24 '18 at 17:33











  • Seems logic. But I removed both cache rules, still 404 page.

    – KeitelDOG
    Nov 24 '18 at 17:49






  • 1





    Your apache code is performing an external redirect (307 response) - In order for Nginx to perform an external redirect, you will need to append redirect to your rewrite statement (for a 302 response). If you need to use a 307, you will need something slightly more complicated.

    – Richard Smith
    Nov 24 '18 at 18:12





















  • You are missing a ;. Test your configuration using nginx -t.

    – Richard Smith
    Nov 24 '18 at 14:44











  • Sorry. I miss it probably by playing with it. Now the Server is working but receive 404 response page with http://localhost/pics/images/max24h/music_video_red_icon.png, just like other variations I used. I'm starting to suspect config conflict or priority, I don't know NGinx enough.

    – KeitelDOG
    Nov 24 '18 at 17:27











  • Your location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ block will prevent Laravel seeing the rewritten /images/ URI.

    – Richard Smith
    Nov 24 '18 at 17:33











  • Seems logic. But I removed both cache rules, still 404 page.

    – KeitelDOG
    Nov 24 '18 at 17:49






  • 1





    Your apache code is performing an external redirect (307 response) - In order for Nginx to perform an external redirect, you will need to append redirect to your rewrite statement (for a 302 response). If you need to use a 307, you will need something slightly more complicated.

    – Richard Smith
    Nov 24 '18 at 18:12



















You are missing a ;. Test your configuration using nginx -t.

– Richard Smith
Nov 24 '18 at 14:44





You are missing a ;. Test your configuration using nginx -t.

– Richard Smith
Nov 24 '18 at 14:44













Sorry. I miss it probably by playing with it. Now the Server is working but receive 404 response page with http://localhost/pics/images/max24h/music_video_red_icon.png, just like other variations I used. I'm starting to suspect config conflict or priority, I don't know NGinx enough.

– KeitelDOG
Nov 24 '18 at 17:27





Sorry. I miss it probably by playing with it. Now the Server is working but receive 404 response page with http://localhost/pics/images/max24h/music_video_red_icon.png, just like other variations I used. I'm starting to suspect config conflict or priority, I don't know NGinx enough.

– KeitelDOG
Nov 24 '18 at 17:27













Your location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ block will prevent Laravel seeing the rewritten /images/ URI.

– Richard Smith
Nov 24 '18 at 17:33





Your location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ block will prevent Laravel seeing the rewritten /images/ URI.

– Richard Smith
Nov 24 '18 at 17:33













Seems logic. But I removed both cache rules, still 404 page.

– KeitelDOG
Nov 24 '18 at 17:49





Seems logic. But I removed both cache rules, still 404 page.

– KeitelDOG
Nov 24 '18 at 17:49




1




1





Your apache code is performing an external redirect (307 response) - In order for Nginx to perform an external redirect, you will need to append redirect to your rewrite statement (for a 302 response). If you need to use a 307, you will need something slightly more complicated.

– Richard Smith
Nov 24 '18 at 18:12







Your apache code is performing an external redirect (307 response) - In order for Nginx to perform an external redirect, you will need to append redirect to your rewrite statement (for a 302 response). If you need to use a 307, you will need something slightly more complicated.

– Richard Smith
Nov 24 '18 at 18:12














1 Answer
1






active

oldest

votes


















1














The main difference between the function of the .htaccess file and your rewrite statement is that Apache will perform an external redirect using a 307 response, while Nginx performs an internal redirect instead. See this document for details.



To force Nginx to perform an external redirect, append the permanent or redirect flag to the rewrite statement, which generates a 301 or 302 response respectively.



For example:



location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2 redirect;
}


The difference between a 302 and a 307 response, is that the latter will redirect a POST request method without changing it to a GET.



If you need to redirect POST requests, you will need to capture the relevant parts of the URI with a regular expression location or if block and use a return 307 instead.



For example:



location @img_proxy {
if ($uri ~ ^/pics(.*).(jpe?g|png|gif|ico|bmp)$) {
return 307 /image$1.$2$is_args$args;
}
}


See this caution on the use of if.






share|improve this answer
























  • Very nice explanation. Thanks for taking me to the right path.

    – KeitelDOG
    Nov 25 '18 at 17:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458741%2fredirect-image-file-not-found-to-uri-with-nginx%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The main difference between the function of the .htaccess file and your rewrite statement is that Apache will perform an external redirect using a 307 response, while Nginx performs an internal redirect instead. See this document for details.



To force Nginx to perform an external redirect, append the permanent or redirect flag to the rewrite statement, which generates a 301 or 302 response respectively.



For example:



location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2 redirect;
}


The difference between a 302 and a 307 response, is that the latter will redirect a POST request method without changing it to a GET.



If you need to redirect POST requests, you will need to capture the relevant parts of the URI with a regular expression location or if block and use a return 307 instead.



For example:



location @img_proxy {
if ($uri ~ ^/pics(.*).(jpe?g|png|gif|ico|bmp)$) {
return 307 /image$1.$2$is_args$args;
}
}


See this caution on the use of if.






share|improve this answer
























  • Very nice explanation. Thanks for taking me to the right path.

    – KeitelDOG
    Nov 25 '18 at 17:33
















1














The main difference between the function of the .htaccess file and your rewrite statement is that Apache will perform an external redirect using a 307 response, while Nginx performs an internal redirect instead. See this document for details.



To force Nginx to perform an external redirect, append the permanent or redirect flag to the rewrite statement, which generates a 301 or 302 response respectively.



For example:



location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2 redirect;
}


The difference between a 302 and a 307 response, is that the latter will redirect a POST request method without changing it to a GET.



If you need to redirect POST requests, you will need to capture the relevant parts of the URI with a regular expression location or if block and use a return 307 instead.



For example:



location @img_proxy {
if ($uri ~ ^/pics(.*).(jpe?g|png|gif|ico|bmp)$) {
return 307 /image$1.$2$is_args$args;
}
}


See this caution on the use of if.






share|improve this answer
























  • Very nice explanation. Thanks for taking me to the right path.

    – KeitelDOG
    Nov 25 '18 at 17:33














1












1








1







The main difference between the function of the .htaccess file and your rewrite statement is that Apache will perform an external redirect using a 307 response, while Nginx performs an internal redirect instead. See this document for details.



To force Nginx to perform an external redirect, append the permanent or redirect flag to the rewrite statement, which generates a 301 or 302 response respectively.



For example:



location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2 redirect;
}


The difference between a 302 and a 307 response, is that the latter will redirect a POST request method without changing it to a GET.



If you need to redirect POST requests, you will need to capture the relevant parts of the URI with a regular expression location or if block and use a return 307 instead.



For example:



location @img_proxy {
if ($uri ~ ^/pics(.*).(jpe?g|png|gif|ico|bmp)$) {
return 307 /image$1.$2$is_args$args;
}
}


See this caution on the use of if.






share|improve this answer













The main difference between the function of the .htaccess file and your rewrite statement is that Apache will perform an external redirect using a 307 response, while Nginx performs an internal redirect instead. See this document for details.



To force Nginx to perform an external redirect, append the permanent or redirect flag to the rewrite statement, which generates a 301 or 302 response respectively.



For example:



location @img_proxy {
rewrite ^/pics(.*).(jpe?g|png|gif|ico|bmp)$ /image$1.$2 redirect;
}


The difference between a 302 and a 307 response, is that the latter will redirect a POST request method without changing it to a GET.



If you need to redirect POST requests, you will need to capture the relevant parts of the URI with a regular expression location or if block and use a return 307 instead.



For example:



location @img_proxy {
if ($uri ~ ^/pics(.*).(jpe?g|png|gif|ico|bmp)$) {
return 307 /image$1.$2$is_args$args;
}
}


See this caution on the use of if.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 20:20









Richard SmithRichard Smith

19.8k42137




19.8k42137













  • Very nice explanation. Thanks for taking me to the right path.

    – KeitelDOG
    Nov 25 '18 at 17:33



















  • Very nice explanation. Thanks for taking me to the right path.

    – KeitelDOG
    Nov 25 '18 at 17:33

















Very nice explanation. Thanks for taking me to the right path.

– KeitelDOG
Nov 25 '18 at 17:33





Very nice explanation. Thanks for taking me to the right path.

– KeitelDOG
Nov 25 '18 at 17:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458741%2fredirect-image-file-not-found-to-uri-with-nginx%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Futebolista

Jornalista