Understanding alias (used as rewrite)

Edho Arief me at myconan.net
Wed Jun 24 10:50:25 UTC 2015

On Wed, Jun 24, 2015 at 3:44 PM, E.B. <emailbuilder88 at yahoo.com> wrote:
> Thanks for your ongoing helps! I hope someone
> can advise further
>> >> You're probably looking for this
>> >>
>> >> fastcgi_param SCRIPT_FILENAME /another/different/path/anotehrscript.php;
>> >
>> > Excellent point! Thanks you!
>> > However, what if the alias was NOT to a php file? Is using
>> > rewrite the only solution - alias not able to working? What
>> > is alias doing to cause 404?
>> This config works for me.
>>   location ~ ^/test {
>>     alias /data/public_html/somefile.php;
>>     include fastcgi.conf;
>>     fastcgi_pass;
>>   }
> Yes, I had also got similar to work, but
> only for the exact match uri-- the first
> in my list of possible uris that must work:
> /my-long-prefix-goes-here
> /my-long-prefix-goes-herexxx
> /my-long-prefix-goes-here/
> /my-long-prefix-goes-here/filename
> I still get 404 for the last 3. That's why
> I thinking it was adding the end of the original
> uri to the alias redirect. but Im not sure.

you need the regexp-based alias (as in my example).

> (I used prefix match not regex match but I
> now tried your version with regex and get same
> result)
>> Check what your error log says to find out why it returns 404.
> Im not getting anything in my log I dont know
> why (access log shows the requested uri but
> I cant find the file not found error). I checked
> php log too and basic nginx error log. I have a
> access and error log set for this domain. Is
> that where the file not found error should be going?
>> Also the question mark you put will become part of file name php look
>> for (it'll look for 'file.php?' instead of 'file.php'.
> I was using the ? to try and avoid unknown file
> lookups because I thought the part of the uri
> after the matching location prefix was being
> added to the aliased location.  I thought I could
> pass it as a query string to PHP so I could ignore
> it that way.
> But you help clarify that it won't work that way
> so I understand partly why I am getting 404, thank
> you.
> Part of what is not clear is if/when alias will have
> the rest of the uri after the match prefix added to
> it??  The doc:
> http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
> says nothing, but it begins to appear to me that if
> you end the aliased location with / then it will have
> the rest of the original uri added to it, but if you
> don't end the alias with / then it will be taken without
> changes.
> If that's not right, can someone explain?

it depends on the type of locaton. If it's regexp-based alias
(location ~ ^/some/(regexp)), the full path is replaced with whatever
in alias params but otherwise the trailing request uri (the one after
path specified in location) will be appended to the alias.

More information about the nginx mailing list