HiveBrain v1.2.0
Get Started
← Back to all entries
principleMinor

Nginx Alias vs Rewrite

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
aliasrewritenginx

Problem

I have two working configurations that, on the surface, seem to do the same thing. They both accomplish the functionality that I need:

location ~ ^/(css|images|js)/ {
    location ~ '^/(css|js)/[0-9]{8}-(.*)

AND

location ~ ^/(css|images|js)/ {
    rewrite '^/(css|js)/[0-9]{8}-(.*)

They both take a URL like /css/87654321-styles.css and deliver the file /css/styles.css. I lean toward the second solution because it's more succinct, but I don't know if one is better than the other for performance reasons, unintended side-effects, etc.

Here's my original SO post, for reference/context.

After being directed to the documentation for the Nginx rewrite module, I found these interesting lines:


The ngx_http_rewrite_module module directives are processed in the following order:



  • the directives of this module specified on the server level are executed sequentially;



  • repeatedly:





  • a location is searched based on a request URI;



  • the directives of this module specified inside the found location are executed sequentially;



  • the loop is repeated if a request URI was rewritten, but not more than 10 times.





So, if I used the rewrite directive without the break flag, there could be at least one additional loop of the server level directive. { alias /$1/$2; } root /server/path/to/web/root; }


AND

%%CODEBLOCK_1%%

They both take a URL like /css/87654321-styles.css and deliver the file /css/styles.css. I lean toward the second solution because it's more succinct, but I don't know if one is better than the other for performance reasons, unintended side-effects, etc.

Here's my original SO post, for reference/context.

After being directed to the documentation for the Nginx rewrite module, I found these interesting lines:


The ngx_http_rewrite_module module directives are processed in the following order:



  • the directives of this module specified on the server level are executed sequentially;



  • repeatedly:





  • a location is searched based on a request URI;



  • the directives of this module specified inside the found location are executed sequentially;



  • the loop is repeated if a request URI was rewritten, but not more than 10 times.





So, if I used the rewrite directive without the break flag, there could be at least one additional loop of the server level directive. /$1/$2 break; root /server/path/to/web/root; }


They both take a URL like /css/87654321-styles.css and deliver the file /css/styles.css. I lean toward the second solution because it's more succinct, but I don't know if one is better than the other for performance reasons, unintended side-effects, etc.

Here's my original SO post, for reference/context.

After being directed to the documentation for the Nginx rewrite module, I found these interesting lines:


The ngx_http_rewrite_module module directives are processed in the following order:



  • the directives of this module specified on the server level are executed sequentially;



  • repeatedly:





  • a location is searched based on a request URI;



  • the directives of this module specified inside the found location are executed sequentially;



  • the loop is repeated if a request URI was rewritten, but not more than 10 times.





So, if I used the rewrite directive without the break flag, there could be at least one additional loop of the server level directive. { alias /$1/$2; } root /server/path/to/web/root; }

AND

%%CODEBLOCK_1%%

They both take a URL like /css/87654321-styles.css and deliver the file /css/styles.css. I lean toward the second solution because it's more succinct, but I don't know if one is better than the other for performance reasons, unintended side-effects, etc.

Here's my original SO post, for reference/context.

After being directed to the documentation for the Nginx rewrite module, I found these interesting lines:


The ngx_http_rewrite_module module directives are processed in the following order:



  • the directives of this module specified on the server level are executed sequentially;



  • repeatedly:





  • a location is searched based on a request URI;



  • the directives of this module specified inside the found location are executed sequentially;



  • the loop is repeated if a request URI was rewritten, but not more than 10 times.





So, if I used the rewrite directive without the break flag, there could be at least one additional loop of the server level directive.

Solution

-
alias is like root, so you need to use full server path

-
you can add break flag to rewrite for avoid internal redirect. added without break rewrite will make loop (internal redirect) for process all locations again; but break tell to rewrite do not loop and run current location.

I don't think here big difference in performance. Main difference is changing internal request uri. But in case of static files serve both work well.

Context

StackExchange Code Review Q#73342, answer score: 4

Revisions (0)

No revisions yet.