I have a user's home alias set up like this:

location ~ ^/~(.+?)(/.*)?$ {
  alias /home/$1/public_html$2;
  include fastcgi_params;

I have a PHP application in one of the folders, and I want to rewrite to /~user/app/index.php when someone visits /~user/app/whatever. I would think this line would work:

try_files $uri $uri/ /home/$1/public_html$2/index.php;

But it doesn't, and nginx configuration is some of the most confusing stuff I've ever had to deal with. What am I missing here?

On a separate but related note, I really miss .htacess when I use nginx. I know people say it's insecure or whatever but it's really nice to keep directory-specific configuration out of this main file.


Your question is unclear.

Do you wish to rewrite an URI (a location) to another (/~user/app/whatever to /~user/app/index.php)? Or do you wish to serve content for /~user/app/whatever with the files contained at /home/$1/public_html/app/whatever/index.php?

nginx' configuration works differently from Apache's, that is a fact. Saying it is 'more confusion' is a personal feeling. I would object that, on the contrary of Apache, nginx allows to create lean, clean and order-independent configurations, guaranteeing readability, maintenance and scalability.

It is normal to be lost because there is a learning curve for every new technology you need to handle. I suppose Apache's configuration is not always straightforward and when you started with it, it was not that obvious how to deal with it...

Regarding .htaccess, it is mixing content with configuration, polluting repositories with files you then need to protect to avoid serving them...

nginx allows you as much granularity and modularity you wish through the use of the include directive, which provides the nice feature of separating the configuration in multiple files, adding them manually where you wish inside the upper-level configuration and even include whole directories at once, allowing a per-server, per-whatever configuration file.

If you need specific rules for a location, just create location block for it. And if you wish to isolate a whole branch from the locations tree in a separate configuration file, you can. This is a cleaner/more readable/more maintainable way of doing what .htaccess was doing wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.