How to Rewrite If File Not Found Using Nginx

Nginx - php-files not found after rewrite

Okie you should try this

  location /assets/ {
alias /var/www/html/theme/theme_1/;
}

If that doesn't work then try

  location /assets/ {
alias /var/www/html/theme/theme_1/;
try_files $uri $uri/ /index.php?$args;
}

Edit-1

On second look I realize the previous answer won't work as ~ \.php { block will catch everything with php extension and the other assets block can never get called. So the solution is to nest the rewrite inside the php block. So use

  location ~ \.php$ {
rewrite ^/assets/(.*)$ /theme/theme_1/$1;

include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

nginx rewrite if file exist

The -f operator requires a pathname, use $document_root or $request_filename.

For example:

if ( -f $document_root$uri ) { ... }

Or:

if ( -f $request_filename ) { ... }

See this document for details.

nginx rewrite - proxy if file not exists

You should adjust your location block to use a combination of a rewrite and a proxy_pass directive, such that each request is rewritten dynamically. As your configuration stands, the variables don't appear to be resolved on a per-request basis - according to Nginx's documentation, only the server name, port, and URI can be specified by variables.

What you need is something akin to this, rewriting the URI and definitively specifying a proxy:

location @fallback 
{
resolver 8.8.8.8;
rewrite ^.*$ /bucket/$arg_member/$arg_imgname break;
proxy_pass http://s3.amazonaws.com;
proxy_set_header Host s3.amazonaws.com;
}

and to satisfy your switchable scheme (HTTP and HTTPS), the best option is likely to have two separate server blocks, with two different @fallback locations - one for HTTP and the other for HTTPS. The proxy_pass location cannot resolve $scheme in this context. Details are somewhat sketchy on what is supported (and why), but Nginx's documentation is all we have to go on.

Redirect Image file not found to Uri with NGinx

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.

How to configure nginx to try_files first, if not exists follow the request_uri

Try with this

location ^~ ^/cat/([0-9]{4})([0-9]{1,4}) {

try_files "/cat/ny/$1/$2.html" @app_server;

}
location @app_server{

# pass this to your app server for processing.

}
  1. Use ^~ as this will also include the edge case like /cat/12345/ (ending slash).
  2. And just be sure whether you want $uri (which is without query string) or $request_uri
    ( which contains query string).


Related Topics



Leave a reply



Submit